Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9b98744

Browse files
authored
Update Schema.sql
Fixes issues with Primary Key creation and adds foreign key constraints
1 parent 6955a2c commit 9b98744

File tree

1 file changed

+67
-6
lines changed

1 file changed

+67
-6
lines changed

‎ExtractDB/Schema.sql

Lines changed: 67 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
DECLARE
22
--Variables for execution control
3-
@DropAndRecreate bit = 1,
3+
@DropAndRecreate bit = 0,
44
@SQL varchar(max),
55
--Variables for Schemas
66
@SchemaName varchar(128),
@@ -144,7 +144,7 @@ BEGIN
144144
FROM #ColumnData
145145
Order by column_id
146146

147-
SET @SQL = @SQL + @FirstColumn + ' ' + @ColumnName + ' '
147+
SET @SQL = @SQL + @FirstColumn + ' [' + @ColumnName + '] '
148148

149149
if @ColIsComputed = 0
150150
BEGIN
@@ -200,7 +200,22 @@ BEGIN
200200
DELETE FROM #ColumnData WHERE ColName = @ColumnName;
201201
END
202202

203-
SET @SQL = 'CREATE TABLE ' + @SchemaName + '.' + @TableName + ' (' + @SQL + ')';
203+
SET @SQL = @SQL +
204+
ISNULL((SELECT CHAR(9) + ', CONSTRAINT [' + k.name + '] PRIMARY KEY (' +
205+
(SELECT STUFF((
206+
SELECT ', [' + c.name + '] ' + CASE WHEN ic.is_descending_key = 1 THEN 'DESC' ELSE 'ASC' END
207+
FROM sys.index_columns ic WITH (NOWAIT)
208+
JOIN sys.columns c WITH (NOWAIT) ON c.[object_id] = ic.[object_id] AND c.column_id = ic.column_id
209+
WHERE ic.is_included_column = 0
210+
AND ic.[object_id] = k.parent_object_id
211+
AND ic.index_id = k.unique_index_id
212+
FOR XML PATH(N''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 2, ''))
213+
+ ')' + CHAR(13)
214+
FROM sys.key_constraints k WITH (NOWAIT)
215+
WHERE k.parent_object_id = @TableObjectId
216+
AND k.[type] = 'PK'), '') + ')' + CHAR(13)
217+
218+
SET @SQL = 'CREATE TABLE ' + @SchemaName + '.' + @TableName + ' (' + @SQL ;
204219

205220
IF @DropAndRecreate = 1
206221
INSERT INTO @Scripts
@@ -341,6 +356,52 @@ BEGIN
341356

342357
DELETE FROM #tables WHERE [object_id] = @TableObjectId
343358
END
359+
360+
--Going to pull in the keys. Not doing this with the table create in case this is being run with the bulk copy to ease the data in.
361+
INSERT INTO @Scripts
362+
(
363+
ScriptType,
364+
TableName,
365+
SqlStatement,
366+
SchemaName
367+
)
368+
SELECT
369+
'Foreign Keys',
370+
ct.[name],
371+
'ALTER TABLE '
372+
+ QUOTENAME(cs.name) + '.' + QUOTENAME(ct.name)
373+
+ ' ADD CONSTRAINT ' + QUOTENAME(fk.name)
374+
+ ' FOREIGN KEY (' + STUFF((SELECT ',' + QUOTENAME(c.name)
375+
-- get all the columns in the constraint table
376+
FROM sys.columns AS c
377+
INNER JOIN sys.foreign_key_columns AS fkc
378+
ON fkc.parent_column_id = c.column_id
379+
AND fkc.parent_object_id = c.[object_id]
380+
WHERE fkc.constraint_object_id = fk.[object_id]
381+
ORDER BY fkc.constraint_column_id
382+
FOR XML PATH(N''), TYPE).value(N'.[1]', N'nvarchar(max)'), 1, 1, N'')
383+
+ ') REFERENCES ' + QUOTENAME(rs.name) + '.' + QUOTENAME(rt.name)
384+
+ '(' + STUFF((SELECT ',' + QUOTENAME(c.name)
385+
-- get all the referenced columns
386+
FROM sys.columns AS c
387+
INNER JOIN sys.foreign_key_columns AS fkc
388+
ON fkc.referenced_column_id = c.column_id
389+
AND fkc.referenced_object_id = c.[object_id]
390+
WHERE fkc.constraint_object_id = fk.[object_id]
391+
ORDER BY fkc.constraint_column_id
392+
FOR XML PATH(N''), TYPE).value(N'.[1]', N'nvarchar(max)'), 1, 1, N'') + ');',
393+
cs.[Name]
394+
FROM sys.foreign_keys AS fk
395+
INNER JOIN sys.tables AS rt -- referenced table
396+
ON fk.referenced_object_id = rt.[object_id]
397+
INNER JOIN sys.schemas AS rs
398+
ON rt.[schema_id] = rs.[schema_id]
399+
INNER JOIN sys.tables AS ct -- constraint table
400+
ON fk.parent_object_id = ct.[object_id]
401+
INNER JOIN sys.schemas AS cs
402+
ON ct.[schema_id] = cs.[schema_id]
403+
WHERE rt.is_ms_shipped = 0 AND ct.is_ms_shipped = 0;
404+
344405
--Functions
345406
INSERT INTO @Scripts
346407
(
@@ -370,6 +431,7 @@ END
370431
from sys.procedures p
371432
inner join sys.sql_modules m on p.object_id = m.object_id
372433

434+
--Grab the Views
373435
INSERT INTO @Scripts
374436
(
375437
ScriptType,
@@ -382,6 +444,5 @@ END
382444
from sys.views p
383445
inner join sys.sql_modules m on p.object_id = m.object_id
384446

385-
SELECT *, LEN(SqlStatement) AS StatementLength FROM @Scripts;
386-
387-
447+
SELECT *, LEN(SqlStatement) AS StatementLength
448+
FROM @Scripts

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /