sql_cheat_sheet
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| sql_cheat_sheet [2020/03/04 02:39] – [List All Foreign Keys] stephen | sql_cheat_sheet [2025/01/13 22:44] (current) – stephen | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ====== SQL Cheat Sheet ====== | ====== SQL Cheat Sheet ====== | ||
| + | |||
| + | For Postgres SQL, see [[postgres_sql_cheat_sheet|Postgres SQL Cheat Sheet]] | ||
| ===== CREATE ===== | ===== CREATE ===== | ||
| Line 28: | Line 30: | ||
| FROM | FROM | ||
| Table2 | Table2 | ||
| + | |||
| + | -- INSERTing multiple rows. | ||
| + | INSERT INTO Table1 | ||
| + | (Col1, | ||
| + | VALUES | ||
| + | (Data1, Data2, Data3) | ||
| + | , (Data1, Data2, Data3) | ||
| </ | </ | ||
| Line 39: | Line 48: | ||
| Table1.Col1 = Table2.Col1 | Table1.Col1 = Table2.Col1 | ||
| , Table1.Col2 = Table2.Col2 | , Table1.Col2 = Table2.Col2 | ||
| + | FROM | ||
| + | Table1 | ||
| + | INNER JOIN | ||
| + | Table2 | ||
| + | ON | ||
| + | Table2.Id = Table1.Id | ||
| + | </ | ||
| + | |||
| + | ===== DELETE ===== | ||
| + | |||
| + | <code sql> | ||
| + | -- Combining an UPDATE with a JOIN. | ||
| + | DELETE | ||
| + | Table1 | ||
| FROM | FROM | ||
| Table1 | Table1 | ||
| Line 99: | Line 122: | ||
| FROM | FROM | ||
| Employee | Employee | ||
| + | </ | ||
| + | |||
| + | ===== Cascading Delete (work in progress) ===== | ||
| + | |||
| + | <code sql> | ||
| + | DECLARE @TargetTable VARCHAR(MAX) = ' | ||
| + | DECLARE @maxExpansions INT = 600 | ||
| + | |||
| + | DECLARE @FamilyTree TABLE ( | ||
| + | ObjectId | ||
| + | , TableName | ||
| + | , IsExpanded BIT NOT NULL | ||
| + | , [Path] | ||
| + | ) | ||
| + | |||
| + | INSERT | ||
| + | @FamilyTree (ObjectId, TableName, IsExpanded, [Path]) | ||
| + | SELECT | ||
| + | tables.object_id | ||
| + | , tables.[name] | ||
| + | , 0 | ||
| + | , tables.[name] | ||
| + | FROM | ||
| + | sys.tables | ||
| + | WHERE | ||
| + | sys.tables.[name] = @TargetTable | ||
| + | |||
| + | DECLARE @currentObjectId INT | ||
| + | DECLARE @currentExpansion INT = 0 | ||
| + | |||
| + | SELECT TOP 1 | ||
| + | @currentObjectId = [@FamilyTree].ObjectId | ||
| + | FROM | ||
| + | @FamilyTree | ||
| + | WHERE | ||
| + | [@FamilyTree].IsExpanded = 0 | ||
| + | |||
| + | WHILE @currentObjectId IS NOT NULL AND @currentExpansion < @maxExpansions | ||
| + | BEGIN | ||
| + | INSERT | ||
| + | @FamilyTree (ObjectId, TableName, IsExpanded, [Path]) | ||
| + | SELECT DISTINCT | ||
| + | ChildTable.object_id | ||
| + | , ChildTable.[name] | ||
| + | , 0 | ||
| + | , CONCAT ([@FamilyTree].[Path], | ||
| + | FROM | ||
| + | @FamilyTree | ||
| + | JOIN | ||
| + | sys.foreign_keys ForeignKey | ||
| + | ON ForeignKey.parent_object_id = [@FamilyTree].ObjectId | ||
| + | JOIN | ||
| + | sys.tables ChildTable | ||
| + | ON ChildTable.object_id = ForeignKey.referenced_object_id | ||
| + | WHERE | ||
| + | [@FamilyTree].ObjectId = @currentObjectId | ||
| + | |||
| + | UPDATE | ||
| + | @FamilyTree | ||
| + | SET | ||
| + | [@FamilyTree].IsExpanded = 1 | ||
| + | WHERE | ||
| + | [@FamilyTree].ObjectId = @currentObjectId | ||
| + | |||
| + | SELECT TOP 1 | ||
| + | @currentObjectId = [@FamilyTree].ObjectId | ||
| + | FROM | ||
| + | @FamilyTree | ||
| + | WHERE | ||
| + | [@FamilyTree].IsExpanded = 0 | ||
| + | |||
| + | SELECT @currentExpansion = @currentExpansion + 1 | ||
| + | END | ||
| + | |||
| + | SELECT DISTINCT | ||
| + | * | ||
| + | , CONCAT( | ||
| + | ' | ||
| + | , [@FamilyTree].TableName | ||
| + | , '] -- ' | ||
| + | , [@FamilyTree].[Path] | ||
| + | ) | ||
| + | FROM | ||
| + | @FamilyTree | ||
| + | ORDER BY | ||
| + | [@FamilyTree].[Path] DESC | ||
| </ | </ | ||
| Line 167: | Line 276: | ||
| </ | </ | ||
| - | ===== Determine the Data Types in Results of a Query===== | + | ===== Determine the Data Types in Results of a Query ===== |
| <code sql> | <code sql> | ||
| SELECT * FROM sys.dm_exec_describe_first_result_set( | SELECT * FROM sys.dm_exec_describe_first_result_set( | ||
| | | ||
| + | </ | ||
| + | |||
| + | ===== Sort out Collation ===== | ||
| + | |||
| + | <code sql> | ||
| + | -- Determine the default collation for the server. | ||
| + | SELECT serverproperty(' | ||
| + | |||
| + | -- Convert a varchar to a specific collation. | ||
| + | SELECT CAST(' | ||
| </ | </ | ||
| Line 214: | Line 333: | ||
| WHERE | WHERE | ||
| SourceObjects.object_id IS NULL OR NewObjects.object_id IS NULL | SourceObjects.object_id IS NULL OR NewObjects.object_id IS NULL | ||
| + | </ | ||
| + | |||
| + | ===== Export a .bacpac By Command Line ===== | ||
| + | |||
| + | Install SqlPackage: | ||
| + | < | ||
| + | dotnet tool install -g microsoft.sqlpackage | ||
| + | </ | ||
| + | |||
| + | Export .backpac. Only add the '' | ||
| + | < | ||
| + | SqlPackage / | ||
| </ | </ | ||
| Line 311: | Line 442: | ||
| </ | </ | ||
| - | ===== Display All Foreign Keys ===== | + | ===== Foreign Keys ===== |
| + | |||
| + | ==== Display Foreign Key Relationships for a Table ==== | ||
| + | |||
| + | Can do this, but it will only show which tables reference the specified table, not what tables the specified table references: | ||
| + | |||
| + | <code sql> | ||
| + | sp_fkeys TableName | ||
| + | </ | ||
| + | |||
| + | Better to do this, which lists all foreign key relationships and lots of other information as well: | ||
| + | |||
| + | <code sql> | ||
| + | sp_help TableName | ||
| + | </ | ||
| + | |||
| + | ==== Display All Foreign Key Info ==== | ||
| <code sql> | <code sql> | ||
| Line 377: | Line 524: | ||
| <code sql> | <code sql> | ||
| - | DECLARE @searchText NVARCHAR(MAX) = N' | + | DECLARE @searchText NVARCHAR(20) = N' |
| DECLARE @sql NVARCHAR(MAX) = N''; | DECLARE @sql NVARCHAR(MAX) = N''; | ||
| Line 543: | Line 690: | ||
| </ | </ | ||
| - | ===== Determining Foreign Key Relationships ===== | ||
| - | |||
| - | Can do this, but it will only show which tables reference the specified table, not what tables the specified table references: | ||
| - | |||
| - | <code sql> | ||
| - | sp_fkeys TableName | ||
| - | </ | ||
| - | |||
| - | Better to do this, which lists all foreign key relationships and lots of other information as well: | ||
| - | |||
| - | <code sql> | ||
| - | sp_help TableName | ||
| - | </ | ||
| ===== Creating a Login or User ===== | ===== Creating a Login or User ===== | ||
sql_cheat_sheet.1583289574.txt.gz · Last modified: 2020/03/05 03:39 (external edit)
