Update with SELECT
UPDATE
Table_A
SET
Table_A.col1 = Table_B.col1,
Table_A.col2 = Table_B.col2
FROM
Some_Table AS Table_A
INNER JOIN Other_Table AS Table_B
ON Table_A.id = Table_B.id
WHERE
Table_A.col3 = 'cool'
– via StackOverflow
Get start of current month
SELECT DATEADD(month, DATEDIFF(month, 0, CURRENT_TIMESTAMP), 0)
– via StackOverflow
Count unique values in a column
SELECT COUNT(DISTINCT col1) FROM table1
– via StackOverflow
Get unique values in a column and their counts
SELECT col1, COUNT(0) FROM table1 GROUP BY col1
– via StackOverflow
Look for a column name in all tables in a database
SELECT
TABLE_SCHEMA as SchemaName,
TABLE_NAME as TableName,
COLUMN_NAME as ColumnName
FROM
INFORMATION_SCHEMA.COLUMNS
WHERE
COLUMN_NAME LIKE '%<column_name>%';
Find tables with foreign keys pointing to a specific table
SELECT
FK.name AS foreign_key_name,
FK_TABLE.name AS foreign_key_table,
FK_COLUMN.name AS foreign_key_column,
REFERENCED_TABLE.name AS referenced_table,
REFERENCED_COLUMN.name AS referenced_column
FROM sys.foreign_keys FK
JOIN sys.objects FK_TABLE ON FK_TABLE.object_id = FK.parent_object_id
JOIN sys.objects REFERENCED_TABLE ON REFERENCED_TABLE.object_id = FK.referenced_object_id
JOIN sys.foreign_key_columns FKC ON FKC.constraint_object_id = FK.object_id
JOIN sys.columns FK_COLUMN ON FK_COLUMN.column_id = FKC.parent_column_id AND FK_COLUMN.object_id = FK.parent_object_id
JOIN sys.columns REFERENCED_COLUMN ON REFERENCED_COLUMN.column_id = FKC.referenced_column_id AND REFERENCED_COLUMN.object_id = FK.referenced_object_id
WHERE REFERENCED_TABLE.name = 'Table_Name';
Script foreign keys for a table (create and drop)
WITH ForeignKeyInfo AS (
SELECT
FK.[name] AS ForeignKeyName,
FK_SCHEMA.[name] AS ForeignKeySchema,
FK_TABLE.[name] AS ForeignKeyTable,
FK_COL.[name] AS ForeignKeyColumn,
REFERENCED_SCHEMA.[name] AS ReferencedSchema,
REFERENCED_TABLE.[name] AS ReferencedTable,
REFERENCED_COL.[name] AS ReferencedColumn,
FKC.constraint_column_id AS ColumnPosition
FROM sys.foreign_keys FK
JOIN sys.tables FK_TABLE ON FK_TABLE.object_id = FK.parent_object_id
JOIN sys.tables REFERENCED_TABLE ON REFERENCED_TABLE.object_id = FK.referenced_object_id
JOIN sys.schemas FK_SCHEMA ON FK_SCHEMA.schema_id = FK_TABLE.schema_id
JOIN sys.schemas REFERENCED_SCHEMA ON REFERENCED_SCHEMA.schema_id = REFERENCED_TABLE.schema_id
JOIN sys.foreign_key_columns FKC ON FK.object_id = FKC.constraint_object_id
JOIN sys.columns FK_COL ON FK_COL.object_id = FKC.parent_object_id AND FK_COL.column_id = FKC.parent_column_id
JOIN sys.columns REFERENCED_COL ON REFERENCED_COL.object_id = FKC.referenced_object_id AND REFERENCED_COL.column_id = FKC.referenced_column_id
WHERE REFERENCED_TABLE.name = 'Table_Name'
)
SELECT
ForeignKeyName,
'ALTER TABLE [' + ForeignKeySchema + '].[' + ForeignKeyTable + '] ' +
'ADD CONSTRAINT [' + ForeignKeyName + '] FOREIGN KEY ([' + ForeignKeyColumn + ']) ' +
'REFERENCES [' + ReferencedSchema + '].[' + ReferencedTable + '] ([' + ReferencedColumn + ']);'
AS ForeignKeyScript,
'ALTER TABLE [' + ForeignKeySchema + '].[' + ForeignKeyTable + '] ' +
'DROP CONSTRAINT [' + ForeignKeyName + '];' AS ForeignKeyDropScript
FROM ForeignKeyInfo
ORDER BY ForeignKeyName, ColumnPosition;
“Arithmetic overflow error converting expression to data type int.” for big SUMs
Convert the field to BIGINT on the fly:
-- instead of
SUM(SomeField)
--use
SUM(CAST(SomeField as bigint))
– via StackOverflow
“SELECT * INTO” syntax that I keep forgetting
SELECT
select_list
INTO
destination
FROM
source
[WHERE condition]
– via SQLServerTutorial
Find all tables referencing a specific table/column
SELECT
FKTable.[name] AS ForeignKeyTable,
FKCol.[name] AS ForeignKeyColumn
FROM
sys.tables AS PKTable
JOIN
sys.columns AS PKCol ON PKTable.[object_id] = PKCol.[object_id]
JOIN
sys.foreign_key_columns AS FK ON PKTable.[object_id] = FK.[referenced_object_id]
AND PKCol.[column_id] = FK.[referenced_column_id]
JOIN
sys.tables AS FKTable ON FKTable.[object_id] = FK.[parent_object_id]
JOIN
sys.columns AS FKCol ON FKTable.[object_id] = FKCol.[object_id]
AND FK.[parent_column_id] = FKCol.[column_id]
WHERE
PKTable.[name] = '<Your_Table>'
AND PKCol.[name] = 'Id'
ORDER BY
ForeignKeyTable,
ForeignKeyColumn;