日期:2014-05-18 浏览次数:20652 次
CREATE PROCEDURE FirstAudithingNews ( @TypeIDz nvarchar(500),--栏目ID字符串格式为(12,13,14,) @NID int, @UserID NVarChar(50), @times DateTime, @title NVarChar(200), @neirong Ntext --该字段含英文单引号 ) AS DECLARE @StartPoint int DECLARE @EndPoint int DECLARE @TypeId int Set @StartPoint=0 Set XAcT_ABORT ON Begin transaction Delete from LNews where NewsID=@NID while (@StartPoint < LEN(@TypeIDz)) Begin Set @EndPoint=CharIndex(',',@TypeIDz,@StartPoint) if(@EndPoint>0) Begin set @TypeId=cast(SUBSTRING(@TypeIDz,@StartPoint,@EndPoint-@StartPoint) as int) insert into Type_News(TypeID,NewsID) values(@TypeId,@NID) --插入 栏目——新闻关联 表 SET @StartPoint = @EndPoint+1 End else Break End --删除最后一个,因为最后一个后面没有逗号,所以在循环中跳出,需另外再删除 -- set @TypeId=cast(SUBSTRING(@TypeIDz,@StartPoint,LEN(@TypeIDz)-@StartPoint+1) as int) -- insert into Type_News(TypeID,NewsID) values(@TypeId,@NID) --//字符串格式为(12,13,14) UPDATE news set FirstUser=@UserID,FirstState=1,State=0,FirstAuditingTime=@times where id=@NID --修改基本信息表(News)里信息 insert into LNews(NewsID,title,content) values(@NID,@title,@neirong) --插入初审表信息 -- Delete from Lyulanbiao where NewsID=@NID --删除预览临时表里数据 commit transaction GO
------解决方案--------------------
1楼审题不认真,是在存储过程里呢
主要用while + charIndex.下面的代码手写.若有错误自行修改.
create proc splitstr @selectIDs varchar(300), AS BEGIN while CharIndex(',', @selectIDs)>0 begin update table set mater = left(@selectIDs, CharIndex(',',@selectIDs)-1) set @selectIDs = (right(@selectIDs, len(@selectIDs)-CharIndex(',',@selectIDs))) end update table set mater = @selectIDs END
------解决方案--------------------
你的揭贴率太低了,我都不太想答。
你这个问题:1是update table set mater = 456 每次只能确定一个值赋给master
2是如何将这些不同值456,98,556....一一的分别取出来。
请看我的博客,有相同的解决方法,希望能给你有所启发
http://blog.csdn.net/iloveyoubaby/archive/2008/09/09/2905214.aspx
------解决方案--------------------
declare @selectIDs varchar(100)
declare @i int
set @selectIDs = '456,98,556
set @i = charindex(',',@selectIDs)
while (1=1)
begin
if @i = 0
begin
print @selectIDs
break
end
else
begin
print substring(@selectIDs,0,@i)
set @selectIDs = substring(@selectIDs,@i+1,len(@selectIDs))
set @i = charindex(',',@selectIDs)
end
end
这样就OK了 我print 了出来 每个数
把print @selectIDs 换成
update table set mater = @selectIDs
print substring(@selectIDs,0,@i)
update table set mater = substring(@selectIDs,0,@i)
或换成其它逻辑 就好了
------解决方案--------------------
556 后我少写了个引号
declare @selectIDs varchar(100)
declare @i int
set @selectIDs = '456,98,556'
set @i = charindex(',',@selectIDs)
while (1=1)
begin
if @i = 0
begin
print @selectIDs
break
end
else
begin
print substring(@selectIDs,0,@i)