Created
August 22, 2016 17:42
-
-
Save casperOne/94fef98ddf98aa11b679deb8ad0e7af9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
select | |
stuff(( | |
select | |
', | |
CONSTRAINT [' + | |
fk.name + | |
'] FOREIGN KEY (' + | |
stuff(( | |
select | |
', ' + tc.escapedColumnName as [text()] | |
from | |
sys.foreign_key_columns as fkc | |
inner join support.TableColumn as tc on | |
tc.tableObjectId = fkc.parent_object_id and | |
tc.columnId = fkc.parent_column_id | |
where | |
fkc.constraint_object_id = fk.object_id | |
order by | |
fkc.parent_column_id | |
for xml path(''), type | |
).value('.', 'nvarchar(max)'), 1, 2, '') + | |
') REFERENCES [' + | |
s.name + '].[' + | |
t.name + '](' + | |
stuff(( | |
select | |
', ' + tc.escapedColumnName as [text()] | |
from | |
sys.foreign_key_columns as fkc | |
inner join support.TableColumn as tc on | |
tc.tableObjectId = fkc.referenced_object_id and | |
tc.columnId = fkc.referenced_column_id | |
where | |
fkc.constraint_object_id = fk.object_id | |
order by | |
fkc.referenced_column_id | |
for xml path(''), type | |
).value('.', 'nvarchar(max)'), 1, 2, '') + | |
')' as [text()] | |
from | |
sys.foreign_keys as fk | |
inner join sys.tables as t on | |
t.object_id = fk.parent_object_id | |
inner join sys.schemas as s on | |
s.schema_id = t.schema_id | |
where | |
fk.parent_object_id = object_id('auth.RoleRole') | |
for xml path (''), type | |
).value('.', 'nvarchar(max)'), 1, 2, '') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment