对表名使用变量[duplicate]

时间:2021-09-07 23:40:47

Possible Duplicate:
how do find the number of rows in a table when the table name is in a variable?

可能重复:当表名在变量中时,如何查找表中的行数?

I need to find tables in a SQL Server database (2000) that contain one value for a column.

我需要在SQL Server数据库(2000)中查找包含列的一个值的表。

I can use the following query to output a list of possible candidate tables containing my_column:

我可以使用以下查询输出包含my_column的可能候选表的列表:

SELECT DISTINCT TABLE_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME = 'my_column'

What I would like to get at, is the following pseudo query with result:

我想得到的是以下伪查询结果:

select '$TABLE_NAME', count(*) from $TABLE_NAME where my_column = '12345'

table01 1
table02 5
table03 0
table04 3

Or more generally formulated: Is it possible to make the FROM-clause variable?

或者更普遍的表述:是否可以使FROM子句变量?

3 个解决方案

#1


3  

Only way it's possible is by using dynamic SQL:

只有这样才能使用动态SQL:

declare @stmt nvarchar(max), @value nvarchar(max)

select @stmt = isnull(@stmt + ' union all ', '') + '
    select ''' + TABLE_NAME +  ''', count(*) from ' +  TABLE_NAME + ' where my_column = @value' 
from INFORMATION_SCHEMA.COLUMNS
where COLUMN_NAME = 'my_column'

select @value = '12345'

exec sp_executesql
    @stmt = @stmt,
    @params = N'@value nvarchar(max)',
    @value = @value

update:
For SQL 2000 you can use nvarchar(4000) If you have really big number of tables, you can use temporary table + cursor:

更新:对于SQL 2000,您可以使用nvarchar(4000)如果您有非常多的表,则可以使用临时表+游标:

create table #Temp_Results (table_name nvarchar(128), cnt int)
declare @stmt nvarchar(4000), @value nvarchar(128)

declare t_cursor cursor local fast_forward for
    select 
        'select ''' + TABLE_NAME +  ''', count(*) from ' +  TABLE_NAME + ' where id = @value'
    from INFORMATION_SCHEMA.COLUMNS
    where COLUMN_NAME = 'name'

select @value = 1

open t_cursor
fetch t_cursor into @stmt
while @@fetch_status = 0
begin
    insert into #Temp_Results
    exec sp_executesql
        @stmt = @stmt,
        @params = N'@value nvarchar(128)',
        @value = @value

    fetch t_cursor into @stmt
end

close t_cursor
deallocate t_cursor

select * from #Temp_Results

#2


0  

You can use the sp_MSforeachtable undocumented stored procedure to make the FROM clause variable.Here is an article that shows how to use it: sp_msForEachTable examples

您可以使用sp_MSforeachtable未记录的存储过程来创建FROM子句变量。这是一篇展示如何使用它的文章:sp_msForEachTable示例

#3


0  

If I understand the question, you want a list of tables containing a given column name and a record count from each table, yes? If so

如果我理解了这个问题,你需要一个包含给定列名和每个表的记录数的表列表,是吗?如果是这样

select o.name as "Table Name", i.rowcnt as "Rows"
from sysobjects o
inner join sysindexes i on o.id = i.id
where i.indid in (0,1)
and o.id in
    (select distinct id from syscolumns where name = 'My_Column')
order by o.name

Requires that you have ability to query system tables.

要求您具有查询系统表的能力。

#1


3  

Only way it's possible is by using dynamic SQL:

只有这样才能使用动态SQL:

declare @stmt nvarchar(max), @value nvarchar(max)

select @stmt = isnull(@stmt + ' union all ', '') + '
    select ''' + TABLE_NAME +  ''', count(*) from ' +  TABLE_NAME + ' where my_column = @value' 
from INFORMATION_SCHEMA.COLUMNS
where COLUMN_NAME = 'my_column'

select @value = '12345'

exec sp_executesql
    @stmt = @stmt,
    @params = N'@value nvarchar(max)',
    @value = @value

update:
For SQL 2000 you can use nvarchar(4000) If you have really big number of tables, you can use temporary table + cursor:

更新:对于SQL 2000,您可以使用nvarchar(4000)如果您有非常多的表,则可以使用临时表+游标:

create table #Temp_Results (table_name nvarchar(128), cnt int)
declare @stmt nvarchar(4000), @value nvarchar(128)

declare t_cursor cursor local fast_forward for
    select 
        'select ''' + TABLE_NAME +  ''', count(*) from ' +  TABLE_NAME + ' where id = @value'
    from INFORMATION_SCHEMA.COLUMNS
    where COLUMN_NAME = 'name'

select @value = 1

open t_cursor
fetch t_cursor into @stmt
while @@fetch_status = 0
begin
    insert into #Temp_Results
    exec sp_executesql
        @stmt = @stmt,
        @params = N'@value nvarchar(128)',
        @value = @value

    fetch t_cursor into @stmt
end

close t_cursor
deallocate t_cursor

select * from #Temp_Results

#2


0  

You can use the sp_MSforeachtable undocumented stored procedure to make the FROM clause variable.Here is an article that shows how to use it: sp_msForEachTable examples

您可以使用sp_MSforeachtable未记录的存储过程来创建FROM子句变量。这是一篇展示如何使用它的文章:sp_msForEachTable示例

#3


0  

If I understand the question, you want a list of tables containing a given column name and a record count from each table, yes? If so

如果我理解了这个问题,你需要一个包含给定列名和每个表的记录数的表列表,是吗?如果是这样

select o.name as "Table Name", i.rowcnt as "Rows"
from sysobjects o
inner join sysindexes i on o.id = i.id
where i.indid in (0,1)
and o.id in
    (select distinct id from syscolumns where name = 'My_Column')
order by o.name

Requires that you have ability to query system tables.

要求您具有查询系统表的能力。