sql server 2008 저장 프로시저
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;
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
LetCode 326 Power of Three(3의 멱)(반복, Log 함수)제목의 뜻을 나는 사실 충분히 이해하지 못한다. 예를 들면 12는 도대체 될 수 있을까?아니면 3, 9, 27, 81 이런 거밖에 안 돼요?우선 간단한 귀환을 써 봅시다. 제출에 성공했습니다. 그러면 스스로 12를 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.