I am looking to generate scripts like:
ALTER TABLE dbo.Person
ADD CONSTRAINT PK_Person PRIMARY KEY CLUSTERED (PersonID);
CREATE INDEX IX_EVT_EVENTS on EVT_EVENTS(prg_event_t1, prg_event_t2, num_order)
The main problem I cannot find all information that I need, for example this query:
SELECT t.name TableName, I.*
FROM sys.indexes I
Inner Join sys.tables T on i.object_id=t.object_id
returns information about all keys and indexes, but.. there is no "Columns' Name" where can I get them?
I tried sys.all_columns and sys.key_constraints also.
Column names are stored in the sys.columns andsys.index_columns tables.
This will select the column names from all tables:
SELECT sc.name
FROM sys.tables st
INNER JOIN sys.columns sc ON sc.OBJECT_ID = st.OBJECT_ID
WHERE st.OBJECT_ID = 'ObjectID'
Or to join with the sys.indexes table, this will select the table name and the columns:
SELECT st.name ,
sc.name
FROM sys.indexes si
INNER JOIN sys.tables st ON si.object_id = st.object_id
INNER JOIN sys.columns sc ON sc.OBJECT_ID = st.OBJECT_ID
You can add a WHERE clause into the statement to filter on your index column
SELECT si.name ,
st.name ,
sc.name
FROM sys.indexes si
INNER JOIN sys.tables st ON si.object_id = st.object_id
INNER JOIN sys.columns sc ON sc.OBJECT_ID = st.OBJECT_ID
WHERE si.name = 'IX_EVT_EVENTS'
I have tried many scripts in web. All of them had flaws. I finally wrote my script using a post in msdn site and changed it to work on many kind of situations.
Note: This scripts works on SQL Server 2012 and later
WITH IndexInfo AS (
SELECT ix.object_id
, ix.NAME AS IndexName
, ix.type_desc
, ix.filter_definition
, ix.is_unique
, ix.is_primary_key
, ix.allow_row_locks
, ix.allow_page_locks
, ds.NAME AS DataSpaceName
, ds.type AS DataSpaceType
, ix.is_padded
, object_schema_name(ix.object_id) AS SchemaName
, object_name(ix.object_id) AS TableName
, IIF(ix.type <= 2, is_included_column, 0) AS HasIncludedColumn
, IIF(ix.type in (5, 6), 1, 0) AS IsColumnStore
, (
SELECT KeyColumns
FROM (
SELECT IC2.object_id
, IC2.index_id
, STUFF((
SELECT ' , ' + C.NAME + IIF(MAX(CONVERT(INT, IC1.is_descending_key)) = 1 AND ix.type <= 2, ' DESC ', ' ')
FROM sys.index_columns IC1
JOIN sys.columns C ON C.object_id = IC1.object_id
AND C.column_id = IC1.column_id
--AND IC1.is_included_column = 0
AND IIF(ix.type <= 2, IC1.is_included_column, 0) = 0
AND IIF(ix.type <= 2, IC1.key_ordinal, ic.index_column_id) > 0
WHERE IC1.object_id = IC2.object_id
AND IC1.index_id = IC2.index_id
GROUP BY IC1.object_id
, C.NAME
, index_id
, IC1.key_ordinal
, IC1.index_column_id
ORDER BY IIF(ix.type <= 2, IC1.key_ordinal, IC1.index_column_id)
FOR XML PATH('')
), 1, 2, '') KeyColumns
FROM sys.index_columns IC2
GROUP BY IC2.object_id
, IC2.index_id
) tmp
WHERE tmp.object_id = ix.object_id
AND tmp.index_id = ix.index_id
) AS KeyColumnsStr
, (
SELECT IncludedColumns
FROM (
SELECT IC2.object_id, IC2.index_id
, STUFF((
SELECT ' , ' + C.NAME
FROM sys.index_columns IC1
JOIN sys.columns C ON C.object_id = IC1.object_id
AND C.column_id = IC1.column_id
--AND IC1.is_included_column = 1
AND IIF(ix.type <= 2, IC1.is_included_column, 0) <> 0
WHERE IC1.object_id = IC2.object_id
AND IC1.index_id = IC2.index_id
--and IIF(ix.type <= 2, IC1.key_ordinal, ic.index_column_id) > 0
GROUP BY IC1.object_id, C.NAME, index_id, IC1.key_ordinal, IC1.index_column_id
ORDER BY IIF(ix.type <= 2, IC1.key_ordinal, IC1.index_column_id)
FOR XML PATH('')
), 1, 2, '') IncludedColumns
FROM sys.index_columns IC2
GROUP BY IC2.object_id, IC2.index_id
) tmp
WHERE tmp.object_id = ix.object_id
AND tmp.index_id = ix.index_id
AND IncludedColumns IS NOT NULL
) AS IncludedColumnsStr
FROM sys.indexes ix
INNER JOIN sys.index_columns ic ON ic.index_id = ix.index_id AND ic.object_id = ix.object_id
INNER JOIN sys.columns col ON col.column_id = ic.column_id AND col.object_id = ix.object_id
INNER JOIN sys.data_spaces ds ON ix.data_space_id = ds.data_space_id
WHERE ix.NAME IS NOT NULL AND IIF(ix.type <= 2, ic.key_ordinal, ic.index_column_id) > 0
)
, Scrpt AS (
SELECT *
, 'CREATE '
+ CASE WHEN is_unique = 1 THEN ' UNIQUE ' ELSE '' END
+ type_desc COLLATE DATABASE_DEFAULT + ' INDEX ' + IndexName + ' ON '
+ SchemaName + '.' + TableName + '(' + KeyColumnsStr + ')'
+ ISNULL(IIF(HasIncludedColumn > 0, ' ', ' INCLUDE (' + IncludedColumnsStr + ')'), '')
+ IIF(filter_definition IS NULL, ' ', ' WHERE ' + filter_definition)
+ ' WITH ('
+ CASE WHEN IsColumnStore = 1 THEN ' DROP_EXISTING = OFF '
ELSE
IIF(is_padded = 1, ' PAD_INDEX = ON ', ' PAD_INDEX = OFF ') + ','
+ ' DROP_EXISTING = OFF ' + ','
+ ' ONLINE = OFF ' + ','
+ IIF(allow_row_locks = 1, ' ALLOW_ROW_LOCKS = ON ', ' ALLOW_ROW_LOCKS = OFF ') + ','
+ IIF(allow_page_locks = 1, ' ALLOW_PAGE_LOCKS = ON ', ' ALLOW_PAGE_LOCKS = OFF ')
END
+ ' ) '
+ IIF(DataSpaceType = 'FG','ON [' + DataSpaceName + ']', '') AS CreateIndexScript
FROM IndexInfo
)
SELECT DISTINCT IndexName, CreateIndexScript
FROM Scrpt
WHERE object_id = object_id('dbo.SWPartition')
As a credit to original author, I mentions the original link here
How to Generate Index Creation Scripts
Related
For example;
I have 500 tables in database. One table is named "Table1" and another "Table234". I need to join "Table1" and "Table234" but I can't join them directly, I need to use other tables. Is it possible to make query where I enter starting table (in this case Table1) and ending table (in this case Table234) and it finds all possible paths over keys from starting to ending table.
Let's say that I have two possible paths:
Table1 -> Table 23 -> Table456 -> Table234
Table1 -> Table 34 -> Table23 -> Zable 456 -> Table234
Select * from table1
join table23 on XX = XX
join table456 on XX = XX
join table234 on XX = XX
Maybe something like BFS to find shortest path, and than save it somewhere and find BFS without that path?
Solution (#Geoff Patterson):
IF OBJECT_ID('tempdb..#paths') IS NOT NULL
DROP TABLE #paths
IF OBJECT_ID('tempdb..#shortestPaths') IS NOT NULL
DROP TABLE #shortestPaths
GO
DECLARE #targetObjectName SYSNAME = 'YourTableNameHere'
;WITH singleColumnFkColumns AS (
SELECT fk1.*
FROM sys.foreign_key_columns fk1
LEFT JOIN sys.foreign_key_columns fk2 ON fk2.constraint_object_id = fk1.constraint_object_id AND fk2.constraint_column_id = 2
WHERE fk1.constraint_column_id = 1
AND fk2.constraint_object_id IS NULL
)
, parentCTE AS (
SELECT
p.object_id AS ParentId
,OBJECT_SCHEMA_NAME(p.object_id) + '.' + p.name AS ParentTable
,pc.column_id AS ParentColumnId
,pc.name AS ParentColumn
,r.object_id AS ChildId
,OBJECT_SCHEMA_NAME(r.object_id) + '.' + r.name AS ChildTable
,rc.column_id AS ChildColumnId
,rc.name AS ChildColumn
,1 AS depth
, ',' + CONVERT(VARCHAR(MAX), p.object_id) + '_NULL_' + CONVERT(VARCHAR(MAX), pc.column_id) +
',' + CONVERT(VARCHAR(MAX), r.object_id) + '_' + CONVERT(VARCHAR(MAX), pc.column_id) + '_' + CONVERT(VARCHAR(MAX), rc.column_id) AS TraversalPath
FROM sys.foreign_key_columns fk
JOIN sys.columns pc ON pc.object_id = fk.parent_object_id AND pc.column_id = fk.parent_column_id
JOIN sys.columns rc ON rc.object_id = fk.referenced_object_id AND rc.column_id = fk.referenced_column_id
JOIN sys.tables p ON p.object_id = fk.parent_object_id
JOIN sys.tables r ON r.object_id = fk.referenced_object_id
WHERE fk.parent_object_id = OBJECT_ID(#targetObjectName)
AND p.object_id <> r.object_id
UNION ALL
SELECT
p.object_id AS ParentId
,OBJECT_SCHEMA_NAME(p.object_id) + '.' + p.name AS ParentTable
,pc.column_id AS ParentColumnId
,pc.name AS ParentColumn
,r.object_id AS ChildId
,OBJECT_SCHEMA_NAME(r.object_id) + '.' + r.name AS ChildTable
,rc.column_id AS ChildColumnId
,rc.name AS ChildColumn
,cte.depth + 1 AS depth
,cte.TraversalPath + ',' + CONVERT(VARCHAR(MAX), r.object_id) + '_' + CONVERT(VARCHAR(MAX), pc.column_id) + '_' + CONVERT(VARCHAR(MAX), rc.column_id) AS TraversalPath
FROM parentCTE cte
JOIN singleColumnFkColumns fk
ON fk.parent_object_id = cte.ChildId
JOIN sys.columns pc ON pc.object_id = fk.parent_object_id AND pc.column_id = fk.parent_column_id
JOIN sys.columns rc ON rc.object_id = fk.referenced_object_id AND rc.column_id = fk.referenced_column_id
JOIN sys.tables p ON p.object_id = fk.parent_object_id
JOIN sys.tables r ON r.object_id = fk.referenced_object_id
WHERE p.object_id <> r.object_id
AND cte.TraversalPath NOT LIKE ('%_' + CONVERT(VARCHAR(MAX), r.object_id) + '%')
)
SELECT *
INTO #paths
FROM parentCTE
ORDER BY depth, ParentTable, ChildTable
GO
SELECT *
INTO #shortestPaths
FROM (
SELECT *, ROW_NUMBER() OVER (PARTITION BY ChildTable ORDER BY depth ASC) AS rankToThisChild
FROM #paths
) x
WHERE rankToThisChild = 1
ORDER BY ChildTable
GO
WITH joinCTE AS (
SELECT p.ChildTable
, p.TraversalPath AS ParentTraversalPath
, NULL AS depth
, CONVERT(VARCHAR(MAX), 'FROM ' + p.ChildTable + ' t' + CONVERT(VARCHAR(MAX), p.depth+1)) AS JoinString
FROM #shortestPaths p
UNION ALL
SELECT cte.ChildTable
, REPLACE(p.TraversalPath, ',' + CONVERT(VARCHAR, p.ChildId) + '_' + CONVERT(VARCHAR, p.ParentColumnId)+ '_' + CONVERT(VARCHAR, p.ChildColumnId), '') AS TraversalPath
, p.depth
, cte.JoinString + '
' + CONVERT(VARCHAR(MAX), 'JOIN ' + p.ParentTable + ' t' + CONVERT(VARCHAR(MAX), p.depth) + ' ON t' + CONVERT(VARCHAR(MAX), p.depth) + '.' + p.ParentColumn + ' = t' + CONVERT(VARCHAR(MAX), p.depth+1) + '.' + p.ChildColumn) AS JoinString
FROM joinCTE cte
JOIN #paths p
ON p.TraversalPath = cte.ParentTraversalPath
)
SELECT ChildTable, 'SELECT TOP 100 *
' +JoinString
FROM joinCTE
WHERE depth = 1
ORDER BY ChildTable
GO
You can replace "YourTableNameHere" in script and it will find You all links to from any to that table.
And in my JAVA software looks like:
RS = CALIzb.main(Conn, "IF OBJECT_ID('tempdb..#paths') IS NOT NULL DROP TABLE #paths IF OBJECT_ID('tempdb..#shortestPaths') IS NOT NULL DROP TABLE #shortestPaths DECLARE #targetObjectName SYSNAME = '"+txtTablica.getText().trim()+"' ;WITH singleColumnFkColumns AS ( SELECT fk1.* FROM sys.foreign_key_columns fk1 LEFT JOIN sys.foreign_key_columns fk2 ON fk2.constraint_object_id = fk1.constraint_object_id AND fk2.constraint_column_id = 2 WHERE fk1.constraint_column_id = 1 AND fk2.constraint_object_id IS NULL ), parentCTE AS ( SELECT p.object_id AS ParentId ,OBJECT_SCHEMA_NAME(p.object_id) + '.' + p.name AS ParentTable,pc.column_id AS ParentColumnId,pc.name AS ParentColumn,r.object_id AS ChildId,OBJECT_SCHEMA_NAME(r.object_id) + '.' + r.name AS ChildTable,rc.column_id AS ChildColumnId,rc.name AS ChildColumn,1 AS depth, ',' + CONVERT(VARCHAR(MAX), p.object_id) + '_NULL_' + CONVERT(VARCHAR(MAX), pc.column_id) +',' + CONVERT(VARCHAR(MAX), r.object_id) + '_' + CONVERT(VARCHAR(MAX), pc.column_id) + '_' + CONVERT(VARCHAR(MAX), rc.column_id) AS TraversalPath FROM sys.foreign_key_columns fk JOIN sys.columns pc ON pc.object_id = fk.parent_object_id AND pc.column_id = fk.parent_column_id JOIN sys.columns rc ON rc.object_id = fk.referenced_object_id AND rc.column_id = fk.referenced_column_id JOIN sys.tables p ON p.object_id = fk.parent_object_id JOIN sys.tables r ON r.object_id = fk.referenced_object_id WHERE fk.parent_object_id = OBJECT_ID(#targetObjectName) AND p.object_id <> r.object_id UNION ALL SELECT p.object_id AS ParentId,OBJECT_SCHEMA_NAME(p.object_id) + '.' + p.name AS ParentTable,pc.column_id AS ParentColumnId,pc.name AS ParentColumn,r.object_id AS ChildId,OBJECT_SCHEMA_NAME(r.object_id) + '.' + r.name AS ChildTable,rc.column_id AS ChildColumnId,rc.name AS ChildColumn,cte.depth + 1 AS depth,cte.TraversalPath + ',' + CONVERT(VARCHAR(MAX), r.object_id) + '_' + CONVERT(VARCHAR(MAX), pc.column_id) + '_' + CONVERT(VARCHAR(MAX), rc.column_id) AS TraversalPath FROM parentCTE cte JOIN singleColumnFkColumns fk ON fk.parent_object_id = cte.ChildId JOIN sys.columns pc ON pc.object_id = fk.parent_object_id AND pc.column_id = fk.parent_column_id JOIN sys.columns rc ON rc.object_id = fk.referenced_object_id AND rc.column_id = fk.referenced_column_id JOIN sys.tables p ON p.object_id = fk.parent_object_id JOIN sys.tables r ON r.object_id = fk.referenced_object_id WHERE p.object_id <> r.object_id AND cte.TraversalPath NOT LIKE ('%_' + CONVERT(VARCHAR(MAX), r.object_id) + '%')) SELECT * INTO #paths FROM parentCTE ORDER BY depth, ParentTable, ChildTable SELECT * INTO #shortestPaths FROM ( SELECT *, ROW_NUMBER() OVER (PARTITION BY ChildTable ORDER BY depth ASC) AS rankToThisChild FROM #paths) x WHERE rankToThisChild = 1 ORDER BY ChildTable");
RS = CALIzb.main(Conn, "WITH joinCTE AS ( SELECT p.ChildTable, p.TraversalPath AS ParentTraversalPath, NULL AS depth, CONVERT(VARCHAR(MAX), 'FROM ' + p.ChildTable + ' t' + CONVERT(VARCHAR(MAX), p.depth+1)) AS JoinString FROM #shortestPaths p UNION ALL SELECT cte.ChildTable, REPLACE(p.TraversalPath, ',' + CONVERT(VARCHAR, p.ChildId) + '_' + CONVERT(VARCHAR, p.ParentColumnId)+ '_' + CONVERT(VARCHAR, p.ChildColumnId), '') AS TraversalPath, p.depth, cte.JoinString + ' ' + CONVERT(VARCHAR(MAX), 'JOIN ' + p.ParentTable + ' t' + CONVERT(VARCHAR(MAX), p.depth) + ' ON t' + CONVERT(VARCHAR(MAX), p.depth) + '.' + p.ParentColumn + ' = t' + CONVERT(VARCHAR(MAX), p.depth+1) + '.' + p.ChildColumn) AS JoinString FROM joinCTE cte JOIN #paths p ON p.TraversalPath = cte.ParentTraversalPath) SELECT ChildTable, 'SELECT TOP 100 * ' +JoinString FROM joinCTE WHERE depth = 1 ORDER BY ChildTable");
vezaKljuceva.setModel(DbUtils.resultSetToTableModel(RS));
SELECT Db_name(database_id) Database_Name
,o.NAME
,indexname = i.NAME
,i.index_id
,reads = user_seeks + user_scans + user_lookups
,writes = user_updates
,rows = (SELECT Sum(p.rows)
FROM sys.partitions p
WHERE p.index_id = s.index_id
AND s.object_id = p.object_id)
,CASE
WHEN s.user_updates < 1 THEN 100
ELSE 1.00 * ( s.user_seeks + s.user_scans + s.user_lookups ) /
s.user_updates
END AS reads_per_write
,'DROP INDEX ' + Quotename(i.NAME) + ' ON '
+ Quotename(c.NAME) + '.'
+ Quotename(Object_name(s.object_id)) AS 'drop statement'
FROM sys.dm_db_index_usage_stats s
INNER JOIN sys.indexes i
ON i.index_id = s.index_id
AND s.object_id = i.object_id
INNER JOIN sys.objects o
ON s.object_id = o.object_id
INNER JOIN sys.schemas c
ON o.schema_id = c.schema_id
WHERE Objectproperty(s.object_id, 'IsUserTable') = 1
AND s.database_id = Db_id()
AND i.type_desc = 'nonclustered'
AND i.is_primary_key = 0
AND i.is_unique_constraint = 0
AND (SELECT Sum(p.rows)
FROM sys.partitions p
WHERE p.index_id = s.index_id
AND s.object_id = p.object_id) > 10000
ORDER BY reads_per_write ASC
USE master
GO
DECLARE #sql VARCHAR(MAX) =
'
USE [?];
SELECT
DB_NAME(database_id) Database_Name, o.name
, indexname = i.name
, i.index_id
, reads = user_seeks + user_scans + user_lookups
, writes = user_updates
, rows = ( SELECT SUM(p.rows) FROM sys.partitions p WHERE p.index_id = s.index_id AND s.object_id = p.object_id )
, CASE WHEN s.user_updates < 1 THEN 100 ELSE 1.00 * ( s.user_seeks + s.user_scans + s.user_lookups ) / s.user_updates END AS reads_per_write
, ''DROP INDEX '' + QUOTENAME(i.name) + '' ON '' + QUOTENAME(c.name) + ''.'' + QUOTENAME(OBJECT_NAME(s.object_id)) AS ''drop statement''
FROM sys.dm_db_index_usage_stats s
INNER JOIN sys.indexes i ON i.index_id = s.index_id AND s.object_id = i.object_id
INNER JOIN sys.objects o ON s.object_id = o.object_id
INNER JOIN sys.schemas c ON o.schema_id = c.schema_id
WHERE OBJECTPROPERTY(s.object_id, ''IsUserTable'') = 1
AND s.database_id = DB_ID()
AND i.type_desc = ''nonclustered''
AND i.is_primary_key = 0
AND i.is_unique_constraint = 0
AND ( SELECT SUM(p.rows) FROM sys.partitions p WHERE p.index_id = s.index_id AND s.object_id = p.object_id ) > 10000 ORDER BY reads_per_write ASC'
EXEC sp_msforeachdb #sql
is there a way to create procedure that will automatically create one table with all structure (columns,constraints, PKs, FKs, indexes, etc.) based on 2 other tables?
For example:
Table a
id
a1
a2
Table b
id
b1
b2
b3
The procedure will receive 3 table names (a and b - input, c - output) and will create table c with columns id,a1,a2,b1,b2,b3, including all data - constraints, PKs, FKs, indexes, etc.
I've got more than 80 pairs of tables to create, so manually it'll take like forever...
UPDATE:
This script recreate DDL (columns, FK's, PK, non-clustered index's) from several tables in one. Possible it will be helpful for you -
DECLARE
#table1_name SYSNAME
, #table2_name SYSNAME
, #name_for_new_table SYSNAME
, #schema_for_new_table SYSNAME
SELECT
#table1_name = 'dbo.WorkOut'
, #table2_name = 'dbo.WorkPlace'
, #schema_for_new_table = 'dbo'
, #name_for_new_table = 'TableMerge'
DECLARE #SQL NVARCHAR(MAX) = ''
;WITH tbl AS
(
SELECT o.[object_id]
FROM sys.objects o WITH (NOWAIT)
WHERE SCHEMA_NAME(o.[schema_id]) + '.' + o.name IN (#table1_name, #table2_name)
AND o.[type] = 'U'
AND o.is_ms_shipped = 0
),
index_column AS
(
SELECT
ic.[object_id]
, ic.index_id
, ic.is_descending_key
, ic.is_included_column
, c.name
FROM sys.index_columns ic WITH (NOWAIT)
JOIN sys.columns c WITH (NOWAIT) ON ic.[object_id] = c.[object_id] AND ic.column_id = c.column_id
WHERE ic.[object_id] IN (SELECT t.[object_id] FROM tbl t)
),
fk_columns AS
(
SELECT
k.constraint_object_id
, cname = c.name
, rcname = rc.name
FROM sys.foreign_key_columns k WITH (NOWAIT)
JOIN sys.columns rc WITH (NOWAIT) ON rc.[object_id] = k.referenced_object_id AND rc.column_id = k.referenced_column_id
JOIN sys.columns c WITH (NOWAIT) ON c.[object_id] = k.parent_object_id AND c.column_id = k.parent_column_id
)
SELECT #SQL = 'CREATE TABLE [' + #schema_for_new_table + '].[' + #name_for_new_table + ']' + CHAR(13) + '(' + CHAR(13) + STUFF((
SELECT DISTINCT CHAR(9) + ', [' + c.name + '] ' + UPPER(t.name) +
CASE WHEN t.name IN ('varchar', 'char', 'varbinary', 'binary', 'text')
THEN '(' + CASE WHEN c.max_length = -1 THEN N'MAX' ELSE CAST(c.max_length AS NVARCHAR(5)) END + ')'
WHEN t.name IN ('nvarchar', 'nchar', 'ntext')
THEN '(' + CASE WHEN c.max_length = -1 THEN N'MAX' ELSE CAST(c.max_length / 2 AS NVARCHAR(5)) END + ')'
WHEN t.name IN ('datetime2', 'time2', 'datetimeoffset')
THEN '(' + CAST(c.scale AS NVARCHAR(5)) + ')'
WHEN t.name = 'decimal'
THEN '(' + CAST(c.[precision] AS NVARCHAR(5)) + ',' + CAST(c.scale AS NVARCHAR(5)) + ')'
ELSE ''
END +
CASE WHEN c.collation_name IS NOT NULL THEN ' COLLATE ' + c.collation_name ELSE '' END +
CASE WHEN c.is_nullable = 1 THEN ' NULL' ELSE ' NOT NULL' END +
CASE WHEN c.[definition] IS NOT NULL THEN ' DEFAULT' + c.[definition] ELSE '' END +
CASE WHEN c.is_identity = 1 THEN ' IDENTITY(' + CAST(ISNULL(c.seed_value, '0') AS CHAR(1)) + ',' + CAST(ISNULL(c.increment_value, '1') AS CHAR(1)) + ')' ELSE '' END + CHAR(13)
FROM (
SELECT
c.name
, c.user_type_id
, c.max_length
, c.[precision]
, c.scale
, dc.[definition]
, ic.seed_value
, ic.increment_value
, c.collation_name
, is_nullable = MIN(CAST(c.is_nullable AS TINYINT))
, is_identity = MIN(CAST(c.is_identity AS TINYINT))
FROM sys.columns c WITH (NOWAIT)
LEFT JOIN sys.default_constraints dc WITH (NOWAIT) ON c.default_object_id != 0 AND c.[object_id] = dc.parent_object_id AND c.column_id = dc.parent_column_id
LEFT JOIN sys.identity_columns ic WITH (NOWAIT) ON c.is_identity = 1 AND c.[object_id] = ic.[object_id] AND c.column_id = ic.column_id
WHERE c.[object_id] IN (SELECT o.[object_id] FROM tbl o)
GROUP BY
c.name
, c.user_type_id
, c.max_length
, c.[precision]
, c.scale
, dc.[definition]
, ic.seed_value
, ic.increment_value
, c.collation_name
) c
JOIN sys.types t WITH (NOWAIT) ON c.user_type_id = t.user_type_id
FOR XML PATH(N''), TYPE, ROOT).value('root[1]', 'NVARCHAR(MAX)'), 1, 2, CHAR(9) + ' ')
+ ISNULL((SELECT CHAR(9) + ', CONSTRAINT [PK_' + #schema_for_new_table + '_' + #name_for_new_table + '] PRIMARY KEY (' +
(SELECT STUFF((
SELECT DISTINCT ', [' + c.name + ']' + CASE WHEN ic.is_descending_key = 1 THEN ' DESC' ELSE ' ASC' END
FROM sys.key_constraints k WITH (NOWAIT)
JOIN sys.index_columns ic WITH (NOWAIT) ON ic.[object_id] = k.parent_object_id AND ic.index_id = k.unique_index_id
JOIN sys.columns c WITH (NOWAIT) ON c.[object_id] = k.parent_object_id AND c.column_id = ic.column_id
WHERE k.[type] = 'PK'
AND ic.is_included_column = 0
AND k.parent_object_id IN (SELECT t.[object_id] FROM tbl t)
FOR XML PATH(N''), TYPE, ROOT).value('root[1]', 'NVARCHAR(MAX)'), 1, 2, '')) + ')' + CHAR(13)), '')
+ ')' + CHAR(13)
+ ISNULL((SELECT (
SELECT CHAR(13) +
'ALTER TABLE [' + #schema_for_new_table + '].[' + #name_for_new_table + '] WITH'
+ CASE WHEN fk.is_not_trusted = 1
THEN ' NOCHECK'
ELSE ' CHECK'
END +
' ADD CONSTRAINT [FK_' + #schema_for_new_table + '_' + #name_for_new_table + '_' + fk.name + '] FOREIGN KEY('
+ STUFF((
SELECT ', [' + k.cname + ']'
FROM fk_columns k
WHERE k.constraint_object_id = fk.[object_id]
FOR XML PATH(N''), TYPE, ROOT).value('root[1]', 'NVARCHAR(MAX)'), 1, 2, '')
+ ')' +
' REFERENCES [' + SCHEMA_NAME(ro.[schema_id]) + '].[' + ro.name + '] ('
+ STUFF((
SELECT ', [' + k.rcname + ']'
FROM fk_columns k
WHERE k.constraint_object_id = fk.[object_id]
FOR XML PATH(N''), TYPE, ROOT).value('root[1]', 'NVARCHAR(MAX)'), 1, 2, '')
+ ')'
+ CASE
WHEN fk.delete_referential_action = 1 THEN ' ON DELETE CASCADE'
WHEN fk.delete_referential_action = 2 THEN ' ON DELETE SET NULL'
WHEN fk.delete_referential_action = 3 THEN ' ON DELETE SET DEFAULT'
ELSE ''
END
+ CASE
WHEN fk.update_referential_action = 1 THEN ' ON UPDATE CASCADE'
WHEN fk.update_referential_action = 2 THEN ' ON UPDATE SET NULL'
WHEN fk.update_referential_action = 3 THEN ' ON UPDATE SET DEFAULT'
ELSE ''
END
+ CHAR(13) + 'ALTER TABLE [' + #schema_for_new_table + '].[' + #name_for_new_table + '] CHECK CONSTRAINT [FK_' + #schema_for_new_table + '_' + #name_for_new_table + '_' + fk.name + ']' + CHAR(13)
FROM sys.foreign_keys fk WITH (NOWAIT)
JOIN sys.objects ro WITH (NOWAIT) ON ro.[object_id] = fk.referenced_object_id
WHERE fk.parent_object_id IN (SELECT t.[object_id] FROM tbl t)
FOR XML PATH(N''), TYPE, ROOT).value('root[1]', 'NVARCHAR(MAX)')), '')
+ ISNULL(((SELECT
CHAR(13) + 'CREATE' + CASE WHEN i.is_unique = 1 THEN ' UNIQUE' ELSE '' END + ' NONCLUSTERED INDEX [IX_' + #schema_for_new_table + '_' + #name_for_new_table + '_' + i.name + '] ON [' + #schema_for_new_table + '].[' + #name_for_new_table + '] (' +
STUFF((
SELECT ', [' + c.name + ']' + CASE WHEN c.is_descending_key = 1 THEN ' DESC' ELSE ' ASC' END
FROM index_column c
WHERE c.is_included_column = 0
AND c.index_id = i.index_id
FOR XML PATH(N''), TYPE, ROOT).value('root[1]', 'NVARCHAR(MAX)'), 1, 2, '') + ')'
+ ISNULL(CHAR(13) + 'INCLUDE (' +
STUFF((
SELECT ', [' + c.name + ']'
FROM index_column c
WHERE c.is_included_column = 1
AND c.index_id = i.index_id
FOR XML PATH(N''), TYPE, ROOT).value('root[1]', 'NVARCHAR(MAX)'), 1, 2, '') + ')', '') + CHAR(13)
FROM sys.indexes i WITH (NOWAIT)
WHERE i.[object_id] IN (SELECT t.[object_id] FROM tbl t)
AND i.is_primary_key = 0
AND i.[type] = 2
FOR XML PATH(N''), TYPE, ROOT).value('root[1]', 'NVARCHAR(MAX)')
), '')
PRINT #SQL
--EXEC sys.sp_executesql #SQL
For example, this query generate this DDL:
CREATE TABLE dbo.TableMerge
(
AbsenceCode VARCHAR(25) COLLATE Cyrillic_General_CI_AS NULL
, CategoryID INT NULL
, DateOut DATETIME NOT NULL
, DepartmentUID UNIQUEIDENTIFIER NOT NULL
, EmployeeID INT NOT NULL
, IsMainWorkPlace BIT NOT NULL DEFAULT((1))
, PaymentType CHAR(2) COLLATE Cyrillic_General_CI_AS NOT NULL
, PositionID INT NOT NULL
, TariffScaleID INT NULL
, TeamUID UNIQUEIDENTIFIER NULL
, TimeSheetDate DATETIME NOT NULL
, WorkHours REAL NULL
, WorkOutID BIGINT NOT NULL IDENTITY(1,1)
, WorkPlaceUID UNIQUEIDENTIFIER NOT NULL
, WorkShiftCD NVARCHAR(20) COLLATE Cyrillic_General_CI_AS NULL
, CONSTRAINT [PK_dbo.TableMerge] PRIMARY KEY (WorkOutID ASC, WorkPlaceUID ASC)
)
If specify this tables as source:
CREATE TABLE dbo.[WorkOut](
[WorkOutID] [bigint] IDENTITY(1,1) NOT NULL,
[TimeSheetDate] [datetime] NOT NULL,
[DateOut] [datetime] NOT NULL,
[EmployeeID] [int] NOT NULL,
[IsMainWorkPlace] [bit] NOT NULL,
[DepartmentUID] [uniqueidentifier] NOT NULL,
[WorkPlaceUID] [uniqueidentifier] NULL,
[TeamUID] [uniqueidentifier] NULL,
[WorkShiftCD] [nvarchar](10) NULL,
[WorkHours] [real] NULL,
[AbsenceCode] [varchar](25) NULL,
[PaymentType] [char](2) NULL,
[CategoryID] [int] NULL,
CONSTRAINT [PK_WorkOut] PRIMARY KEY CLUSTERED
(
[WorkOutID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
CREATE TABLE dbo.[WorkPlace](
[WorkPlaceUID] [uniqueidentifier] NOT NULL,
[PositionID] [int] NOT NULL,
[DepartmentUID] [uniqueidentifier] NOT NULL,
[PaymentType] [char](2) NOT NULL,
[TariffScaleID] [int] NULL,
[CategoryID] [int] NULL,
CONSTRAINT [PK_WorkPlace] PRIMARY KEY CLUSTERED
(
[WorkPlaceUID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
Is it possible in MS SQL Server 2008 to export database structure into a T-SQL file?
I want to export not only tables schema but also primary keys, foreign keys, constraints, indexes, stored procedures, user defined types/functions.
Also I don't want the data to be present in this T-SQL file.
Is there any way to achieve that?
You can generate scripts to a file via SQL Server Management Studio, here are the steps:
Right click the database you want to generate scripts for (not the table) and select tasks - generate scripts
Next, select the requested table/tables, views, stored procedures, etc (from select specific database objects)
Click advanced - select the types of data to script
Click Next and finish
MSDN Generate Scripts
When generating the scripts, there is an area that will allow you to script, constraints, keys, etc. From SQL Server 2008 R2 there is an Advanced Option under scripting:
In the picture you can see. In the set script options, choose the last option: Types of data to script you click at the right side and you choose what you want.
This is the option you should choose to export a schema and data
Have you tried the Generate Scripts (Right click, tasks, generate scripts) option in SQL Management Studio? Does that produce what you mean by a "SQL File"?
i wrote this sp to create automatically the schema with all things, pk, fk, partitions, constraints...
I wrote it to run in same sp.
IMPORTANT!!
before exec
create type TableType as table (ObjectID int)
here the SP:
create PROCEDURE [dbo].[util_ScriptTable]
#DBName SYSNAME
,#schema sysname
,#TableName SYSNAME
,#IncludeConstraints BIT = 1
,#IncludeIndexes BIT = 1
,#NewTableSchema sysname
,#NewTableName SYSNAME = NULL
,#UseSystemDataTypes BIT = 0
,#script varchar(max) output
AS
BEGIN try
if not exists (select * from sys.types where name = 'TableType')
create type TableType as table (ObjectID int)--drop type TableType
declare #sql nvarchar(max)
DECLARE #MainDefinition TABLE (FieldValue VARCHAR(200))
--DECLARE #DBName SYSNAME
DECLARE #ClusteredPK BIT
DECLARE #TableSchema NVARCHAR(255)
--SET #DBName = DB_NAME(DB_ID())
SELECT #TableName = name FROM sysobjects WHERE id = OBJECT_ID(#TableName)
DECLARE #ShowFields TABLE (FieldID INT IDENTITY(1,1)
,DatabaseName VARCHAR(100)
,TableOwner VARCHAR(100)
,TableName VARCHAR(100)
,FieldName VARCHAR(100)
,ColumnPosition INT
,ColumnDefaultValue VARCHAR(100)
,ColumnDefaultName VARCHAR(100)
,IsNullable BIT
,DataType VARCHAR(100)
,MaxLength varchar(10)
,NumericPrecision INT
,NumericScale INT
,DomainName VARCHAR(100)
,FieldListingName VARCHAR(110)
,FieldDefinition CHAR(1)
,IdentityColumn BIT
,IdentitySeed INT
,IdentityIncrement INT
,IsCharColumn BIT
,IsComputed varchar(255))
DECLARE #HoldingArea TABLE(FldID SMALLINT IDENTITY(1,1)
,Flds VARCHAR(4000)
,FldValue CHAR(1) DEFAULT(0))
DECLARE #PKObjectID TABLE(ObjectID INT)
DECLARE #Uniques TABLE(ObjectID INT)
DECLARE #HoldingAreaValues TABLE(FldID SMALLINT IDENTITY(1,1)
,Flds VARCHAR(4000)
,FldValue CHAR(1) DEFAULT(0))
DECLARE #Definition TABLE(DefinitionID SMALLINT IDENTITY(1,1)
,FieldValue VARCHAR(200))
set #sql=
'
use '+#DBName+'
SELECT distinct DB_NAME()
,inf.TABLE_SCHEMA
,inf.TABLE_NAME
,''[''+inf.COLUMN_NAME+'']'' as COLUMN_NAME
,CAST(inf.ORDINAL_POSITION AS INT)
,inf.COLUMN_DEFAULT
,dobj.name AS ColumnDefaultName
,CASE WHEN inf.IS_NULLABLE = ''YES'' THEN 1 ELSE 0 END
,inf.DATA_TYPE
,case inf.CHARACTER_MAXIMUM_LENGTH when -1 then ''max'' else CAST(inf.CHARACTER_MAXIMUM_LENGTH AS varchar) end--CAST(CHARACTER_MAXIMUM_LENGTH AS INT)
,CAST(inf.NUMERIC_PRECISION AS INT)
,CAST(inf.NUMERIC_SCALE AS INT)
,inf.DOMAIN_NAME
,inf.COLUMN_NAME + '',''
,'''' AS FieldDefinition
--caso di viste, dà come campo identity ma nn dà i valori, quindi lo ignoro
,CASE WHEN ic.object_id IS not NULL and ic.seed_value is not null THEN 1 ELSE 0 END AS IdentityColumn--CASE WHEN ic.object_id IS NULL THEN 0 ELSE 1 END AS IdentityColumn
,CAST(ISNULL(ic.seed_value,0) AS INT) AS IdentitySeed
,CAST(ISNULL(ic.increment_value,0) AS INT) AS IdentityIncrement
,CASE WHEN c.collation_name IS NOT NULL THEN 1 ELSE 0 END AS IsCharColumn
,cc.definition
from (select schema_id,object_id,name from sys.views union all select schema_id,object_id,name from sys.tables)t
--sys.tables t
join sys.schemas s on t.schema_id=s.schema_id
JOIN sys.columns c ON t.object_id=c.object_id --AND s.schema_id=c.schema_id
LEFT JOIN sys.identity_columns ic ON t.object_id=ic.object_id AND c.column_id=ic.column_id
left JOIN sys.types st ON st.system_type_id=c.system_type_id and st.principal_id=t.object_id--COALESCE(c.DOMAIN_NAME,c.DATA_TYPE) = st.name
LEFT OUTER JOIN sys.objects dobj ON dobj.object_id = c.default_object_id AND dobj.type = ''D''
left join sys.computed_columns cc on t.object_id=cc.object_id and c.column_id=cc.column_id
join INFORMATION_SCHEMA.COLUMNS inf on t.name=inf.TABLE_NAME
and s.name=inf.TABLE_SCHEMA
and c.name=inf.COLUMN_NAME
WHERE inf.TABLE_NAME = #TableName and inf.TABLE_SCHEMA=#schema
ORDER BY inf.ORDINAL_POSITION
'
print #sql
INSERT INTO #ShowFields( DatabaseName
,TableOwner
,TableName
,FieldName
,ColumnPosition
,ColumnDefaultValue
,ColumnDefaultName
,IsNullable
,DataType
,MaxLength
,NumericPrecision
,NumericScale
,DomainName
,FieldListingName
,FieldDefinition
,IdentityColumn
,IdentitySeed
,IdentityIncrement
,IsCharColumn
,IsComputed)
exec sp_executesql #sql,
N'#TableName varchar(50),#schema varchar(50)',
#TableName=#TableName,#schema=#schema
/*
SELECT #DBName--DB_NAME()
,TABLE_SCHEMA
,TABLE_NAME
,COLUMN_NAME
,CAST(ORDINAL_POSITION AS INT)
,COLUMN_DEFAULT
,dobj.name AS ColumnDefaultName
,CASE WHEN c.IS_NULLABLE = 'YES' THEN 1 ELSE 0 END
,DATA_TYPE
,CAST(CHARACTER_MAXIMUM_LENGTH AS INT)
,CAST(NUMERIC_PRECISION AS INT)
,CAST(NUMERIC_SCALE AS INT)
,DOMAIN_NAME
,COLUMN_NAME + ','
,'' AS FieldDefinition
,CASE WHEN ic.object_id IS NULL THEN 0 ELSE 1 END AS IdentityColumn
,CAST(ISNULL(ic.seed_value,0) AS INT) AS IdentitySeed
,CAST(ISNULL(ic.increment_value,0) AS INT) AS IdentityIncrement
,CASE WHEN st.collation_name IS NOT NULL THEN 1 ELSE 0 END AS IsCharColumn
FROM INFORMATION_SCHEMA.COLUMNS c
JOIN sys.columns sc ON c.TABLE_NAME = OBJECT_NAME(sc.object_id) AND c.COLUMN_NAME = sc.Name
LEFT JOIN sys.identity_columns ic ON c.TABLE_NAME = OBJECT_NAME(ic.object_id) AND c.COLUMN_NAME = ic.Name
JOIN sys.types st ON COALESCE(c.DOMAIN_NAME,c.DATA_TYPE) = st.name
LEFT OUTER JOIN sys.objects dobj ON dobj.object_id = sc.default_object_id AND dobj.type = 'D'
WHERE c.TABLE_NAME = #TableName
ORDER BY c.TABLE_NAME, c.ORDINAL_POSITION
*/
SELECT TOP 1 #TableSchema = TableOwner FROM #ShowFields
INSERT INTO #HoldingArea (Flds) VALUES('(')
INSERT INTO #Definition(FieldValue)VALUES('CREATE TABLE ' + CASE WHEN #NewTableName IS NOT NULL THEN #DBName + '.' + #NewTableSchema + '.' + #NewTableName ELSE #DBName + '.' + #TableSchema + '.' + #TableName END)
INSERT INTO #Definition(FieldValue)VALUES('(')
INSERT INTO #Definition(FieldValue)
SELECT CHAR(10) + FieldName + ' ' +
--CASE WHEN DomainName IS NOT NULL AND #UseSystemDataTypes = 0 THEN DomainName + CASE WHEN IsNullable = 1 THEN ' NULL ' ELSE ' NOT NULL ' END ELSE UPPER(DataType) +CASE WHEN IsCharColumn = 1 THEN '(' + CAST(MaxLength AS VARCHAR(10)) + ')' ELSE '' END +CASE WHEN IdentityColumn = 1 THEN ' IDENTITY(' + CAST(IdentitySeed AS VARCHAR(5))+ ',' + CAST(IdentityIncrement AS VARCHAR(5)) + ')' ELSE '' END +CASE WHEN IsNullable = 1 THEN ' NULL ' ELSE ' NOT NULL ' END +CASE WHEN ColumnDefaultName IS NOT NULL AND #IncludeConstraints = 1 THEN 'CONSTRAINT [' + ColumnDefaultName + '] DEFAULT' + UPPER(ColumnDefaultValue) ELSE '' END END + CASE WHEN FieldID = (SELECT MAX(FieldID) FROM #ShowFields) THEN '' ELSE ',' END
CASE WHEN DomainName IS NOT NULL AND #UseSystemDataTypes = 0 THEN DomainName +
CASe WHEN IsNullable = 1 THEN ' NULL '
ELSE ' NOT NULL '
END
ELSE
case when IsComputed is null then
UPPER(DataType) +
CASE WHEN IsCharColumn = 1 THEN '(' + CAST(MaxLength AS VARCHAR(10)) + ')'
ELSE
CASE WHEN DataType = 'numeric' THEN '(' + CAST(NumericPrecision AS VARCHAR(10))+','+ CAST(NumericScale AS VARCHAR(10)) + ')'
ELSE
CASE WHEN DataType = 'decimal' THEN '(' + CAST(NumericPrecision AS VARCHAR(10))+','+ CAST(NumericScale AS VARCHAR(10)) + ')'
ELSE ''
end
end
END +
CASE WHEN IdentityColumn = 1 THEN ' IDENTITY(' + CAST(IdentitySeed AS VARCHAR(5))+ ',' + CAST(IdentityIncrement AS VARCHAR(5)) + ')'
ELSE ''
END +
CASE WHEN IsNullable = 1 THEN ' NULL '
ELSE ' NOT NULL '
END +
CASE WHEN ColumnDefaultName IS NOT NULL AND #IncludeConstraints = 1 THEN 'CONSTRAINT [' + replace(ColumnDefaultName,#TableName,#NewTableName) + '] DEFAULT' + UPPER(ColumnDefaultValue)
ELSE ''
END
else
' as '+IsComputed+' '
end
END +
CASE WHEN FieldID = (SELECT MAX(FieldID) FROM #ShowFields) THEN ''
ELSE ','
END
FROM #ShowFields
IF #IncludeConstraints = 1
BEGIN
set #sql=
'
use '+#DBName+'
SELECT distinct '',CONSTRAINT ['' + #NewTableName+''_''+replace(name,#TableName,'''') + ''] FOREIGN KEY ('' + ParentColumns + '') REFERENCES ['' + ReferencedObject + '']('' + ReferencedColumns + '')''
FROM ( SELECT ReferencedObject = OBJECT_NAME(fk.referenced_object_id), ParentObject = OBJECT_NAME(parent_object_id),fk.name
, REVERSE(SUBSTRING(REVERSE(( SELECT cp.name + '',''
FROM sys.foreign_key_columns fkc
JOIN sys.columns cp ON fkc.parent_object_id = cp.object_id AND fkc.parent_column_id = cp.column_id
WHERE fkc.constraint_object_id = fk.object_id FOR XML PATH('''') )), 2, 8000)) ParentColumns,
REVERSE(SUBSTRING(REVERSE(( SELECT cr.name + '',''
FROM sys.foreign_key_columns fkc
JOIN sys.columns cr ON fkc.referenced_object_id = cr.object_id AND fkc.referenced_column_id = cr.column_id
WHERE fkc.constraint_object_id = fk.object_id FOR XML PATH('''') )), 2, 8000)) ReferencedColumns
FROM sys.foreign_keys fk
inner join sys.schemas s on fk.schema_id=s.schema_id and s.name=#schema) a
WHERE ParentObject = #TableName
'
print #sql
INSERT INTO #Definition(FieldValue)
exec sp_executesql #sql,
N'#TableName varchar(50),#NewTableName varchar(50),#schema varchar(50)',
#TableName=#TableName,#NewTableName=#NewTableName,#schema=#schema
/*
SELECT ',CONSTRAINT [' + name + '] FOREIGN KEY (' + ParentColumns + ') REFERENCES [' + ReferencedObject + '](' + ReferencedColumns + ')'
FROM ( SELECT ReferencedObject = OBJECT_NAME(fk.referenced_object_id), ParentObject = OBJECT_NAME(parent_object_id),fk.name
, REVERSE(SUBSTRING(REVERSE(( SELECT cp.name + ','
FROM sys.foreign_key_columns fkc
JOIN sys.columns cp ON fkc.parent_object_id = cp.object_id AND fkc.parent_column_id = cp.column_id
WHERE fkc.constraint_object_id = fk.object_id FOR XML PATH('') )), 2, 8000)) ParentColumns,
REVERSE(SUBSTRING(REVERSE(( SELECT cr.name + ','
FROM sys.foreign_key_columns fkc
JOIN sys.columns cr ON fkc.referenced_object_id = cr.object_id AND fkc.referenced_column_id = cr.column_id
WHERE fkc.constraint_object_id = fk.object_id FOR XML PATH('') )), 2, 8000)) ReferencedColumns
FROM sys.foreign_keys fk ) a
WHERE ParentObject = #TableName
*/
set #sql=
'
use '+#DBName+'
SELECT distinct '',CONSTRAINT ['' + #NewTableName+''_''+replace(c.name,#TableName,'''') + ''] CHECK '' + definition
FROM sys.check_constraints c join sys.schemas s on c.schema_id=s.schema_id and s.name=#schema
WHERE OBJECT_NAME(parent_object_id) = #TableName
'
print #sql
INSERT INTO #Definition(FieldValue)
exec sp_executesql #sql,
N'#TableName varchar(50),#NewTableName varchar(50),#schema varchar(50)',
#TableName=#TableName,#NewTableName=#NewTableName,#schema=#schema
/*
SELECT ',CONSTRAINT [' + name + '] CHECK ' + definition FROM sys.check_constraints
WHERE OBJECT_NAME(parent_object_id) = #TableName
*/
set #sql=
'
use '+#DBName+'
SELECT DISTINCT PKObject = cco.object_id
FROM sys.key_constraints cco
JOIN sys.index_columns cc ON cco.parent_object_id = cc.object_id AND cco.unique_index_id = cc.index_id
JOIN sys.indexes i ON cc.object_id = i.object_id AND cc.index_id = i.index_id
join sys.schemas s on cco.schema_id=s.schema_id and s.name=#schema
WHERE OBJECT_NAME(parent_object_id) = #TableName AND i.type = 1 AND is_primary_key = 1
'
print #sql
INSERT INTO #PKObjectID(ObjectID)
exec sp_executesql #sql,
N'#TableName varchar(50),#schema varchar(50)',
#TableName=#TableName,#schema=#schema
/*
SELECT DISTINCT PKObject = cco.object_id
FROM sys.key_constraints cco
JOIN sys.index_columns cc ON cco.parent_object_id = cc.object_id AND cco.unique_index_id = cc.index_id
JOIN sys.indexes i ON cc.object_id = i.object_id AND cc.index_id = i.index_id
WHERE OBJECT_NAME(parent_object_id) = #TableName AND i.type = 1 AND is_primary_key = 1
*/
set #sql=
'
use '+#DBName+'
SELECT DISTINCT PKObject = cco.object_id
FROM sys.key_constraints cco
JOIN sys.index_columns cc ON cco.parent_object_id = cc.object_id AND cco.unique_index_id = cc.index_id
JOIN sys.indexes i ON cc.object_id = i.object_id AND cc.index_id = i.index_id
join sys.schemas s on cco.schema_id=s.schema_id and s.name=#schema
WHERE OBJECT_NAME(parent_object_id) = #TableName AND i.type = 2 AND is_primary_key = 0 AND is_unique_constraint = 1
'
print #sql
INSERT INTO #Uniques(ObjectID)
exec sp_executesql #sql,
N'#TableName varchar(50),#schema varchar(50)',
#TableName=#TableName,#schema=#schema
/*
SELECT DISTINCT PKObject = cco.object_id
FROM sys.key_constraints cco
JOIN sys.index_columns cc ON cco.parent_object_id = cc.object_id AND cco.unique_index_id = cc.index_id
JOIN sys.indexes i ON cc.object_id = i.object_id AND cc.index_id = i.index_id
WHERE OBJECT_NAME(parent_object_id) = #TableName AND i.type = 2 AND is_primary_key = 0 AND is_unique_constraint = 1
*/
SET #ClusteredPK = CASE WHEN ##ROWCOUNT > 0 THEN 1 ELSE 0 END
declare #t TableType
insert #t select * from #PKObjectID
declare #u TableType
insert #u select * from #Uniques
set #sql=
'
use '+#DBName+'
SELECT distinct '',CONSTRAINT '' + #NewTableName+''_''+replace(cco.name,#TableName,'''') + CASE type WHEN ''PK'' THEN '' PRIMARY KEY '' + CASE WHEN pk.ObjectID IS NULL THEN '' NONCLUSTERED '' ELSE '' CLUSTERED '' END WHEN ''UQ'' THEN '' UNIQUE '' END + CASE WHEN u.ObjectID IS NOT NULL THEN '' NONCLUSTERED '' ELSE '''' END
+ ''(''+REVERSE(SUBSTRING(REVERSE(( SELECT c.name + + CASE WHEN cc.is_descending_key = 1 THEN '' DESC'' ELSE '' ASC'' END + '',''
FROM sys.key_constraints ccok
LEFT JOIN sys.index_columns cc ON ccok.parent_object_id = cc.object_id AND cco.unique_index_id = cc.index_id
LEFT JOIN sys.columns c ON cc.object_id = c.object_id AND cc.column_id = c.column_id
LEFT JOIN sys.indexes i ON cc.object_id = i.object_id AND cc.index_id = i.index_id
WHERE i.object_id = ccok.parent_object_id AND ccok.object_id = cco.object_id
order by key_ordinal FOR XML PATH(''''))), 2, 8000)) + '')''
FROM sys.key_constraints cco
inner join sys.schemas s on cco.schema_id=s.schema_id and s.name=#schema
LEFT JOIN #U u ON cco.object_id = u.objectID
LEFT JOIN #t pk ON cco.object_id = pk.ObjectID
WHERE OBJECT_NAME(cco.parent_object_id) = #TableName
'
print #sql
INSERT INTO #Definition(FieldValue)
exec sp_executesql #sql,
N'#TableName varchar(50),#NewTableName varchar(50),#schema varchar(50),#t TableType readonly,#u TableType readonly',
#TableName=#TableName,#NewTableName=#NewTableName,#schema=#schema,#t=#t,#u=#u
/*
SELECT ',CONSTRAINT ' + name + CASE type WHEN 'PK' THEN ' PRIMARY KEY ' + CASE WHEN pk.ObjectID IS NULL THEN ' NONCLUSTERED ' ELSE ' CLUSTERED ' END WHEN 'UQ' THEN ' UNIQUE ' END + CASE WHEN u.ObjectID IS NOT NULL THEN ' NONCLUSTERED ' ELSE '' END
+ '(' +REVERSE(SUBSTRING(REVERSE(( SELECT c.name + + CASE WHEN cc.is_descending_key = 1 THEN ' DESC' ELSE ' ASC' END + ','
FROM sys.key_constraints ccok
LEFT JOIN sys.index_columns cc ON ccok.parent_object_id = cc.object_id AND cco.unique_index_id = cc.index_id
LEFT JOIN sys.columns c ON cc.object_id = c.object_id AND cc.column_id = c.column_id
LEFT JOIN sys.indexes i ON cc.object_id = i.object_id AND cc.index_id = i.index_id
WHERE i.object_id = ccok.parent_object_id AND ccok.object_id = cco.object_id FOR XML PATH(''))), 2, 8000)) + ')'
FROM sys.key_constraints cco
LEFT JOIN #PKObjectID pk ON cco.object_id = pk.ObjectID
LEFT JOIN #Uniques u ON cco.object_id = u.objectID
WHERE OBJECT_NAME(cco.parent_object_id) = #TableName
*/
END
INSERT INTO #Definition(FieldValue) VALUES(')')
set #sql=
'
use '+#DBName+'
select '' on '' + d.name + ''([''+c.name+''])''
from sys.tables t join sys.indexes i on(i.object_id = t.object_id and i.index_id < 2)
join sys.index_columns ic on(ic.partition_ordinal > 0 and ic.index_id = i.index_id and ic.object_id = t.object_id)
join sys.columns c on(c.object_id = ic.object_id and c.column_id = ic.column_id)
join sys.schemas s on t.schema_id=s.schema_id
join sys.data_spaces d on i.data_space_id=d.data_space_id
where t.name=#TableName and s.name=#schema
order by key_ordinal
'
print 'x'
print #sql
INSERT INTO #Definition(FieldValue)
exec sp_executesql #sql,
N'#TableName varchar(50),#schema varchar(50)',
#TableName=#TableName,#schema=#schema
IF #IncludeIndexes = 1
BEGIN
set #sql=
'
use '+#DBName+'
SELECT distinct '' CREATE '' + i.type_desc + '' INDEX ['' + replace(i.name COLLATE SQL_Latin1_General_CP1_CI_AS,#TableName,#NewTableName) + ''] ON '+#DBName+'.'+#NewTableSchema+'.'+#NewTableName+' (''
+ REVERSE(SUBSTRING(REVERSE(( SELECT name + CASE WHEN sc.is_descending_key = 1 THEN '' DESC'' ELSE '' ASC'' END + '',''
FROM sys.index_columns sc
JOIN sys.columns c ON sc.object_id = c.object_id AND sc.column_id = c.column_id
WHERE t.name=#TableName AND sc.object_id = i.object_id AND sc.index_id = i.index_id
and is_included_column=0
ORDER BY key_ordinal ASC FOR XML PATH('''') )), 2, 8000)) + '')''+
ISNULL( '' include (''+REVERSE(SUBSTRING(REVERSE(( SELECT name + '',''
FROM sys.index_columns sc
JOIN sys.columns c ON sc.object_id = c.object_id AND sc.column_id = c.column_id
WHERE t.name=#TableName AND sc.object_id = i.object_id AND sc.index_id = i.index_id
and is_included_column=1
ORDER BY key_ordinal ASC FOR XML PATH('''') )), 2, 8000))+'')'' ,'''')+''''
FROM sys.indexes i join sys.tables t on i.object_id=t.object_id
join sys.schemas s on t.schema_id=s.schema_id
AND CASE WHEN #ClusteredPK = 1 AND is_primary_key = 1 AND i.type = 1 THEN 0 ELSE 1 END = 1 AND is_unique_constraint = 0 AND is_primary_key = 0
where t.name=#TableName and s.name=#schema
'
print #sql
INSERT INTO #Definition(FieldValue)
exec sp_executesql #sql,
N'#TableName varchar(50),#NewTableName varchar(50),#schema varchar(50), #ClusteredPK bit',
#TableName=#TableName,#NewTableName=#NewTableName,#schema=#schema,#ClusteredPK=#ClusteredPK
END
/*
SELECT 'CREATE ' + type_desc + ' INDEX [' + [name] COLLATE SQL_Latin1_General_CP1_CI_AS + '] ON [' + OBJECT_NAME(object_id) + '] (' + REVERSE(SUBSTRING(REVERSE(( SELECT name + CASE WHEN sc.is_descending_key = 1 THEN ' DESC' ELSE ' ASC' END + ','
FROM sys.index_columns sc
JOIN sys.columns c ON sc.object_id = c.object_id AND sc.column_id = c.column_id
WHERE OBJECT_NAME(sc.object_id) = #TableName AND sc.object_id = i.object_id AND sc.index_id = i.index_id
ORDER BY index_column_id ASC FOR XML PATH('') )), 2, 8000)) + ')'
FROM sys.indexes i
WHERE OBJECT_NAME(object_id) = #TableName
AND CASE WHEN #ClusteredPK = 1 AND is_primary_key = 1 AND type = 1 THEN 0 ELSE 1 END = 1 AND is_unique_constraint = 0 AND is_primary_key = 0
*/
INSERT INTO #MainDefinition(FieldValue)
SELECT FieldValue FROM #Definition
ORDER BY DefinitionID ASC
----------------------------------
--SELECT FieldValue+'' FROM #MainDefinition FOR XML PATH('')
set #script='use '+#DBName+' '+(SELECT FieldValue+'' FROM #MainDefinition FOR XML PATH(''))
--declare #q varchar(max)
--set #q=(select replace((SELECT FieldValue FROM #MainDefinition FOR XML PATH('')),'</FieldValue>',''))
--set #script=(select REPLACE(#q,'<FieldValue>',''))
--drop type TableType
END try
-- ##############################################################################################################################################################################
BEGIN CATCH
BEGIN
-- INIZIO Procedura in errore =========================================================================================================================================================
PRINT '***********************************************************************************************************************************************************'
PRINT 'ErrorNumber : ' + CAST(ERROR_NUMBER() AS NVARCHAR(MAX))
PRINT 'ErrorSeverity : ' + CAST(ERROR_SEVERITY() AS NVARCHAR(MAX))
PRINT 'ErrorState : ' + CAST(ERROR_STATE() AS NVARCHAR(MAX))
PRINT 'ErrorLine : ' + CAST(ERROR_LINE() AS NVARCHAR(MAX))
PRINT 'ErrorMessage : ' + CAST(ERROR_MESSAGE() AS NVARCHAR(MAX))
PRINT '***********************************************************************************************************************************************************'
-- FINE Procedura in errore =========================================================================================================================================================
END
set #script=''
return -1
END CATCH
-- ##############################################################################################################################################################################
to exec it:
declare #s varchar(max)
exec [util_ScriptTable] 'db','schema_source','table_source',1,1,'schema_dest','tab_dest',0,#s output
select #s
If I run the following script
SELECT so.Name, sc.Text
FROM syscomments sc JOIN sysobjects so ON sc.ID = so.ID
I will get all the create procedure/function scripts. Is there something like this for tables, indexes, keys, and triggers?
Thank you for your help!
Nothing inbuilt.
To do it in T-SQL You'd need to find or write your own script. Example here that might get you started.
for SQL 2012 and later use this
I have tried many scripts in web. All of them had flaws. I finally wrote my script using a post in msdn site and changed it to work on many kind of situations.
Note: This scripts works on SQL Server 2012 and later
WITH IndexInfo AS (
SELECT ix.object_id
, ix.NAME AS IndexName
, ix.type_desc
, ix.filter_definition
, ix.is_unique
, ix.is_primary_key
, ix.allow_row_locks
, ix.allow_page_locks
, ds.NAME AS DataSpaceName
, ds.type AS DataSpaceType
, ix.is_padded
, object_schema_name(ix.object_id) AS SchemaName
, object_name(ix.object_id) AS TableName
, IIF(ix.type <= 2, is_included_column, 0) AS HasIncludedColumn
, IIF(ix.type in (5, 6), 1, 0) AS IsColumnStore
, (
SELECT KeyColumns
FROM (
SELECT IC2.object_id
, IC2.index_id
, STUFF((
SELECT ' , ' + C.NAME + IIF(MAX(CONVERT(INT, IC1.is_descending_key)) = 1 AND ix.type <= 2, ' DESC ', ' ')
FROM sys.index_columns IC1
JOIN sys.columns C ON C.object_id = IC1.object_id
AND C.column_id = IC1.column_id
--AND IC1.is_included_column = 0
AND IIF(ix.type <= 2, IC1.is_included_column, 0) = 0
AND IIF(ix.type <= 2, IC1.key_ordinal, ic.index_column_id) > 0
WHERE IC1.object_id = IC2.object_id
AND IC1.index_id = IC2.index_id
GROUP BY IC1.object_id
, C.NAME
, index_id
, IC1.key_ordinal
, IC1.index_column_id
ORDER BY IIF(ix.type <= 2, IC1.key_ordinal, IC1.index_column_id)
FOR XML PATH('')
), 1, 2, '') KeyColumns
FROM sys.index_columns IC2
GROUP BY IC2.object_id
, IC2.index_id
) tmp
WHERE tmp.object_id = ix.object_id
AND tmp.index_id = ix.index_id
) AS KeyColumnsStr
, (
SELECT IncludedColumns
FROM (
SELECT IC2.object_id, IC2.index_id
, STUFF((
SELECT ' , ' + C.NAME
FROM sys.index_columns IC1
JOIN sys.columns C ON C.object_id = IC1.object_id
AND C.column_id = IC1.column_id
--AND IC1.is_included_column = 1
AND IIF(ix.type <= 2, IC1.is_included_column, 0) <> 0
WHERE IC1.object_id = IC2.object_id
AND IC1.index_id = IC2.index_id
--and IIF(ix.type <= 2, IC1.key_ordinal, ic.index_column_id) > 0
GROUP BY IC1.object_id, C.NAME, index_id, IC1.key_ordinal, IC1.index_column_id
ORDER BY IIF(ix.type <= 2, IC1.key_ordinal, IC1.index_column_id)
FOR XML PATH('')
), 1, 2, '') IncludedColumns
FROM sys.index_columns IC2
GROUP BY IC2.object_id, IC2.index_id
) tmp
WHERE tmp.object_id = ix.object_id
AND tmp.index_id = ix.index_id
AND IncludedColumns IS NOT NULL
) AS IncludedColumnsStr
FROM sys.indexes ix
INNER JOIN sys.index_columns ic ON ic.index_id = ix.index_id AND ic.object_id = ix.object_id
INNER JOIN sys.columns col ON col.column_id = ic.column_id AND col.object_id = ix.object_id
INNER JOIN sys.data_spaces ds ON ix.data_space_id = ds.data_space_id
WHERE ix.NAME IS NOT NULL AND IIF(ix.type <= 2, ic.key_ordinal, ic.index_column_id) > 0
)
, Scrpt AS (
SELECT *
, 'CREATE '
+ CASE WHEN is_unique = 1 THEN ' UNIQUE ' ELSE '' END
+ type_desc COLLATE DATABASE_DEFAULT + ' INDEX ' + IndexName + ' ON '
+ SchemaName + '.' + TableName + '(' + KeyColumnsStr + ')'
+ ISNULL(IIF(HasIncludedColumn > 0, ' ', ' INCLUDE (' + IncludedColumnsStr + ')'), '')
+ IIF(filter_definition IS NULL, ' ', ' WHERE ' + filter_definition)
+ ' WITH ('
+ CASE WHEN IsColumnStore = 1 THEN ' DROP_EXISTING = OFF '
ELSE
IIF(is_padded = 1, ' PAD_INDEX = ON ', ' PAD_INDEX = OFF ') + ','
+ ' DROP_EXISTING = OFF ' + ','
+ ' ONLINE = OFF ' + ','
+ IIF(allow_row_locks = 1, ' ALLOW_ROW_LOCKS = ON ', ' ALLOW_ROW_LOCKS = OFF ') + ','
+ IIF(allow_page_locks = 1, ' ALLOW_PAGE_LOCKS = ON ', ' ALLOW_PAGE_LOCKS = OFF ')
END
+ ' ) '
+ IIF(DataSpaceType = 'FG','ON [' + DataSpaceName + ']', '') AS CreateIndexScript
FROM IndexInfo
)
SELECT DISTINCT IndexName, CreateIndexScript
FROM Scrpt
WHERE object_id = object_id('dbo.SWPartition')
As a credit to original author, I mentions the original link here
How to Generate Index Creation Scripts