Is it possible to do an inner join on 2 tables where both the tables are on different server??
Add a linked server (B) to server A then write the following query
SELECT
*
FROM
[SERVERB].[DATABASE].[SCHEMA].[TABLE] A
INNER JOIN [SERVERA].[DATABASE].[SCHEMA].[TABLE] B ON A.ID = B.ID
It is certainly possible in SQL code. How you would do it in C# I don't know but in SQl Server, I would set up linked servers and then the code is:
select t1.field1, t2.field2
From server1.database1.dbo.table1 t1
join server2.database2.dbo.table2 t2
on t1.id = t2.id
So you just use the four part name instead of the three part name. But you do have to have a linked server set up first.
You can download both tables to the client, then perform a join using LINQ.
For more detail, please provide more details.
If you are using SQL Server try using a Linked Server, if Oracle use a database link. I am not sure how it would be achieved in the rest.
Related
We're using .NET Entity Framework to talk to an Azure SQL database. We used QueryOriginInterceptor to add some comments to the top of each SQL command being sent to SQL Server, with the goal of helping identify the location where a particular query came from in the code.
The problem is, when looking at long running queries in the Azure UI (and looking in sys.dm_exec_query_stats), the comments are not there.
For example, if we run this query:
-- Stack:
-- Utils.Orders.GetOrders
select *
from [Order] o
join OrderItem oi on oi.OrderId = o.ID
And looking in Azure, the long running query looks like:
Is there a way to preserve these comments?
sys.dm_exec_query_stats does not include the comments, but dm_exec_sql_text does.
This artice explains how to use the two to diagnose issues.
The relevant SQL query from the article is:
SELECT TOP 25
databases.name,
dm_exec_sql_text.text AS TSQL_Text,
CAST(CAST(dm_exec_query_stats.total_worker_time AS DECIMAL)/CAST(dm_exec_query_stats.execution_count AS DECIMAL) AS INT) as cpu_per_execution,
CAST(CAST(dm_exec_query_stats.total_logical_reads AS DECIMAL)/CAST(dm_exec_query_stats.execution_count AS DECIMAL) AS INT) as logical_reads_per_execution,
CAST(CAST(dm_exec_query_stats.total_elapsed_time AS DECIMAL)/CAST(dm_exec_query_stats.execution_count AS DECIMAL) AS INT) as elapsed_time_per_execution,
dm_exec_query_stats.creation_time,
dm_exec_query_stats.execution_count,
dm_exec_query_stats.total_worker_time AS total_cpu_time,
dm_exec_query_stats.max_worker_time AS max_cpu_time,
dm_exec_query_stats.total_elapsed_time,
dm_exec_query_stats.max_elapsed_time,
dm_exec_query_stats.total_logical_reads,
dm_exec_query_stats.max_logical_reads,
dm_exec_query_stats.total_physical_reads,
dm_exec_query_stats.max_physical_reads,
dm_exec_query_plan.query_plan,
dm_exec_cached_plans.cacheobjtype,
dm_exec_cached_plans.objtype,
dm_exec_cached_plans.size_in_bytes
FROM sys.dm_exec_query_stats
CROSS APPLY sys.dm_exec_sql_text(dm_exec_query_stats.plan_handle)
CROSS APPLY sys.dm_exec_query_plan(dm_exec_query_stats.plan_handle)
INNER JOIN sys.databases
ON dm_exec_sql_text.dbid = databases.database_id
INNER JOIN sys.dm_exec_cached_plans
ON dm_exec_cached_plans.plan_handle = dm_exec_query_stats.plan_handle
WHERE databases.name = 'AdventureWorks2014'
ORDER BY dm_exec_query_stats.max_logical_reads DESC;
I recently asked a question on StackOverflow (MySQL Returns All Rows When field = 0) regarding a query statement not working in MySQL. I now have a very similar problem, this time using OleDB where I am trying to use a join to include fields that have 0 as an entry, but not select every field in the table as a result.
The new look MySQL query posted in the above question as the accepted answer works without a hitch. However the OleDB counterpart I have written to do almost the same does not. It's a bit messy as the tables are not named very well (I didn't create this database) and I'm getting a simple syntax error;
myQuery.CommandText = "SELECT s.scm_num, s.scm_name, c.cr3_text, q.qsp_value, s.scm_bprefix, s.scm_nxbnum FROM qspreset q INNER JOIN sdccomp s LEFT OUTER JOIN cntref3 c ON s.scm_cntref3=c.cr3_id AND q.qsp_relat=s.scm_invtype AND q.qsp_what=13";
I'm querying another table here as well as the two involved in the LEFT OUTER JOIN and I believe that is where I am making the mistake.
Join conditions need to be with the join
myQuery.CommandText =
"SELECT s.scm_num, s.scm_name, c.cr3_text, q.qsp_value, s.scm_bprefix, s.scm_nxbnum
FROM qspreset q
INNER JOIN sdccomp s
on q.qsp_relat = s.scm_invtype AND q.qsp_what = 13
LEFT OUTER JOIN cntref3 c
ON s.scm_cntref3 = c.cr3_id";
q.qsp_what = 13 can be moved to a where
I happen to like this style
In the case of MSSQL T-SQL and some queries with a lot of joins I have gotten more efficient query plan by moving a where condition up into a join. The filter happened early rather that last.
If you don't believe you can put a hard value in a join see SQLfiddle
I have recently inherited a set of very large SQL Server databases. the application and database schema are a mess. I have run across a few fields in the database that store different types of sensitive data, where they should not be stored. since there are almost 10,000 tables in my database, I am in desperate need of a way to programmatically scan a few of these databases to find out where the data is. I realize this will be very resource intensive, so I have setup a server specifically to run a scan on backups of the databases.
I also have zero dollars for purchasing any tools.
Does anyone know of a way with C# and SQL that I can scan all user tables in the database for sensitive data?
an example of scanning for one type of data (eg. SSN) would be extremely helpful. I confident that I can extrapolate that into all the scenarios I would need.
this sql will list all user tables and row counts in a database. It will be a starting point..
SELECT o.name,
ddps.row_count
FROM sys.indexes AS i
INNER JOIN sys.objects AS o ON i.OBJECT_ID = o.OBJECT_ID
INNER JOIN sys.dm_db_partition_stats AS ddps ON i.OBJECT_ID = ddps.OBJECT_ID
AND i.index_id = ddps.index_id
WHERE i.index_id < 2 AND o.is_ms_shipped = 0 ORDER BY o.NAME
Hth,
O
This query will help you to find the column with particular name and datatype
SELECT t.name AS table_name,
SCHEMA_NAME(t.schema_id) AS schema_name,
c.name AS column_name ,tp.name
FROM sys.tables AS t
INNER JOIN sys.columns c ON t.OBJECT_ID = c.OBJECT_ID
INNER JOIN sys.types tp ON tp.user_type_id=c.user_type_id
WHERE c.name LIKE '%Product%' AND tp.name LIKE '%int%'
ORDER BY schema_name, table_name;
This might be irrelevant at this point of time but shall serve as an additional note: You can use Information Schema Views to query the database objects which comply with the ISO standard definition for the INFORMATION_SCHEMA.
MSDN LINK
If you can open the DB into Microsoft SQL Server Managment Studio, you can try to use ApexSQL . It's a plugin that can be downloaded from here:
http://www.apexsql.com/sql_tools_search.aspx
For example: you select the database and you can look for a column name. It will show you all tables in which you have that column.
Hope it helps.
I need to execute a Query(Select only) with join of two tables from different database of same server.
Eg query will be similar to:
SELECT * FROM DB1.tbl_a LEFT JOIN DB2.tbl_b ON DB1.tbl_a.fieldX = DB2.tbl_b.fieldY WHERE ....
Where tbl_a,tbl_b are 2 tables from 2 different database DB1,DB2 respectively
How to do that? How can I connect to MySQL server without specifying the database in the connection string but in sql query Using C#.?
Actually this question has already been answered, here is the answer :
Yes, assuming the account has appropriate permissions you can use:
SELECT ...
FROM A.table t1
JOIN B.table2 t2 ON t2.column = t1.col
You just need to prefix the table reference with the name of the database it resides in.
I need to execute a Query(Select only) with join of two tables from different database of same server.
For this SQL User have access on both Databases.
Query will be like this:
SELECT * FROM [DB1].[dbo].[tbl_a] T1 LEFT JOIN [DB2].[dbo].[tbl_b] T2 ON T1.fieldX = T2.fieldY WHERE ....
Is this possible:
Joining tables in two databases in one server?
1.1 What connection string shall i use?
Joining tables in two databases in different server?
2.1 What connection string shall i use?
If I remember correctly you can do that in the follwoing fashion:
select * from [database name].[owner].table_name a
inner join [database_name].[owner].table_name b on (a.id = b.a_id)
If the database is on another server you must first create a linked server:
USE master
GO
EXEC sp_addlinkedserver
'RemotDB',
N'SQL Server'
GO
When servers are linked you can reference a table in on the other server with the following syntax:
[server_name].[database_name].[owner].table_name
Not sure what OpenQuery is, but I've done both scenarios without it. Just use fully qualified names.
1:
SELECT *
FROM SpecificDatabase.dbo.SpecificTable spec
INNER JOIN CommonDatabase.dbo.CommonTable comm ON spec.someField = comm.someField
2: (setup a linked server object)
SELECT *
FROM SpecificDatabase.dbo.SpecificTable spec
INNER JOIN LinkedServer.CommonDatabase.dbo.CommonTable comm ON spec.someField = comm.someField
For connection strings, are you just referring to what goes in the From and Join clauses or do you mean connection string values in the code?
Nobody has answered your question regarding the connection string, so I'll jump in here. If you have 2 servers A and B, you can set up a linked server on server A pointing to server B (as detailed in the other postings). You then run your query on server A referencing server B in your query (again as detailed in the other postings). You are running this query on server A, so your connection string will be to server A.
Make sure the user in the connection string has authorisation to access the tables in the query on both servers. If not, you can set up the linked server to always use a specific set of credentials by using the sp_addlinkedsrvlogin system stored procedure. See http://msdn.microsoft.com/en-us/library/ms189811.aspx for further details.
I you are using sql server
than use OpEnQuery or OPENROWSET will resolved your issue.
step 1:
EXEC sp_addlinkedserver
#server = 'TEST',
#srvproduct = 'SQLServer OLEDB Provider',
#provider = 'SQLOLEDB',
#datasrc = 'InfoNet'
step 2 :
For Example :
SELECT loc.OrderID, loc.ProductID, rmt.ProductName
FROM [Order Details] loc INNER JOIN
OPENQUERY(InfoNet, 'SELECT * FROM Northwind.dbo.Products') rmt
ON loc.ProductID = rmt.ProductID