티스토리 뷰
오래간만에 저장프로시져에서 커서를 써보려 머리를 굴려보다가
구글링을 통해 아래와 같은 소스를 얻을 수 있었다.
출처의 필자분 설명에 따르면
커서 사용시보다 빠르고 좋다고 한다.
/*
작성자 : MN2팀 최지선
작성일 : 2005-04-08
설명 : 해당고객의 내원정보를 보고, 가장최근의 치아
치료자료를 Update한다.
*/
Create Proc SPU_TblCustHistory_Update
@CustiD As
Integer,
@VisitiD As Integer
As
Declare @PositioniD As
Tinyint
Declare @DiagnosisCD As Char(2)
Declare @DiseaseCD As
Char(5)
Declare @ToothNo As Char(1)
Declare @TmpiD As Integer, @MaxiD As
Integer
--테이블 변수 선언
Declare @tmpTooths Table
( TmpiD int identity(1,1) not
null,
DiagnosisCD Char(2) Not Null,
DiseaseCD Char(5) Not
Null,
PositioniD Tinyint Not Null,
ToothNo Char(1) Not Null)
--테이블 변수에 내용 삽입
Insert Into @tmpTooths(DiagnosisCD, DiseaseCD, PositioniD, ToothNo)
Select
VT.DiagnosisCD, VT.DiseaseCD, VTT.PositioniD, VTT.ToothNo
From
TblVisitTreatNo VT
Inner Join TblVisitTreatNoTooth VTT On VTT.VisitiD =
Vt.VisitiD And VTT.TreatNo = VT.TreatNo
Where VT.VisitiD = @VisitiD
Order
By VT.TreatNo Desc
--테이블변수의 최소iD, 최대iD를 읽는다.
Select @TmpiD = Min(tmpiD), @MaxiD = Max(tmpiD) From @tmpTooths
While @TmpID <= @MaxiD --변수가 최대값이내일 때까지 실행
Begin
--1개 Row읽어들이고,
Select @DiagnosisCD = DiagnosisCD,
@DiseaseCD =
DiseaseCD,
@PositioniD = PositioniD,
@ToothNo = ToothNo
From
@tmpTooths
Where TmpiD = @TmpiD
--작업할 쿼리 실행
Delete TblCustToothHistory Where CustiD = @CustiD And
PositioniD = @PositioniD And ToothNo = @ToothNo
Insert Into TblCustToothHistory(CustiD, ToothNo, PositioniD, DiseaseCD,
DiagnosisCD)
Values(@CustiD, @ToothNo, @PositioniD, @DiseaseCD,
@DiagnosisCD)
--다음행을 읽어들이기위해 1증가
Set @TmpiD = @TmpiD + 1
End
Return 0
[출처] http://blog.naver.com/frogy95/80006155733
'SQL > MS-SQL' 카테고리의 다른 글
MS-SQL 2000 페이징 (0) | 2009.06.02 |
---|---|
사용자 정의 함수 (0) | 2009.04.22 |
MS-SQL 스키마 보기 쿼리 (0) | 2008.12.22 |
데이터베이스 연결문자열 (0) | 2008.12.08 |
명령줄 유틸 (Command Line Utility) (0) | 2008.11.19 |
- Total
- Today
- Yesterday
- centos
- macos
- sencha touch
- CSS
- JQuery
- ASP
- mssql
- IOS
- classic asp
- Docker
- IE
- Mac
- nodejs
- Android
- javascript
- JSON
- Wordpress
- 워드프레스
- iphone
- git
- Prototype
- 한글
- Debug
- laravel
- Linux
- API
- 안드로이드
- PHP
- nginx
- iis
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 | 31 |