Copy/Paste "design" information of Micrsoft SQL Server tables into Excel - sql-server

Im kinda new to SQL so I'm sorry for a question that might sound trivial to some of you.
Is there any "clever" way to copy paste/information of SQL tables ("design" option) into Excel?
Example of table information
In the example I've selected one particular table; I can do my ordinary copy/paste of the three columns into Excel, and so on for other tables. The problem is that I have to do this for hundreds of tables. How can I automate this? Thank you!

You could leverage some of the system tables to help build this information. But you need more than 3 columns of data. I will leave it to you to determine how you want to display all this information. This query will give you all the tables and columns along with their nullability and datatype.
select TableName = t.name
, ColumnName = c.name
, DatatypeName = st.name
, MaxLength = case when st.name in ('nchar', 'nvarchar') then c.max_length / 2 else c.max_length end
, c.precision
, c.scale
, c.is_nullable
from sys.columns c
join sys.tables t on t.object_id = c.object_id
join sys.types st on st.user_type_id = c.user_type_id
order by t.name
, c.column_id

Related

SQL Server query to find tables across all databases, that references a column in a table?

I want to find the tables from all the databases that references a column in a table in one of the database. Can anyone help me here?
Not sure how to proceed
this should do, please see that i have used like operator (where col.name like '%COLUMN_NAME_HERE%'), so it will find matching patterns, if you want to find exact match then please use (where col.name = 'COLUMN_NAME_HERE')
select
schema_name(tab.schema_id) as schema_name
,tab.name as table_name
, col.column_id,col.name as column_name
, t.name as data_type, col.max_length, col.precision
from sys.tables as tab
inner join sys.columns as col on tab.object_id = col.object_id
left join sys.types as t on col.user_type_id = t.user_type_id
where col.name like '%COLUMN_NAME_HERE%'
order by schema_name,table_name, column_id*

I want to find column name in my database - is it possible?

My database name is CARE_DynamicsAX and I want to find a column name workerStatus
select * from INFORMATION_SCHEMA.COLUMNS
where COLUMN_NAME = 'workerStatus'
If I'm not wrong, you are trying to find the Table where you have a column name as workerStatus. If that is the case, you might run this query to find the same.
This works for the column names from TABLES for SQL Server.
This query would run under the assumption that you know that the column name that you are searching starts with workerStat
SELECT c.name AS ColName, t.name AS TableName
FROM sys.columns c
JOIN sys.tables t ON c.object_id = t.object_id
WHERE c.name LIKE 'workerStat%'

How to compare the columns of two tables, SQL Server?

I understand creating a join and comparing the values of specific columns from two tables. In this case, I am only interested in comparing the columns between two different tables, not the values.
I want to check to see if I have the correct number of columns in my new table, and that the spelling of each column in my new table matches the other. Essentially, a way to QC the schema of the new table.
Any suggestions for a SQL query to execute this?
You can utilize the table below:
SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_CATALOG = SOME DB
AND TABLE_NAME = SOME TBL
Or you can use sys.columns similarly.
You want to check whether the Data Definition is correct. You cannot do so using a Data Query on those tables.
However, SQL Server stores the information about the tables in the database in other tables. See: https://msdn.microsoft.com/en-us/library/ms186778.aspx
In this case you might get what you want using a query on the INFORMATION_SCHEMA.COLUMNS table.
;WITH cteTable1Columns AS (
SELECT
c.object_id
,c.name
FROM
sys.columns c
WHERE
c.object_id = object_id('A')
)
, cteTable2Columns AS (
SELECT
c.object_id
,c.name
FROM
sys.columns c
WHERE
c.object_id = object_id('B')
)
SELECT
OBJECT_NAME(c1.object_id) as Table1Name
,c1.name as Table1ColName
,OBJECT_NAME(c2.object_id) as Table2name
,c2.name as Table2ColName
FROM
cteTable1Columns c1
FULL JOIN cteTable2Columns c2
ON c1.name = c2.name
WHERE
c1.object_id is NULL
OR c2.object_id IS NULL
If you use a method like this to identify the columns in each table then do a full outer join on name it will show you all of your problems if any exist. (note you can also use INFORMATION_SCHEMA.COLUMNS)

SQL Server Schema Output

How do I output the schema of my database? I want it to output the design of the database.
Something like this might work:
SELECT TABLE_TYPE, TABLE_NAME, COLUMN_NAME, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH
FROM INFORMATION_SCHEMA.COLUMNS
ORDER BY TABLE_TYPE, TABLE_NAME, COLUMN_NAME
But I can't get it to run correctly. A excel file with table names, their columns, types, primary keys, etc. is what I want.
SELECT * FROM INFORMATION_SCHEMA.TABLES;
... sounds like what you are looking for, sans any formatting you want to do.
select
t.type_desc,
t.name as [table],
c.name as [column],
y.name,
c.max_length
from sys.tables t inner join
sys.columns c on c.object_id = t.object_id inner join
sys.types y on c.system_type_id = y.system_type_id
where y.name <> 'sysname'
order by
t.type_desc,
t.name,
c.name
Below posts would be useful for generating data dictionary
Database Documentation - http://deepakrangarajan.blogspot.com/2011/03/database-documentation.html
Generating a Database Data Dictionary - http://sqlserverdiaries.com/blog/index.php/2011/02/generating-a-database-data-dictionary/

How do I get a list of tables affected by a set of stored procedures?

I have a huge database with some 100 tables and some 250 stored procedures. I want to know the list of tables affected by a subset of stored procedures. For example, I have a list of 50 stored procedures, out of 250, and I want to know the list of tables that will be affected by these 50 stored procedures. Is there any easy way for doing this, other than reading all the stored procedures and finding the list of tables manually?
PS: I am using SQL Server 2000 and SQL Server 2005 clients for this.
This would be your SQL Server query:
SELECT
[NAME]
FROM
sysobjects
WHERE
xType = 'U' AND --specifies a user table object
id in
(
SELECT
sd.depid
FROM
sysobjects so,
sysdepends sd
WHERE
so.name = 'NameOfStoredProcedure' AND
sd.id = so.id
)
Hope this helps someone.
sp_depends 'StoredProcName' will return the object name and object type that the stored proc depends on.
EDIT: I like #KG's answer better. More flexible IMHO.
I'd do it this way in SQL 2005 (uncomment the "AND" line if you only want it for a particular proc):
SELECT
[Proc] = SCHEMA_NAME(p.schema_id) + '.' + p.name,
[Table] = SCHEMA_NAME(t.schema_id) + '.' + t.name,
[Column] = c.name,
d.is_selected,
d.is_updated
FROM sys.procedures p
INNER JOIN sys.sql_dependencies d
ON d.object_id = p.object_id
AND d.class IN (0,1)
INNER JOIN sys.tables t
ON t.object_id = d.referenced_major_id
INNER JOIN sys.columns c
ON c.object_id = t.object_id
AND c.column_id = d.referenced_minor_id
WHERE p.type IN ('P')
-- AND p.object_id = OBJECT_ID('MyProc')
ORDER BY
1, 2, 3
One very invasive option would be to get a duplicate database and set a trigger on every table that logs that something happened. Then run all the SP's. If you can't do lots of mods to the DB that wont work
Also, be sure to add the logging to existing triggers rather than replace them with logging if you also want tables that the SP's effect via triggers.

Resources