Find schema name for a given database? - sql-server

How do I determine the schema name for a given database "MyDB", listing all tables contained in the database (in MS SQL Express)? Is it "MyDB.Security.Schemas/INFORMATION_SCHEMA"?
I am using EF Core with Blazor, code first.
Background: I want to determine whether a certain table is present in the database.

You can select all the tables with a given and known schema from one database with this query:
SELECT [schemas].[schema_id] AS SchemaId,
[schemas].[name] AS SchemaName,
[tables].[name] AS TableName
FROM sys.schemas AS [schemas]
INNER JOIN sys.tables AS [tables]
ON [schemas].[schema_id] = [tables].[schema_id]
WHERE [schemas].[name] = 'your-schema-name'
If you like to select over all databases on the server you can have a look at this SO answer:
How do I list all tables in all databases in SQL Server in a single result set?

Related

Is there a way to identify if a column is not been used in a SQL Server Database?

I have a big database and a lot tables and I would like to identify what columns are not been called by any store procedure or any query, or not in use.
I'm not sure if this is what you're looking for, but in your DB under views there's a folder for System Views - three of which are the following. Looking in all_objects look for the table name, then use the object_id of that table to select from the other queries. There may be other meta-data here that is appropriate for your need.
SELECT *
FROM sys.all_objects
SELECT *
FROM sys.all_columns
WHERE object_id = 981578535
SELECT *
FROM sys.all_views
WHERE object_id = 981578535
After an investigation I have found a new feature of SQL Server called Query Store where you can find in disk the executions with other information. If you have a SQL Server 2016 instance you can find it in the properties of the data base. There you can change the amount of days to capture and then you can in theory try to find the columns in question. The idea of this technology is to give you more options for performance tuning.
You can find the information with this query :
SELECT TOP 10 qt.query_sql_text, q.query_id,
qt.query_text_id, p.plan_id, rs.last_execution_time
FROM sys.query_store_query_text AS qt
JOIN sys.query_store_query AS q
ON qt.query_text_id = q.query_text_id
JOIN sys.query_store_plan AS p
ON q.query_id = p.query_id
JOIN sys.query_store_runtime_stats AS rs
ON p.plan_id = rs.plan_id
where qt.query_sql_text LIKE '%ColumnToFind%'
ORDER BY rs.last_execution_time;
Credits: Query Store

How can we query data from two different databases and compare the results using SQL Server?

Suppose I have two databases located on the same server. How can we write the query part in sql server to extract data from two different databases located on the same server.
Use 3 part naming: [DatabaseName].[SchemaName].[TableName]
select
t1.*,
t2.*
from [MyDatabase].[dbo].[MyTable] t1
join [MyOtherDatabase].[dbo].[MyOtherTable] t2 on t1.SomeColumn = t2.SomeColumn
Fully qualify the table with DB_NAME.Schema_name.table_name. For example, if you have database as DB1 and DB2 with default schema as dbo and table names tab1. then you can differentiate between them saying
select * from DB1.dbo.tab1
OR
select * from DB2.dbo.tab1

Any system table or view containing the information of all databases, schemas, tables and columns in SQL Server 2008?

I'm trying to find a system table or make some joins to get a table in which I can find all the columns of all the tables of all the schemas of all the databases of my server.
I'm doing this for academic purposes, so I can't use any stored procedure or function, I have to create it myself.
The point is to create a store procedure or function that receives a database name, a schema name and a table name as input parameters and throws a list of all the columns contained, but If I don't receive a table name, I should list all the columns of all the tables contained in the given database and schema. If I also don't receive a schema name as input parameter I should list all columns of all tables of all schemas in the given database.
Any ideas? As I say, I cannot use any stored procedure or function to do this.
This is what you need for all the columns of all the tables in the current database accessable by currect user:
http://msdn.microsoft.com/en-us/library/ms188348.aspx
To find the same from all databases, you have to connect to each of the databases and then access the system view.
If you connect as the dba user, you will have access to all tables in all schemas.
There are tables in the sys schema which may be of interest. sys.databases, sys.schemas, sys.tables, sys.columns all of which can be easily joined together and filtered to your needs.
sp_msforeachdb 'SELECT t.name FROM [?].sys.schemas s
INNER JOIN [?].sys.tables t ON s.[schema_id] = t.[schema_id]
INNER JOIN [?].sys.columns c ON c.[object_id] = t.[object_id]';

How to get a list of all tables in two different databases

I'm trying to create a little SQL script (in SQL Server Management Studio) to get a list of all tables in two different databases. The goal is to find out which tables exist in both databases and which ones only exist in one of them.
I have found various scripts on SO to list all the tables of one database, but so far I wasn't able to get a list of tables of multiple databases.
So: is there a way to query SQL Server for all tables in a specific database, e.g. SELECT * FROM ... WHERE databaseName='first_db' so that I can join this with the result for another database?
SELECT * FROM database1.INFORMATION_SCHEMA.TABLES
UNION ALL
SELECT * FROM database2.INFORMATION_SCHEMA.TABLES
UPDATE
In order to compare the two lists, you can use FULL OUTER JOIN, which will show you the tables that are present in both databases as well as those that are only present in one of them:
SELECT *
FROM database1.INFORMATION_SCHEMA.TABLES db1
FULL JOIN database2.INFORMATION_SCHEMA.TABLES db2
ON db1.TABLE_NAME = db2.TABLE_NAME
ORDER BY COALESCE(db1.TABLE_NAME, db2.TABLE_NAME)
You can also add WHERE db1.TABLE_NAME IS NULL OR db2.TABLE_NAME IS NULL to see only the differences between the databases.
As far as I know, you can only query tables for the active database. But you could store them in a temporary table, and join the result:
use db1
insert #TableList select (...) from sys.tables
use db2
insert #TableList2 select (...) from sys.tables
select * from #TableList tl1 join Tablelist2 tl2 on ...
Just for completeness, this is the query I finally used (based on Andriy M's answer):
SELECT * FROM DB1.INFORMATION_SCHEMA.Tables db1
LEFT OUTER JOIN DB2.INFORMATION_SCHEMA.Tables db2
ON db1.TABLE_NAME = db2.TABLE_NAME
ORDER BY db1.TABLE_NAME
To find out which tables exist in db2, but not in db1, replace the LEFT OUTER JOIN with a RIGHT OUTER JOIN.

sql server: looking for table usage through out database

How would I figure out what type of sql code such as procs, functions, views etc. are interacting with my table called TABLE1 through out a given database. Sample code would be very helpful for me.
thanks
select so.name, so.xtype
from sysobjects so (nolock)
inner join syscomments sc (nolock) on sc.id = so.id
where sc.text like '%tablename%'
This code will search all SQL Server objects for a reference to your table. You have to run this query for each database.
If a stored procedure uses your table it will appear in this query. The same is true of functions, views, and triggers.
xtype tells you the type of object.
Here are the possible xtype values:
D = Field names
F = Foreign Key
FN = Function
P = Stored Procedures
PK = Primary Key
S = System Tables
U = User tables
V = Hidden tables
Not enough info in your question, but one thing you can do is use SQL Profiler to profile where INSERTs, UPDATEs, and DELETEs are coming from.
I assume you are talking about how an app is interacting with data and what name (of say a sproc) is doing the insert / update / delete.
Look at SQL Profiler, it comes with your client tools install. Filter it to only show connections to your database (either db name or ID).
If you've been good and created your SPs/views/functions after your table was created, sp_depends will tell you evertyhing referencing the table. Exept for dynamic sql that is.

Resources