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 [2015/06/09 00:14] – [Randomness] 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 83: | Line 106: | ||
| BestEmployee_CTE | BestEmployee_CTE | ||
| </ | </ | ||
| + | |||
| + | ===== Table Variable ===== | ||
| + | |||
| + | <code sql> | ||
| + | -- Use *only* if a nested SELECT or CTE won't do the job. | ||
| + | DECLARE @SpecialEmployees TABLE | ||
| + | ( | ||
| + | EmployeeID INT | ||
| + | ) | ||
| + | |||
| + | INSERT | ||
| + | @SpecialEmployees (EmployeeID) | ||
| + | SELECT | ||
| + | Employee.EmployeeID | ||
| + | FROM | ||
| + | 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 | ||
| + | </ | ||
| + | |||
| + | ===== Copy a Table ===== | ||
| + | |||
| + | <code sql> | ||
| + | -- Doesn' | ||
| + | CREATE TABLE | ||
| + | NewTable | ||
| + | AS | ||
| + | SELECT | ||
| + | * | ||
| + | FROM | ||
| + | SourceTable | ||
| + | -- Omit, if data is also to be copied. | ||
| + | WHERE | ||
| + | 1=2; | ||
| + | </ | ||
| + | |||
| ===== Alter a Table ===== | ===== Alter a Table ===== | ||
| Line 131: | Line 274: | ||
| ( | ( | ||
| ... | ... | ||
| + | </ | ||
| + | |||
| + | ===== Determine the Data Types in Results of a Query ===== | ||
| + | |||
| + | <code sql> | ||
| + | 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 138: | Line 298: | ||
| BACKUP DATABASE SourceDatabase | BACKUP DATABASE SourceDatabase | ||
| TO DISK = ' | TO DISK = ' | ||
| - | WITH COPY_ONLY | + | WITH COPY_ONLY, INIT |
| CREATE DATABASE NewDatabase | CREATE DATABASE NewDatabase | ||
| Line 159: | Line 319: | ||
| RESTORE DATABASE NewDatabase | RESTORE DATABASE NewDatabase | ||
| FROM DISK = ' | FROM DISK = ' | ||
| - | WITH REPLACE, | + | WITH REPLACE |
| - | MOVE ' | + | , MOVE ' |
| - | MOVE ' | + | , MOVE ' |
| + | |||
| + | -- Compare the databases. No results = no differences. | ||
| + | SELECT | ||
| + | * | ||
| + | FROM | ||
| + | SourceDatabase.sys.objects SourceObjects | ||
| + | FULL JOIN | ||
| + | NewDatabase.sys.objects NewObjects | ||
| + | ON NewObjects.object_id = SourceObjects.object_id | ||
| + | WHERE | ||
| + | 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 / | ||
| + | </ | ||
| + | |||
| + | ===== Close all database connections ===== | ||
| + | |||
| + | <code sql> | ||
| + | ALTER DATABASE DatabaseName SET SINGLE_USER WITH ROLLBACK IMMEDIATE | ||
| + | GO | ||
| + | |||
| + | -- Do this straight away, because it sucks if a database is left in single user mode. | ||
| + | ALTER DATABASE DatabaseName SET MULTI_USER | ||
| + | GO | ||
| + | </ | ||
| + | |||
| + | ===== Find stuff in all Databases ===== | ||
| + | |||
| + | <code sql> | ||
| + | -- Find a table in all databases. | ||
| + | sp_msforeachdb @command1=' | ||
| + | </ | ||
| + | |||
| + | <code sql> | ||
| + | -- Find a table column in all databases. | ||
| + | sp_msforeachdb @command1=' | ||
| + | </ | ||
| + | |||
| + | ===== Compare two Databases ===== | ||
| + | |||
| + | <code sql> | ||
| + | -- No results = no differences. This is only suitable for databases that have been duplicated *exactly*. | ||
| + | SELECT | ||
| + | * | ||
| + | FROM | ||
| + | SourceDatabase.sys.objects SourceObjects | ||
| + | FULL JOIN | ||
| + | TargetDatabase.sys.objects TargetDatabase | ||
| + | ON TargetDatabase.object_id = SourceObjects.object_id | ||
| + | WHERE | ||
| + | SourceObjects.object_id IS NULL OR TargetDatabase.object_id IS NULL | ||
| + | </ | ||
| ===== Most Recent ===== | ===== Most Recent ===== | ||
| Line 222: | Line 442: | ||
| </ | </ | ||
| - | ===== List 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> | ||
| SELECT | SELECT | ||
| - | | + | ForeignKey.[name] |
| - | , ForeignKey.TABLE_SCHEMA | + | , PrimaryTable.[name] |
| - | , ForeignKey.TABLE_NAME | + | , PrimaryColumn.[name] PrimaryColumn |
| - | , ForeignKey.COLUMN_NAME | + | , ForeignTable.[name] |
| - | , ReferentialConstraint.UNIQUE_CONSTRAINT_NAME PrimaryKey_Name | + | , ForeignColumn.[name] ForeignColumn |
| - | , PrimaryKey.TABLE_SCHEMA | + | |
| - | , PrimaryKey.TABLE_NAME | + | |
| - | , PrimaryKey.COLUMN_NAME | + | |
| - | , ReferentialConstraint.MATCH_OPTION | + | |
| - | , ReferentialConstraint.UPDATE_RULE | + | |
| - | , ReferentialConstraint.DELETE_RULE | + | |
| FROM | FROM | ||
| - | INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS ReferentialConstraint | + | sys.foreign_key_columns ForeignKeyColumn |
| JOIN | JOIN | ||
| - | INFORMATION_SCHEMA.KEY_COLUMN_USAGE | + | sys.foreign_keys |
| - | ON ForeignKey.CONSTRAINT_NAME | + | ON ForeignKey.object_id |
| JOIN | JOIN | ||
| - | INFORMATION_SCHEMA.KEY_COLUMN_USAGE PrimaryKey | + | sys.columns PrimaryColumn |
| - | ON PrimaryKey.CONSTRAINT_NAME | + | ON PrimaryColumn.object_id |
| + | AND PrimaryColumn.column_id = ForeignKeyColumn.referenced_column_id | ||
| + | JOIN | ||
| + | sys.tables PrimaryTable | ||
| + | ON PrimaryTable.object_id = ForeignKeyColumn.referenced_object_id | ||
| + | JOIN | ||
| + | sys.columns ForeignColumn | ||
| + | ON ForeignColumn.object_id = ForeignKeyColumn.parent_object_id | ||
| + | AND ForeignColumn.column_id = ForeignKeyColumn.parent_column_id | ||
| + | JOIN | ||
| + | sys.tables ForeignTable | ||
| + | ON ForeignTable.object_id = ForeignKeyColumn.parent_object_id | ||
| + | ORDER BY | ||
| + | PrimaryTable.[name] | ||
| + | , PrimaryColumn.[name] | ||
| + | , ForeignTable.[name] | ||
| + | , ForeignColumn.[name] | ||
| </ | </ | ||
| + | ===== Display All Table Column Info ===== | ||
| + | |||
| + | <code sql> | ||
| + | SELECT | ||
| + | [Schema].[name] | ||
| + | , [Table].[name] | ||
| + | , [Column].[name] | ||
| + | , [ColumnType].[name] | ||
| + | , [Column].max_length | ||
| + | , [Column].[precision] [Precision] | ||
| + | , [Column].is_nullable Nullable | ||
| + | FROM | ||
| + | sys.tables [Table] | ||
| + | JOIN | ||
| + | sys.schemas [Schema] | ||
| + | ON [Schema].schema_id = [Table].schema_id | ||
| + | JOIN | ||
| + | sys.columns [Column] | ||
| + | ON [Column].object_id = [Table].object_id | ||
| + | JOIN | ||
| + | sys.types [ColumnType] | ||
| + | ON [ColumnType].system_type_id = [Column].system_type_id | ||
| + | ORDER BY | ||
| + | [Schema].[name] | ||
| + | , [Table].[name] | ||
| + | , [Column].[name] | ||
| + | </ | ||
| + | |||
| + | ===== Search All Text in a Database ===== | ||
| + | |||
| + | <code sql> | ||
| + | DECLARE @searchText NVARCHAR(20) = N' | ||
| + | DECLARE @sql NVARCHAR(MAX) = N''; | ||
| + | |||
| + | SELECT @sql = @sql + | ||
| + | CONCAT( | ||
| + | ' | ||
| + | , [Table].[name] | ||
| + | , ''' | ||
| + | , [Column].[name] | ||
| + | , ''' | ||
| + | , [Table].[name] | ||
| + | , ' | ||
| + | , [Column].[name] | ||
| + | , '] AS [Data] FROM ' | ||
| + | , [Table].[name] | ||
| + | , ' WHERE [' | ||
| + | , [Table].[name] | ||
| + | , ' | ||
| + | , [Column].[name] | ||
| + | , '] LIKE '' | ||
| + | , @searchText | ||
| + | , ' | ||
| + | , CHAR(13) + CHAR(10) | ||
| + | , 'UNION ALL' | ||
| + | , CHAR(13) + CHAR(10) | ||
| + | ) | ||
| + | FROM | ||
| + | sys.columns AS [Column] | ||
| + | JOIN | ||
| + | sys.tables AS [Table] | ||
| + | ON [Table].object_id = [Column].object_id | ||
| + | JOIN | ||
| + | sys.systypes AS ColumnType | ||
| + | ON ColumnType.xtype = [Column].system_type_id | ||
| + | WHERE | ||
| + | [ColumnType].[name] IN (' | ||
| + | ORDER BY | ||
| + | [Table].[name] | ||
| + | , [Column].[name] | ||
| + | |||
| + | SELECT @sql = SUBSTRING(@sql, | ||
| + | EXEC (@sql)</ | ||
| + | |||
| + | ==== Alternative ==== | ||
| + | |||
| + | - Run the SQL below. | ||
| + | - Copy the results into the editor. | ||
| + | - Remove the last '' | ||
| + | - Append the bit at the beginning. | ||
| + | |||
| + | <code sql> | ||
| + | -- DECLARE @searchText NVARCHAR(100) = N' | ||
| + | SELECT | ||
| + | CONCAT( | ||
| + | ' | ||
| + | , [TABLE].[name] | ||
| + | , ''' | ||
| + | , [COLUMN].[name] | ||
| + | , ''' | ||
| + | , [TABLE].[name] | ||
| + | , ' | ||
| + | , [COLUMN].[name] | ||
| + | , '] COLLATE ' | ||
| + | , convert(NVARCHAR, | ||
| + | , ' AS [Data] FROM [' | ||
| + | , [TABLE].[name] | ||
| + | , '] WHERE [' | ||
| + | , [TABLE].[name] | ||
| + | , ' | ||
| + | , [COLUMN].[name] | ||
| + | , '] LIKE @searchText UNION ALL' | ||
| + | ) | ||
| + | FROM | ||
| + | sys.columns AS [COLUMN] | ||
| + | JOIN | ||
| + | sys.tables AS [TABLE] | ||
| + | ON [TABLE].object_id = [COLUMN].object_id | ||
| + | JOIN | ||
| + | sys.systypes AS ColumnType | ||
| + | ON ColumnType.xtype = [COLUMN].system_type_id | ||
| + | JOIN | ||
| + | sys.schemas AS [Schema] | ||
| + | ON [Schema].schema_id = [TABLE].schema_id | ||
| + | WHERE | ||
| + | [ColumnType].[name] IN (' | ||
| + | AND [TABLE].is_ms_shipped = 0 | ||
| + | AND [Schema].[name] = ' | ||
| + | ORDER BY | ||
| + | [TABLE].[name] | ||
| + | , [COLUMN].[name] | ||
| + | </ | ||
| ===== Randomness ===== | ===== Randomness ===== | ||
| Line 253: | Line 621: | ||
| SELECT LEFT(CAST(NEWID() AS VARCHAR(100)), | SELECT LEFT(CAST(NEWID() AS VARCHAR(100)), | ||
| + | |||
| + | -- Semi-random, | ||
| + | SELECT HashBytes(' | ||
| + | |||
| + | -- Same thing, but returns varchar(32) | ||
| + | SELECT CONVERT(VARCHAR(32), | ||
| </ | </ | ||
| Line 259: | Line 633: | ||
| <code sql> | <code sql> | ||
| EXEC sys.xp_fixeddrives | EXEC sys.xp_fixeddrives | ||
| + | </ | ||
| + | |||
| + | ===== Does file exist on SQL Server ===== | ||
| + | |||
| + | <code sql> | ||
| + | -- Just for general info | ||
| + | EXEC master.dbo.xp_fileexist N' | ||
| + | |||
| + | -- Part of a script that needs to make a decision. | ||
| + | DECLARE @result INT | ||
| + | EXEC master.dbo.xp_fileexist N' | ||
| + | IF @result = 1 | ||
| + | PRINT 'File exists.' | ||
| + | ELSE | ||
| + | PRINT 'File does not exist.' | ||
| + | </ | ||
| + | |||
| + | ===== Determine Contents of a Directory on SQL Server ===== | ||
| + | |||
| + | <code sql> | ||
| + | EXEC Master.dbo.xp_DirTree ' | ||
| </ | </ | ||
| Line 295: | 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 ===== | ||
| Line 358: | Line 740: | ||
| WHERE sys.objects.TYPE = ' | WHERE sys.objects.TYPE = ' | ||
| ORDER BY SortOrder | ORDER BY SortOrder | ||
| + | </ | ||
| + | |||
| + | ===== Get a database out of Single-User Mode ===== | ||
| + | |||
| + | <code sql> | ||
| + | EXEC sp_who | ||
| + | |||
| + | -- Find the spid of the connect that is connected to the database. | ||
| + | KILL 56 | ||
| + | |||
| + | ALTER DATABASE [DatebaseName] SET MULTI_USER | ||
| </ | </ | ||
sql_cheat_sheet.1433808858.txt.gz · Last modified: 2017/01/01 19:52 (external edit)
