sqlserver 创建游标
Posted 铭毅小子
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sqlserver 创建游标相关的知识,希望对你有一定的参考价值。
游标主要是用于循环遍历
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 创建游标的主要内容,如果未能解决你的问题,请参考以下文章