2012-11-28 3 views
1

이제 방금 기본 DML 항목 외부에서 저장 프로 시저 및 SQL 코드를 작성하는 방법을 배우기 시작했습니다. 최근에 나는 테이블 값 매개 변수를 발견했습니다. 나는 TVP를 만드는 스크립트를 발견했으나 제대로 작동하지만 두 가지가 이해가되지 않는다. 하나는 언제 사용합니까? TVP가 유익 할 때의 전형적인 현실 시나리오는 무엇입니까? 두번째, 다음 스크립트에서 beginend을 제거하면 어떻게됩니까? 그 키워드를 갖는 것과 그렇지 않은 것의 차이점은 무엇입니까? SQL 서버 2008 R2테이블 값 매개 변수의 프로덕션 용도

use [tempdb] 
--this is the database table that will be populated 
create table SampleTable 
(
id int not null identity (1,1) 
,SampleString varchar(50) 
,SampleInt int null 
) 
go 
--create the table data type 
create type dbo.SampleDataType as table 
(
SampleString varchar(50) 
,SampleInt int 
) 
go 
--the stored procedure takes the SampleDataType as an input parameter 
create proc SampleProc 
(
--accepts the TVP as the lone parameter 
--make sure that the TVP is readonly 
@Sample as dbo.SampleDataType readonly 
) 
as 
begin 
--we simply insert the values into the db table from the parameter 
insert into SampleTable(SampleString,SampleInt) 
select SampleString,SampleInt from @Sample 
end 
go 
--this is the sample script to test that the sproc worked 
declare @SampleData as dbo.SampleDataType 
insert into @SampleData(SampleString,SampleInt) values ('one',1); 
insert into @SampleData(SampleString,SampleInt) values ('two',2); 
select * from @SampleData 

답변

2

한 실제 사용 to parameterise an in clause입니다.

쿼리에 (x, y, z, ...)에 대한 필터가있는 경우 더 이상 쉼표로 구분 된 목록으로 전달한 다음이를 분할하는 등 one of the methods here에 더 이상 의존 할 필요가 없습니다.

BEGIN ... END에는 아무런 차이가 없습니다. 그것은 블록을 정의합니다. 예를 들어 IF 문 다음에이를 사용하여 여러 명령문을 하나의 논리 블록으로 그룹화 할 수 있습니다.

+0

행에서 여러 개의 'select'문 뒤에'go'를 추가하는 것과 비슷합니까? – wootscootinboogie

+0

@wootscootinboogie - 아니요. 'GO'는 일괄 구분 기호이며 클라이언트 도구에서만 이해합니다. –

+0

클라이언트 도구가 의미하는 바를 설명 할 수 있습니까? – wootscootinboogie

관련 문제