mark防止以后要用到:分割字符串的函数:/*Created By:xumhCreated Date:2013-11-8Description:SQL分割字符串函数 @str 字符串 @splitchar 分隔符*/ALTER FUNCTION [dbo].[fn_sys_SplitStr](@str VARCHAR(8000),@splitchar VARCHAR(100)) RETURNS @temp TABLE(ID VARCHAR(100)) AS BEGIN DECLARE @ch AS VARCHAR(100) SET @str=@str+@splitchar WHILE(@str<>'') BEGIN SET @ch=LEFT(@str,CHARINDEX(@splitchar,@str,1)-1) INSERT @temp VALUES(@ch) SET @str=STUFF(@str,1,CHARINDEX(@splitchar,@str,1),'') END RETURN END进行游标的使用和字符串的分割ALTER PROCEDURE [dbo].[sp_ServiceHandoverIssues_SSApply2IS2SSCheck2Pool] @TaskID INTAS BEGIN DECLARE @error INT DECLARE @temp VARCHAR(50) DECLARE @Gridorder INT DECLARE @ProjectID NVARCHAR(50) DECLARE @ProjectNo NVARCHAR(50) DECLARE @ContractNo NVARCHAR(50) DECLARE @RectificationDate DATETIME DECLARE @IssuesItem NVARCHAR(50) DECLARE @IsComplete NVARCHAR(50) DECLARE @RectificationRemark NVARCHAR(500) SET @Gridorder = 0 SET @error = 0 --申明游标为ContractNo DECLARE order_cursor CURSOR FOR SELECT ProjectID , ProjectNo , ContractNo , RectificationDate , IssuesItem FROM Form_ServiceHandover_DIssues4SSApply WHERE TaskID = @TaskID --打开游标-- OPEN order_cursor --开始循环游标变量-- FETCH NEXT FROM order_cursor INTO @ProjectID, @ProjectNo, @ContractNo, @RectificationDate, @IssuesItem WHILE @@FETCH_STATUS = 0 --返回被 FETCH语句执行的最后游标的状态-- BEGIN --数据目的表1安装监督反馈遗留项整改情况表Form_ServiceHandover_DIssues4IS-- INSERT INTO [dbo].[Form_ServiceHandover_DIssues4IS] ( TaskID , Gridorder , ProjectID , ProjectNo , ContractNo , RectificationDate , IssuesItem ) SELECT @TaskID , @Gridorder + ( ROW_NUMBER() OVER ( ORDER BY [ID] ) ) , @ProjectID , @ProjectNo , @ContractNo , @RectificationDate , [ID] FROM dbo.fn_sys_SplitStr(@IssuesItem, ',') --表Form_ServiceHandover_DIssues4SSCheck-- INSERT INTO [dbo].[Form_ServiceHandover_DIssues4SSCheck] ( TaskID , Gridorder , ProjectID , ProjectNo , ContractNo , IssuesItem ) SELECT @TaskID , @Gridorder + ( ROW_NUMBER() OVER ( ORDER BY [ID] ) ) , @ProjectID , @ProjectNo , @ContractNo , [ID] FROM dbo.fn_sys_SplitStr(@IssuesItem, ',') --表TB_ServiceHandover_IssuesPool-- INSERT INTO [dbo].[TB_ServiceHandover_IssuesPool] ( ProjectID , ProjectNo , ContractNo , IssuesItem ) SELECT @ProjectID , @ProjectNo , @ContractNo , [ID] FROM dbo.fn_sys_SplitStr(@IssuesItem, ',') SELECT @Gridorder = @Gridorder + COUNT(*) FROM dbo.fn_sys_SplitStr(@IssuesItem, ','); SET @error = @error + @@ERROR --记录每次运行sql后是否正确,0正确 FETCH NEXT FROM order_cursor INTO @ProjectID, @ProjectNo, @ContractNo, @RectificationDate, @IssuesItem --转到下一个游标,没有会死循环 END CLOSE order_cursor --关闭游标 DEALLOCATE order_cursor --释放游标 ENDGO
新闻热点
疑难解答