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
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 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 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.
I have a Windows Service that requires execute permissions on a SQL Server 2005 DB. On start up I check to see if I can connect to the DB and stop the service if I can't. I also want to do a check to see if I can execute a stored procedure using that connection. Is there a way to do that without actually attempting to execute a sproc and looking at the exception if one occurs?
SQL 2005 and on you can check any permission with HAS_PERM_BY_NAME:
SELECT HAS_PERMS_BY_NAME('sp_foo', 'OBJECT', 'EXECUTE');
you could run a query like this:
SELECT
o.NAME,COALESCE(p.state_desc,'?permission_command?')+' '+COALESCE(p.permission_name,'?permission_name?')+' ON ['+SCHEMA_NAME(o.schema_id)+'].['+COALESCE(o.Name,'?object_name?')+'] TO ['+COALESCE(dp.Name,'?principal_name?')+']' COLLATE SQL_Latin1_General_CP1_CI_AS AS GrantCommand
FROM sys.all_objects o
INNER JOIN sys.database_permissions p ON o.OBJECT_ID=p.major_id
LEFT OUTER JOIN sys.database_principals dp ON p.grantee_principal_id = dp.principal_id
where p.state_desc='GRANT' AND p.permission_name='EXECUTE'
AND o.NAME='YourProcedureName'
AND dp.Name='YourSecurityName'
...and remove the fancy formatting of the grant command, it is there only for reference
these are nice too...
SELECT * FROM fn_my_permissions('YourTable', 'OBJECT')
SELECT * FROM fn_my_permissions('YourProcedure', 'OBJECT')
SELECT * FROM fn_my_permissions (NULL, 'DATABASE')
SELECT * FROM fn_my_permissions(NULL, 'SERVER')
To see what permissions someone else has you can do this:
EXECUTE AS user = 'loginToTest'
GO
PRINT 'SELECT permissions on tables:'
SELECT
HAS_PERMS_BY_NAME( QUOTENAME(SCHEMA_NAME(schema_id))+'.' + QUOTENAME(name)
,'OBJECT','SELECT'
) AS have_select
, *
FROM sys.tables;
PRINT 'EXECUTE permissions on stored procedures:'
SELECT
HAS_PERMS_BY_NAME( QUOTENAME(SCHEMA_NAME(schema_id)) + '.' + QUOTENAME(name)
,'OBJECT', 'EXECUTE') AS have_execute
, *
FROM sys.procedures;
GO
REVERT;
GO
The first part of this answer shows how you can check rights in T-SQL, the second part gives an example how to use this in the Entity Framework (note that there are differences between the EF versions - the example given is EF 4, but can be changed easily to a newer version):
First part (SQL):
I am using the following T-SQL script to check permissions. It first checks if you have any rights, then it checks execute permissions for the SPs, finally select permissions for the tables. See this link to get more information.
-- 1. Do I have any permissions?
SELECT HAS_PERMS_BY_NAME(db_name(), 'DATABASE', 'ANY') DoIHaveAnyRights;
-- 2. create list of schemas
declare #SchemaList table (schema_id int, name nvarchar(max));
PRINT 'Schemas regarded:'
insert into #SchemaList
select distinct schema_id, name FROM sys.schemas
where name in ('dbo') -- enter the schemas you like to check comma-separated
SELECT s.name, s.schema_id FROM sys.schemas s
join #SchemaList sl on s.schema_id=sl.schema_id
-- 3. check execute permissions
PRINT 'EXECUTE permissions on stored procedures:'
SELECT
HAS_PERMS_BY_NAME(QUOTENAME(SCHEMA_NAME(t.schema_id)) + '.' + QUOTENAME(t.name)
,'OBJECT', 'EXECUTE') AS [have execute]
,'['+s.name +'].['+ t.name+']' as [object]
--, *
FROM sys.procedures t
join #SchemaList s on t.schema_id=s.schema_id
order by s.name, t.name;
-- 4. check select permissions
PRINT 'SELECT permissions on tables:'
SELECT
HAS_PERMS_BY_NAME(QUOTENAME(SCHEMA_NAME(t.schema_id))+'.' + QUOTENAME(t.name)
,'OBJECT','SELECT') AS [have select]
,'['+s.name +'].['+ t.name+']' as [object]
--, *
FROM sys.tables t
join #SchemaList s on t.schema_id=s.schema_id
order by s.name, t.name;
With the Northwind database for example, this gives you the following result:
Note that you can configure the schemas regarded in step 2. If you don't need a limited set of schemas checked, you can just comment out the where clause in the insert into #SchemaList statement to get all schemas.
Second part (Entity Framework):
In this section, I'd like to show you, how you can get the results into Entity Framework.
Let's suppose you want to check if you have any rights before using any of the tables in your LINQ queries. Take a look at this example (for simplicity, I've done it in LinqPad, please add System.Data.Entity.dll and its namespaces via F4 before you run it):
void Main()
{
var dc=this;
var sql="SELECT TOP 1 "
+ "HAS_PERMS_BY_NAME(db_name(), 'DATABASE', 'ANY') DoIHaveAnyRights;";
var result=dc.ExecuteStoreQuery<Rights>(sql);
if (result1.DoIHaveAnyRights==1)
{
Console.WriteLine("OK"); // ok
}
else
{
// no rights: Show error etc.
Console.WriteLine("No rights"); // ok
}
}
public class Rights
{
public Int32 DoIHaveAnyRights { get; set; }
}
Likewise you can use the query from the first part of my answer, e.g.:
var sql="select top 1 case when cnt.NoRightsCount=0 then 1 else 0 end "
+"as DoIHaveAnyRights "
+"from (SELECT count(1) NoRightsCount FROM sys.procedures t "
+"where HAS_PERMS_BY_NAME("
+"QUOTENAME(SCHEMA_NAME(t.schema_id)) + '.' + QUOTENAME(t.name)"
+",'OBJECT', 'EXECUTE')<>1) cnt";
This query will check if there are any stored procedures in your database for which you have no rights to execute - in this case the return value would be result1.DoIHaveAnyRights!=1.
I think you get the idea, play around with the possibilities: Keep in mind that EF requires access to all the database tables, stored procedures etc. you're mapping to - you could use the code above to check before you access them. Unfortunately, there is currently no easier way to do this.
You would have to have rights to access the DataDictionary for your database and run a query against it to determine which permissions the account you log in with has. This would vary from database to database.