sql server 2008 저장 프로시저

저장 프로 세 스: 사용자 정보 에 대한 관리 와 표시 작업 을 훨씬 쉽게 할 수 있 습 니 다.저장 프로 세 스 는 SQL 문장 과 선택 가능 한 제어 흐름 문장의 사전 컴 파일 집합 으로 하나의 이름 으로 저장 하고 하나의 단원 으로 처리 합 니 다.저장 프로 세 스 는 데이터베이스 에 저장 되 어 있 으 며, 응용 프로그램 이 호출 을 통 해 실행 할 수 있 으 며, 사용자 가 변 수 를 설명 하고, 조건 부 실행 및 기타 강력 한 프로 그래 밍 기능 을 사용 할 수 있 습 니 다.저장 프로 세 스 는 프로그램 흐름, 논리 및 데이터베이스 에 대한 조 회 를 포함 할 수 있다.그들 은 매개 변수, 출력 매개 변 수 를 받 아들 이 고 단일 또는 여러 결과 집합 을 되 돌려 주 며 값 을 되 돌려 줄 수 있다.
    SQL 문 구 를 사용 하 는 모든 목적 에서 저장 과정 을 사용 할 수 있 습 니 다. 다음 과 같은 장점 이 있 습 니 다.
    (1) 기능 이 강하 고 제한 이 적다.
    (2) 하나의 저장 과정 에서 일련의 SQL 문 구 를 실행 할 수 있다.
    (3) 자신의 저장 과정 에서 다른 저장 과정 을 인용 할 수 있 는데 이것 은 일련의 복잡 한 문 구 를 간소화 할 수 있다.
    (4) 저장 과정 은 생 성 할 때 바로 위 에서 컴 파일 되 기 때문에 하나의 SQL 구문 보다 빨리 실 행 됩 니 다.
    (5) 여러 개의 반환 값, 즉 여러 개의 출력 매개 변 수 를 가 질 수 있 고 SELECT 를 사용 하여 결과 집합 을 되 돌려 줄 수 있다.
 
    함수: 하나 이상 의 SQL 문장 으로 구 성 된 서브루틴 으로 코드 를 다시 사용 할 수 있 도록 봉인 할 수 있 습 니 다.사용자 정의 함수 에 많은 제한 이 있 고 많은 문 구 를 사용 할 수 없 으 며 많은 기능 이 실현 되 지 않 습 니 다.함 수 는 반환 값 을 직접 참조 하여 표 변수 로 기록 집합 을 되 돌려 줍 니 다.그러나 사용자 정의 함 수 는 전역 데이터베이스 상 태 를 수정 하 는 작업 을 수행 할 수 없습니다. 
 
복잡 한 저장 프로시저 예제:
 
다음은 관련 관 계 를 가 진 여러 장의 데이터 시트 를 복사 하 는 저장 과정 사용 방법 이다.
 
--    (     ,         )
/*
use test
go

drop proc updAndInsertTableDataValue;
1、     id ,                   ,       
2、     id          ,    ,     
3、         ,                  
*/
use TEST
go

create PROCEDURE  updAndInsertTableDataValue 
@tableId int
as
declare @tName varchar(100),@tUser int,@tState int,@tType int,@tDepType int,@tDepId int,@tUrl varchar(50),@tGType int,@tValidity date
declare @newTableId int,@newDataId int
--    id      ,    
select @tName=tableListName,@tUser=createUser,@tState=tableListState,@tType=tableType,@tDepType=tableDepType,@tDepId=departId
,@tUrl=tableImgUrl,@tGType=gradeType,@tValidity=validity
from CW_SELF_TABLE_LIST where tableListId=@tableId;
--       ,      
insert 
	into CW_SELF_TABLE_LIST(createDate,createUser,tableListName,tableListState,tableImgUrl,tableType,tableDepType,departId,tableMark,gradeType,validity)
	values(CURRENT_TIMESTAMP,@tUser,@tName+'('+CONVERT(varchar(100), GETDATE(), 23)+')' ,@tState,@tUrl,@tType,@tDepType,@tDepId,1,@tGType,@tValidity); 
--               
set @newTableId = @@identity
--            
update CW_SELF_TABLE_LIST set tableMark=0 where tableListId=@tableId;	

--******        ,         **********
declare tableDataCursor cursor 
	for select tableDataId,orderId,tableDataIntro,tableDataName,tableDataState,tableDataType
	from CW_SELF_TABLE_DATA where  tableListId=@tableId;
--    
open tableDataCursor
DECLARE @dataId int,@dOrder int,@dIntro varchar(150),@dName varchar(150),@dState int,@dType varchar(100)

FETCH NEXT FROM tableDataCursor INTO @dataId,@dOrder,@dIntro,@dName,@dState,@dType
while(@@FETCH_STATUS<>-1)
begin
if(@@FETCH_STATUS<>-2)
begin 
--     tableData  
insert into CW_SELF_TABLE_DATA(createDate,orderId,tableDataIntro,tableDataName,tableDataState,tableDataType,tableListId)
	values(CURRENT_TIMESTAMP,@dOrder,@dIntro,@dName,@dState,@dType,@newTableId)
--      id      
set @newDataId = @@IDENTITY

--***********            ************
declare tableValueCursor cursor
	for select orderId,tableState,tableValueIntro,tableValueName
	from cw_self_table_value where tableDataId=@dataId	
open tableValueCursor
declare @vOrder int ,@vState int ,@vIntro varchar(255),@vName varchar(50)

fetch next from tableValueCursor into @vOrder,@vState,@vIntro,@vName
while(@@FETCH_STATUS<>-1)
begin
if(@@FETCH_STATUS<>-2)
begin 
insert into CW_SELF_TABLE_VALUE(createDate,orderId,tableState,tableValueIntro,tableValueName,tableDataId)
	values(CURRENT_TIMESTAMP,@vOrder,@vState,@vIntro,@vName,@newDataId) 
end
fetch next from tableValueCursor into @vOrder,@vState,@vIntro,@vName
end
close tableValueCursor
deallocate tableValueCursor
end
FETCH NEXT FROM tableDataCursor INTO @dataId,@dOrder,@dIntro,@dName,@dState,@dType
end	
close tableDataCursor	
deallocate tableDataCursor
return @newTableId
go

--exec updAndInsertTableDataValue 15;

좋은 웹페이지 즐겨찾기