码迷,mamicode.com
首页 > 数据库 > 详细

sqlserver 创建游标

时间:2020-05-18 22:44:14      阅读:87      评论:0      收藏:0      [点我收藏+]

标签:varchar   dealloc   arc   service   --   创建   serve   pen   after   

游标主要是用于循环遍历

alter TRIGGER [dbo].[z_area_service_provider]
ON [dbo].[z_area]
AFTER UPDATE
AS
declare @id varchar(50)
declare @update_name varchar(255)
declare @service_provider_id varchar(255)
declare @col varchar(255)
select @id=id,@service_provider_id=service_provider_id,@update_name=update_by from inserted

if UPDATE(service_provider_id) and iSNULL(@service_provider_id,‘‘)!=‘‘
begin

select * into #t from [f_splitSTR]((select service_provider_id from z_area where id=@id ),‘,‘)
DECLARE #point_cursor CURSOR
FOR
select * from #t --每条信息从头到尾的写入                 -- 创建临时表
OPEN #point_cursor
FETCH NEXT FROM #point_cursor INTO @col
while @@fetch_status = 0
begin
if not exists(select * from z_area_provider where area_id =@id and service_provider_id=@col)
begin
insert into z_area_provider(id,create_date,create_by,service_provider_id,area_id)
values(NEWID(),GETDATE(),@update_name,@col,@id)
end
FETCH NEXT FROM #point_cursor INTO @col
END
CLOSE #point_cursor
DEALLOCATE #point_cursor
drop table #t                 删除临时表
end

sqlserver 创建游标

标签:varchar   dealloc   arc   service   --   创建   serve   pen   after   

原文地址:https://www.cnblogs.com/myxz/p/12913192.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!