Sql server 游标内列中的SQL计数

Sql server 游标内列中的SQL计数,sql-server,tsql,Sql Server,Tsql,我们要做的是以下几点: declare @Count numeric (10) set @Count = 0 set @Count = (select count(*) from FILENAME where COLUMNNAME like '%e%') print(@tel) 我想在几个文件上完成,这些文件有很多列,并且有所有列的总数,所以我的想法是通过如下操作获得每个文件的列列表: select * from sys.columns where object_id = ( select

我们要做的是以下几点:

declare @Count numeric (10)
set @Count = 0
set @Count = (select count(*) from FILENAME where COLUMNNAME like '%e%')
print(@tel)
我想在几个文件上完成,这些文件有很多列,并且有所有列的总数,所以我的想法是通过如下操作获得每个文件的列列表:

select * from sys.columns 
where object_id = ( select object_id from sys.objects where name = 'FILENAME')
现在,我认为最简单的方法是使用游标,将@Counts加在另一个变量中。但我遇到的问题是,当COLUMNNAME位于游标中的变量中时,我只能(据我所知)进行如下查询:

    declare @SQL varchar(max)
    set @SQL = @SQL + 'set @Count = (select count(*) from FILENAME where ' 
+ @columnname + ' like ''%e%'' )'

set @tot_Count = @tot_Count + @Count
但正如我从阅读游标中的动态sql中了解到的,这是一个很大的问题。 虽然当我看到“print(sql)”时,查询似乎是正确的,但它在游标中似乎确实不起作用

有没有其他方法可以得到我想要的结果? 还是我做错了什么

作为参考,我的想法是:

DECLARE @columnname VARCHAR(50) 
DECLARE @tot_Count NUMERIC(10), @Count NUMERIC(10)
set @tot_Count = 0

DECLARE MyCursor CURSOR FOR  
    SELECT name 
    from sys.columns 
        where object_id = ( select object_id from sys.objects where name = 'FILENAME' )

open MyCursor
FETCH NEXT FROM MyCursor INTO @columnname

WHILE @@FETCH_STATUS = 0
BEGIN
    declare @SQL varchar(max)
    set @SQL =''
    set @Count = 0
    set @SQL = @SQL + 'set @teller = (select count(*) from FILENAME where ' + @columnname + ' like ''%e%'' )'
    print (@SQL)
    exec (@SQL)

    set @tot_Count = @tot_Count + @Count

    FETCH NEXT FROM MyCursor into @name
END

CLOSE MyCursor
DEALLOCATE MyCursor 

print (@tot_teller)

变量1:您可以使用动态变量代替光标操作

declare @qry varchar(max)
select @qry = isnull(@qry + ' union all', '') + ' select count(*) cnt from ' + object_name(object_id) + ' where ' + name + ' like ''%e%'''
from sys.columns
where object_id = object_id('YourTableName')

set @qry = 'select sum(cnt) from (' + @qry + ') q'
print @qry
exec (@qry)
它更短、更好,但exec限制为4000个字符

变体2:或者您可以使用光标,通过动态qry插入预先准备好的临时表

    if object_id('tempdb..#tmptbl') is not null drop table #tmptbl
    create table #tmptbl (cnt int not null)

    declare @colqry varchar(800)
    declare mycursor cursor for  
        select 'insert into #tmptbl (cnt) select count(*) cnt from ' + object_name(object_id) + ' where ' + name + ' like ''%e%'''
        from sys.columns
        where object_id = object_id('YourTableName')

    open mycursor
    fetch next from mycursor into @colqry

    while @@fetch_status = 0
    begin

        print @colqry
        exec (@colqry)

        fetch next from mycursor into @colqry
    end
    close mycursor
    deallocate mycursor 

    select sum(cnt) from #tmptbl

用你正在使用的数据库标记你的问题。游标不适合调整活动,我将发布一个关于你的动态需求的查询可能有一些小错误,不确定,请尝试看看是否有任何错误谢谢,这确实是我在寻找的
    if object_id('tempdb..#tmptbl') is not null drop table #tmptbl
    create table #tmptbl (cnt int not null)

    declare @colqry varchar(800)
    declare mycursor cursor for  
        select 'insert into #tmptbl (cnt) select count(*) cnt from ' + object_name(object_id) + ' where ' + name + ' like ''%e%'''
        from sys.columns
        where object_id = object_id('YourTableName')

    open mycursor
    fetch next from mycursor into @colqry

    while @@fetch_status = 0
    begin

        print @colqry
        exec (@colqry)

        fetch next from mycursor into @colqry
    end
    close mycursor
    deallocate mycursor 

    select sum(cnt) from #tmptbl