SELECT column1, column2, column3 FROM Table1
EXCEPT
SELECT column1, column2, column3 FROM Table2
or even
SELECT * FROM Table1
EXCEPT
SELECT * FROM Table2
SELECT column1, column2, column3 FROM Table1
EXCEPT
SELECT column1, column2, column3 FROM Table2
SELECT * FROM Table1
EXCEPT
SELECT * FROM Table2
DECLARE @tablename nvarchar(50)
DECLARE @column_name nvarchar(50)
SET @tablename = 'Products'
SET @column_name = 'ProductName'
SELECT INFORMATION_SCHEMA.TABLE_CONSTRAINTS.CONSTRAINT_NAME
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
INNER JOIN INFORMATION_SCHEMA.COLUMNS ON INFORMATION_SCHEMA.TABLE_CONSTRAINTS.TABLE_NAME = INFORMATION_SCHEMA.COLUMNS.TABLE_NAME
WHERE INFORMATION_SCHEMA.TABLE_CONSTRAINTS.TABLE_NAME =@tablename
AND INFORMATION_SCHEMA.COLUMNS.COLUMN_NAME=@columnname
DECLARE @database nvarchar(50)
DECLARE @table nvarchar(50)
SET @database = 'Northwind'
SET @table = 'Products'
DECLARE @sql nvarchar(255)
WHILE EXISTS(SELECT * FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE constraint_catalog = @database AND table_name = @table)
BEGIN
SELECT @sql = 'ALTER TABLE ' + @table + ' DROP CONSTRAINT ' + CONSTRAINT_NAME
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
WHERE constraint_catalog = @database and table_name AND @table
EXEC sp_executesql @sql
END
DECLARE @database nvarchar(50)
DECLARE @table nvarchar(50)
SET @database = 'Northwind'
SET @table = 'Products'
DECLARE @sql nvarchar(255)
WHILE EXISTS(SELECT * FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE constraint_catalog = @database AND table_name = @table)
BEGIN
SELECT @sql = 'ALTER TABLE ' + @table + ' NOCHECK CONSTRAINT ' + CONSTRAINT_NAME
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
WHERE constraint_catalog = @database AND table_name = @table
EXEC sp_executesql @sql
END
DECLARE @database nvarchar(50)
DECLARE @table nvarchar(50)
SET @database = 'Northwind'
SET @table = 'Products'
DECLARE @sql nvarchar(255)
SELECT @sql = 'ALTER TABLE ' + @table + ' NOCHECK CONSTRAINT ALL'
EXEC sp_executesql @sql
CREATE FUNCTION dbo.AuthorTitles ( @author varchar(100) )
RETURNS VARCHAR(8000)
AS
BEGIN
DECLARE @RES VARCHAR(8000)
SELECT @RES = COALESCE(@RES + ',','') + title
FROM titles as t
INNER JOIN titleauthor as ta ON t.title_id=ta.title_id
INNER JOIN authors as a on ta.au_id=a.au_id
WHERE a.au_lname = @author
RETURN (@RES)
END
SELECT DISTINCT a.au_lname,dbo.AuthorTitles(a.au_lname)
FROM authors as a
INNER JOIN titleauthor as ta ON a.au_id=ta.au_id
INNER jOIN titles as t ON ta.title_id=t.title_id
GROUP BY a.au_lname,t.title
SELECT DISTINCT t1.Name, t2.Name, t2.ZipCode, t1.ZipCodes
FROM Table1 as t1
INNER JOIN Table2 as t2 ON CHARINDEX(t2.ZipCode , t1.ZipCodes ) > 0
SELECT DISTINCT t1.Name, t2.Name, t2.ZipCode, t1.ZipCodes
FROM Table1 as t1
INNER JOIN Table2 as t2 ON PATINDEX('%33%' , t1.ZipCodes ) > 0
PATINDEX('%BC%', 'ABCD')
PATINDEX('AB%', 'ABCD')
CREATE PROCEDURE myProc
@TableName as VARCHAR(50),
@fieldlist as VARCHAR(100),
@ID as INT,
@Name as VARCHAR(50)
AS
DECLARE @sql VARCHAR(1000)
DECLARE @str NVARCHAR(1000)
SET @sql = 'INSERT INTO ' + @TableName + '(' + @fieldlist + ')' + ' Values(@ID,@Name)'
Select @str = CAST(@sql as NVarchar(1000))
EXECUTE sp_executesql @str,N'@ID INT,@Name VARCHAR(50)', @ID, @Name
GO
EXEC myProc 'Table1', 'ID, Name' ,1,'Taylor Lautner'
GO
CREATE FUNCTION dbo.getSecondTuesday
(@dt DATETIME)
RETURNS DATETIME
AS
BEGIN
DECLARE @firstDayOfMonth DATETIME
SET @firstDayOfMonth=DATEADD(mm, DATEDIFF(m, 0, @dt), 0)
DECLARE @dayWeek int
SET @dayWeek = DATEPART(DW,@firstDayOfMonth)
DECLARE @count int
SET @count = ((10 - @dayWeek) % 7) + 7
DECLARE @outputDate datetime
SET @outputdate = CONVERT(VARCHAR(10),DATEADD(day,@count,@firstDayOfMonth),101)
RETURN @outputdate
END
SELECT dbo.AddLeadingZero(@hour) + ':' + dbo.AddLeadingZero(@minute)
CREATE FUNCTION dbo.AddLeadingZero
(@val INT)
RETURNS VARCHAR(2)
AS
BEGIN
DECLARE @length INT
DECLARE @output VARCHAR(2)
SET @length=LEN(CAST(@val as varchar))
IF @length > 1
BEGIN
SET @output=CAST(@val as varchar)
END
ELSE
BEGIN
SET @output= '0' + CAST(@val as varchar)
END
RETURN @output
END
DECLARE @Rows INT
SET @Rows = 10
SELECT TOP ( @Rows ) *
FROM dbo.MyTable
CREATE FUNCTION dbo.fn_GetTopTableRecords(@id AS int, @count AS INT)
RETURNS TABLE
AS
RETURN
SELECT TOP(@count) *
FROM dbo.MyTable
WHERE ID = @id
ORDER BY Field1 DESC
GO
SELECT t1.ID,
t2.ID,
t2.Field1
FROM
dbo.Table1 as t1
CROSS APPLY
dbo.fn_GetTopTableRecords(ID, 5) AS t2
ORDER BY
t1.ID ASC, t2.Field1 DESC