#title New T-SQL Features in SQL Server 2011 [[TableOfContents]] ==== with result sets ==== result set에 대한 data type을 조절할 수 있다. {{{ create procedure denali_withresultset as select 1 as no,'tsql' type, 'withresultset' as feature union all select 2 as no,'tsql' type, 'throw' as feature union all select 3 as no,'tsql' type, 'offset' as feature union all select 4 as no,'tsql' type, 'sequence' as feature go exec denali_withresultset with result sets ( ( no int, featuretype varchar(50), featurename varchar(50) ) ) --결과가 다음과 같이 나오게 함 /* no featuretype featurename 1 tsql withresultset 2 tsql throw 3 tsql offset 4 tsql sequence */ }}} ==== offset and fetch ==== 그뎌 생겼다. 다음의 예는 처음 10개의 row는 결과에 포함하지 않고, 11~ 15까지 5개 Row를 리턴한다. {{{ select productid, name from adventureworks.production.product order by name offset 10 rows fetch next 5 rows only }}} 즉, 다음의 예와 결과가 같다. {{{ select productid, name from ( select row_number() over(order by name) as rowid, productid, name from adventureworks.production.product ) x where rowid between 11 and 15 order by rowid }}} 처음 5개 row를 가져와라. {{{ select productid, name from adventureworks.production.product order by name --offset 10 rows fetch first 5 rows only }}} ==== throw in error handling ==== 다음과 같이 던지면 에러를 리턴한다. {{{ THROW 50001, 'Error message', 1; }}} ''Msg 50001, Level 16, State 1, Line 1 Error message'' 다음의 예를 참고하자. {{{ begin try select 'using throw' select 1 / 0 end try begin catch --throw error throw end catch }}} 결과는 다음과 같다. {{{ (1 row(s) affected) (0 row(s) affected) Msg 8134, Level 16, State 1, Line 3 Divide by zero error encountered. }}} ==== sequence ==== 구지 생겨야 하는지는 모르겠지만, oracle에 대한 win-back 을 위하여 저지른 짓인듯 하긴하다.. lead(), leg()나 만들어주지.. {{{ create sequence dbo.seq as int start with 1 increment by 1; }}} {{{ SELECT NEXT VALUE FOR dbo.Seq; SELECT NEXT VALUE FOR dbo.Seq; SELECT NEXT VALUE FOR dbo.Seq; --결과는 차례로 1,2,3 나온다. }}} 다음과 같이 사용할 수 있다. {{{ create table dbo.examp1 ( seq int not null, name varchar(50) not null ); create table dbo.examp2 ( seq int not null, name varchar(50) not null ); insert into dbo.examp1(seq, name) values(next value for dbo.seq, 'tom'); insert into dbo.examp2(seq, name) values(next value for dbo.seq, 'jerry'); select * from examp1 --결과 4, tom select * from examp2 --결과 5, jerry }}} ==== top over ==== 실행되는 코드인가?? 아직 확인해 봤다. {{{ SELECT TOP (3) OVER(PARTITION BY custid ORDER BY val DESC) orderid, orderdate, custid, empid, val FROM Sales.OrderValues; }}} {{{ SELECT TOP (3, PARTITION BY custid ORDER BY val DESC) orderid, orderdate, custid, empid, val FROM Sales.OrderValues; }}} ==== 참고자료 ==== * [http://www.sql-server-performance.com/articles/dba/tsql_sql_server_2011_features_p1.aspx New T-SQL Features in SQL Server 2011]