晨想

纯技术、纯兴趣

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
构造动态SQL,从游标中获取返回值

 

declare @TableName varchar(200)



set @TableName='TmpBaseCustomer_9'

declare @sql nvarchar(4000)
set @sql=N'
declare @RepeatStr varchar(1000)
declare @clName    varchar(200)
declare @total    int
set @RepeatStr=''''
declare curCustomer CURSOR for
select 名称,count(名称) as total from '+quotename(@TableName)+' group by 名称 having(count(*))>1
open curCustomer
fetch next from curCustomer into @clName,@total
while @@Fetch_Status=0
begin
    if (@total>1)
        begin
            set @RepeatStr=@RepeatStr+'',''+@clName+''有【''+cast(@total as varchar)+''】行''            
        end
    fetch next from curCustomer into @clName,@total
end
close curCustomer
deallocate curCustomer
select @nRepeatStr=@RepeatStr
'
declare @nRepeatStr varchar(1000)

exec sp_executesql @sql,
    N'@nRepeatStr varchar(1000) out',
    @nRepeatStr out

select @nRepeatStr





posted on 2008-11-20 08:32  超晨  阅读(2146)  评论(0编辑  收藏  举报