码迷,mamicode.com
首页 > 其他好文 > 详细

存储过程2

时间:2016-03-02 09:30:32      阅读:129      评论:0      收藏:0      [点我收藏+]

标签:

USE [H3]
GO
/****** Object: StoredProcedure [dbo].[sp_states_string] Script Date: 03/02/2016 08:37:50 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[sp_states_string]
(
@string NVARCHAR(4000)
)
AS
BEGIN
IF Object_id(‘Tempdb..##temp‘) IS NOT NULL
DROP TABLE ##temp

CREATE TABLE ##temp
(
instanceId varchar(100),
states varchar(100)
);
truncate table ##temp

IF Object_id(‘Tempdb..##temp2‘) IS NOT NULL
DROP TABLE ##temp2
CREATE TABLE ##temp2
(
instanceId varchar(100),
states varchar(100)
);
truncate table ##temp2


DECLARE @object_id nvarchar(500)
DECLARE @where nvarchar(4000)
DECLARE @i INT
DECLARE @len INT
SET @where = ‘SELECT SSOC.BusinessID,IC.State FROM [H3].[dbo].[I_SSOC_SSOC] SSOC Left outer join OT_InstanceContext IC ON SSOC.InstanceId = IC.ObjectID WHERE ‘
--print @string
IF (@string IS NULL) OR (LTRIM(@string) = ‘‘)
RETURN

WHILE CHARINDEX(‘,‘,@string) > 0
BEGIN
SET @len = LEN(@string)
SET @i = CHARINDEX(‘,‘, @string)
SET @object_id = LEFT(@string, @i-1)
SET @where = @where +‘SSOC.BusinessID=‘+‘‘‘‘+@object_id+‘‘‘‘+‘ or ‘
print @where
INSERT INTO ##temp(instanceId,states) VALUES (@object_id,6)
SET @string = RIGHT(@string, @len - @i)
END
SET @object_id = @string
SET @where = @where +‘SSOC.BusinessID=‘+‘‘‘‘+@object_id+‘‘‘‘
--print @where
INSERT INTO ##temp(instanceId,states) VALUES (@object_id,6)
--select * from ##temp
print @where
DECLARE @sql nvarchar(4000)
--select @sql = @where
--exec(@sql)
declare @s varchar(8000)
--声明一个游标,查询满足条件的数据

set @s=‘declare UpdateHKUNoCursor cursor for ‘+@where

exec(@s)
open UpdateHKUNoCursor --打开

declare @noToUpdate varchar(20) --声明一个变量,用于读取游标中的值
declare @noToUpdate2 varchar(20) --声明一个变量,用于读取游标中的值
fetch next from UpdateHKUNoCursor into @noToUpdate,@noToUpdate2

while @@fetch_status=0 --循环读取
begin
print @noToUpdate
print @noToUpdate2
INSERT INTO ##temp2(instanceId,states) VALUES (@noToUpdate,@noToUpdate2)
--update person set hku_no=dbo.GetExtUserHKUNo() where psn_code=@noToUpdate
fetch next from UpdateHKUNoCursor into @noToUpdate,@noToUpdate2
end

close UpdateHKUNoCursor --关闭

deallocate UpdateHKUNoCursor --删除


END

存储过程2

标签:

原文地址:http://www.cnblogs.com/maipaocai/p/5233539.html

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