在运行时构建 Select 语句并使用 sp_MSforeachtable 执行

发布于 2024-11-18 14:25:24 字数 1651 浏览 4 评论 0原文

我有下表:

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 但我不确定我们是否可以这样做?

你们能帮帮我吗?

I have the below table:

ID  TableName   FieldName
1   tbl1    fieldA
1   tbl1    fieldB
2   tbl2    fieldC
2   tbl2    fieldD

The ddl is as under

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

What I need to do is that, at runtime I need to construct the queries with the fields and the table names and henceforth I need to execute them.

So, at runtime the queries that are to be formed are

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

My query is as under and works great

-- 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

But I am pretty sure that it can be done even in a better way. I was looking for sp_MSforeachtable but I am not sure if we can do so?

Could you people please help me out.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

流绪微梦 2024-11-25 14:25:24

我举了一个例子。调整它以满足您的需求。

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

I made an example. Adjust it to match your needs.

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文