Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

Stored procedures are nested when one stored procedure calls another or executes managed code by

referencing a CLR routine, type, or aggregate. You can nest stored procedures and managed code
references up to 32 levels.

Using a SELECT statement with a searched CASE expression


USE AdventureWorks2008R2;
GO
SELECT ProductNumber, Name, 'Price Range' =
CASE
WHEN ListPrice = 0 THEN 'Mfg item - not for resale'
WHEN ListPrice < 50 THEN 'Under $50'
WHEN ListPrice >= 50 and ListPrice < 250 THEN 'Under $250'
WHEN ListPrice >= 250 and ListPrice < 1000 THEN 'Under $1000'
ELSE 'Over $1000'
END
FROM Production.Product
ORDER BY ProductNumber ;
GO

Query in sql that will return the column names for the table?

select * from information_schema.columns


where table_name = 'Foo'

How to remove duplicate rows from a table in SQL Server


1.
SELECT col1, col2, col3=count(*)
INTO holdkey
FROM t1
GROUP BY col1, col2
HAVING count(*) > 1
2.
SELECT DISTINCT t1.*
INTO holddups
FROM t1, holdkey
WHERE t1.col1 = holdkey.col1
AND t1.col2 = holdkey.col2
3.
SELECT col1, col2, count(*)
FROM holddups
GROUP BY col1, col2
4.
DELETE t1
FROM t1, holdkey
WHERE t1.col1 = holdkey.col1
AND t1.col2 = holdkey.col2
5.
INSERT t1 SELECT * FROM holddups
Following code is useful to delete duplicate records. The table must have
identity column, which will be used to identify the duplicate records. Table
in example is has ID as Identity Column and Columns which have duplicate data
are DuplicateColumn1, DuplicateColumn2 and DuplicateColumn3.

DELETE
FROM MyTable
WHERE ID NOT IN
(
SELECT MAX(ID)
FROM MyTable
GROUP BY DuplicateColumn1, DuplicateColumn2, DuplicateColumn3)

You might also like