日期:2014-05-16  浏览次数:20554 次

工作日志五 数据库某个表利用存储过程状态校验处理

???? 存在三种状态的记录,一个是禁用的,一个是可用的,一个是已经删除的。

create procedure getCheckTableData?
(@tableName varchar(100),@oldIStatus varchar(100),@cSourceGUIDS varchar(6000),@data varchar(1000) output)
as
declare?
??????? @iStatus int,
??????? @newIStatus varchar(100),
??????? @cGUID varchar(18),
??????? @sql nvarchar(4000)
?
?? set @newIStatus ='iStatus'
?? begin
?? if(@oldIStatus <>'')
???? set @newIStatus=@oldIStatus
?? end
? set?? @cSourceGUIDS=@cSourceGUIDS+';'?
?? print @cSourceGUIDS
? while(@cSourceGUIDS<>'')??
?????? begin??
??????????????????????? set @cGUID=left(@cSourceGUIDS,charindex(';',@cSourceGUIDS,1)-1)

??????????????????????? if(@cGUID <> '')
????????????????????????? begin
???????set @sql = N'set @iStatus=(select '+@newIStatus+' from '+@tableName+' where cGUID='+''''+@cGUID+''''+')'
???????print @sql
???????exec sp_executesql @sql,N'@iStatus int output',@iStatus output
??????????????????????????? begin
??????????????????????????? if(@iStatus is null)
??????????????????????????????? set @iStatus = -1
????????????????????????????? end
???????set @data=@data+@cGUID+','+cast(@iStatus as varchar(12))+';'
??????????????????????? end
??????????????????????? set?? @cSourceGUIDS=stuff(@cSourceGUIDS,1,charindex(';',@cSourceGUIDS,1),'')??
?print @data
end??
?--print @data
?----调用
execute? getCheckTableData 'cm_material','iStatus','003guid;00sguid',data