DB/MS-SQL

동적으로 원하는 개수만큼 행을 다루기(TOP, ROWCOUNT)

채윤아빠 2008. 7. 10. 16:48
728x90
반응형
SELECT TOP 20 * FROM HumanResources.Employee


DECLARE @top INT
SET
@top = 10
SELECT TOP(@top) * FROM HumanResources.Employee

-- set value
DECLARE @top INT
SET
@top = 10
SET ROWCOUNT @top
SELECT * FROM HumanResources.Employee
-- set value to return all rows
SET ROWCOUNT 0

-- update example
DECLARE @top INT
SET
@top = 10
UPDATE TOP(@top) HumanResources.Employee SET MaritalStatus = 'S' WHERE EmployeeID < 20


-- delete example
DECLARE @top INT
SET
@top = 10
DELETE TOP(10) HumanResources.Employee WHERE EmployeeID < 20


-- insert example
DECLARE @top INT
SET
@top = 10
INSERT TOP(@top) dbo.contact2
SELECT * FROM dbo.contact