CREATE
--ALTER
 PROC TopTitles(@count INT)
AS
SELECT TOP (@count) title FROM pubs..titles
ORDER BY ytd_sales DESC

TopTitles 3



select * from titles

TRUNCATE TABLE testtitles

CREATE TABLE testTitles
(
title_id VARCHAR(20)
)







INSERT TOP (5) INTO testTitles
SELECT title_id FROM titles ORDER BY Title


INSERT TOP (5) INTO pubs..testTitles
SELECT SUBSTRINg( name, 0, 20) FROM Production.product


select * from production.product


SELECT NEWID()

truncate table pubs..testTitles

select * from pubs..testTitles


loop:
INSERT TOP (5) INTO pubs..testTitles
SELECT SUBSTRINg( name, 0, 20) FROM Production.product P
WHERE NOT EXISTS SUBSTRINg( name, 0, 20)

select @@rowcount

--DROP TABLE TESTDATA
CREATE TABLE TestData
(
data int PRIMARY KEY,
NAME VARCHAR(MAX)
)

set nocount on

--truncate table testdata

DECLARE @rows INT
SET @rows = 1000000
WHILE @rows > 0
BEGIN
INSERT INTO TestData VALUES (@rows, 'asdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdf')
SET @rows = @rows - 1
END

SELECT COUNT(*) FROM testdata

CREATE TABLE TestData2
(
data int
)


INSERT TOP(10000) INTO TestData2
SELECT data from TestData


truncate table testdata2


select max(data) from testdata2

select * from testdata2

DECLARE @blockSize INT
SET @blockSize = 10
DECLARE @rcount INT
SET @rcount = 1
WHILE @rcount > 0
BEGIN
DELETE TestData WHERE data > 500
SET @rcount = @@ROWCOUNT
END

DECLARE @t TABLE (D INT)
INSERT INTO TestData 
OUTPUT inserted.data into @t
VALUES 
(1, 'asdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdf')
SELECT * FROM @t


create TABLE #T1
(ID INT,
DATA INT
)



INSERT Scratch.dbo.TestData
	OUTPUT INSERTED.id AS [Inserted id],
	INSERTED.data AS [Inserted data]
	into #T1
SELECT * FROM
(SELECT 3 AS D
UNION
SELECT 5 AS D
UNION
SELECT 23 AS D
UNION
SELECT 4 AS D
UNION
SELECT 1000 AS D
) AS A

SELECT * FROM #T1
