_대문 | 방명록 | 최근글 | 홈피소개 | 주인놈
FrontPage › NewT-SQLFeaturesInSQLServer2011

Contents

[-]
1 with result sets
2 offset and fetch
3 throw in error handling
4 sequence
5 top over
6 참고자료


1 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
*/

2 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

3 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.

4 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

5 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;



댓글 남기기..
이름: : 오른쪽의 새로고침을 클릭해 주세요. 새로고침
EditText : Print : Mobile : FindPage : DeletePage : LikePages : Powered by MoniWiki : Last modified 2018-04-13 23:12:53

만나는 모든 사람들에게서 무엇인가를 배울 수 있는 사람이 현명한 사람이다. (탈무드)