sql server - 遊標,查重

 

遊標

Declare @EntryTime  Nvarchar(100) --入職時間
Declare @EhrNo  Nvarchar(100) --Ehr編號
DECLARE CusPostCursor CURSOR FOR
select B.EntryTime,A.EhrNo from tableA A left join tableB B on A.EhrNo = B.EhrNo
--打開遊標 
OPEN CusPostCursor
--讀取遊標中的值 遍歷每條數據 獲取
fetch next from CusPostCursor into @EntryTime,@EhrNo
while @@fetch_status<>-1
 begin
--業務
Update tableA set EntryTime = @EntryTime where EhrNo = @EhrNo

 --讀取下個遊標的值
fetch next from CusPostCursor into @EntryTime,@EhrNo

end
--關閉遊標 
close CusPostCursor
--釋放遊標 
deallocate CusPostCursor

查詢重複數據

--單列查重

select * from test
where name in (select name from test group by name having count
(name) > 1)            

--多列查重

SELECT a.* FROM test a,(
SELECT name,code
FROM test 
GROUP BY name,code
HAVING COUNT(1)>1

) AS b
WHERE a.name=b.name AND a.code=b.code

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章