Created
April 11, 2022 16:15
-
-
Save CheetahChrome/0747039f80a7ab8f870ab286c3e7c2b0 to your computer and use it in GitHub Desktop.
Visual Studio Snippet to add a FK Relationship with Drop FK
This file contains hidden or 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
<?xml version="1.0" encoding="utf-8" ?> | |
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet"> | |
<CodeSnippet Format="1.0.0"> | |
<Header> | |
<Title>Generic FK Create/Drop</Title> | |
<Shortcut>FKMSTS</Shortcut> | |
<Description>Code snippet for Foreign Key Creation and Dropping</Description> | |
<Author>William Wegerson</Author> | |
<SnippetTypes> | |
<SnippetType>Expansion</SnippetType> | |
</SnippetTypes> | |
</Header> | |
<Snippet> | |
<Declarations> | |
<Literal> | |
<ID>schema</ID> | |
<ToolTip>Schema Main Table</ToolTip> | |
<Default>info</Default> | |
</Literal> | |
<Literal> | |
<ID>table</ID> | |
<ToolTip>table</ToolTip> | |
<Default>SourceTable</Default> | |
</Literal> | |
<Literal> | |
<ID>column</ID> | |
<ToolTip>Info table column</ToolTip> | |
<Default>SourceColumn</Default> | |
</Literal> | |
<Literal> | |
<ID>targetSchema</ID> | |
<ToolTip>Schema Target Table</ToolTip> | |
<Default>info</Default> | |
</Literal> | |
<Literal> | |
<ID>targetTable</ID> | |
<ToolTip>Target table</ToolTip> | |
<Default>TargetTable</Default> | |
</Literal> | |
<Literal> | |
<ID>targetColumn</ID> | |
<ToolTip>Target Column</ToolTip> | |
<Default>TargetColumn</Default> | |
</Literal> | |
</Declarations> | |
<Code Language="sql"> | |
<![CDATA[ | |
-- Create $schema$.$table$.$column$ To $targetSchema$.$targetTable$ FK | |
alter table [$schema$].[$table$] | |
with check add constraint [FK_$table$$column$_$targetTable$] | |
foreign key([$column$]) | |
references [$targetSchema$].[$targetTable$] ([$targetColumn$]) | |
go | |
alter table [$schema$].[$table$] | |
check constraint [FK_$table$$column$_$targetTable$] | |
go | |
-- $schema$.$table$.$column$ | |
-- DROP FK From $schema$.$table$.$column$ | |
if exists(select 1 from INFORMATION_SCHEMA.TABLE_CONSTRAINTS where CONSTRAINT_NAME='FK_$table$$column$_$targetTable$') | |
ALTER TABLE [$schema$].[$table$] DROP CONSTRAINT FK_$table$$column$_$targetTable$; | |
-- $schema$.$table$.$column$ | |
]]> | |
</Code> | |
</Snippet> | |
</CodeSnippet> | |
</CodeSnippets> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment