Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 在运行时生成Select语句,并使用sp_MSforeachtable执行_Sql_Tsql_Sql Server 2005_Sp Msforeachtable - Fatal编程技术网

Sql 在运行时生成Select语句,并使用sp_MSforeachtable执行

Sql 在运行时生成Select语句,并使用sp_MSforeachtable执行,sql,tsql,sql-server-2005,sp-msforeachtable,Sql,Tsql,Sql Server 2005,Sp Msforeachtable,我有下表: ID TableName FieldName 1 tbl1 fieldA 1 tbl1 fieldB 2 tbl2 fieldC 2 tbl2 fieldD ddl如下所示 Declare @tblPlaceholder table(ID int,TableName Nvarchar(20),FieldName Nvarchar(20)) insert into @tblPlaceholder select 1,'tbl1',

我有下表:

ID  TableName   FieldName
1   tbl1    fieldA
1   tbl1    fieldB
2   tbl2    fieldC
2   tbl2    fieldD
ddl如下所示

Declare @tblPlaceholder table(ID int,TableName Nvarchar(20),FieldName Nvarchar(20))
insert into @tblPlaceholder 
    select 1,'tbl1','fieldA' union all select 1,'tbl1','fieldB' union all
    select 2,'tbl2','fieldC' union all select 2,'tbl2','fieldD'
select * from @tblPlaceholder
我需要做的是,在运行时,我需要用字段和表名构造查询,然后我需要执行它们

因此,在运行时,要形成的查询是

ID  Query
1    Select tbl1.fieldA,tbl1.fieldB from tbl1
2    Select tbl2.fieldC,tbl2.fieldD from tbl2
我的问题如下,效果很好

-- Step 1: Build the query and insert the same into a table variable
Insert into @t 
Select ID, Query = ' Select ' + FieldNames + ' from ' + TableName
From
(
Select ID,TableName, FieldNames = Stuff((select ',' + CAST(TableName as Nvarchar(20)) + '.' + CAST(FieldName as Nvarchar(20)) 
            from @tblPlaceholder t2 where t2.ID = t1.ID
            FOR XML PATH('')),1,1,'')
From @tblPlaceholder t1
Group By t1.ID,t1.TableName)X

/* output of step 1
select * from @t

ID  Query
1    Select tbl1.fieldA,tbl1.fieldB from tbl1
2    Select tbl2.fieldC,tbl2.fieldD from tbl2
*/


-- Step 2 : loop thru the ID and execute the queries
While (@i <= 2) -- since there are two id's.. this can even be configurable as max(id)
Begin
      SELECT @QueryList = (Select Query from @t where ID = @i)
      exec(@QueryList)    
      set @i  += 1
End
但我非常确信,即使是以更好的方式也可以做到这一点。我正在寻找sp_MSforeachtable,但我不确定我们是否可以这样做


你们能帮帮我吗。

我举了个例子。调整它以符合您的需要

DECLARE @mytable AS VARCHAR(100)
DECLARE @sql AS VARCHAR(1000)

        DECLARE mycursor CURSOR FOR
        SELECT name
        FROM sys.tables
        -- WHERE your conditions here!

        OPEN mycursor 
        FETCH NEXT FROM mycursor INTO @mytable

        WHILE @@FETCH_STATUS = 0

        BEGIN

        SET @sql = 'SELECT * FROM ' + @tablename -- Define the query string. replace with your code
        EXEC(@sql) -- execure the query
        FETCH NEXT FROM mycursor INTO @mytable

        END

        CLOSE mycursor
        DEALLOCATE mycursor

除非@tblPlaceholder为每个表包含一行,为每个表包含所有列,否则spmsforeachtable没有任何意义-它为数据库中的每个用户表执行一次给定的命令,这与您的代码看起来所做的不匹配。