一组具有动态 table 名称的动态 SQL 语句是否需要游标或第二个存储过程
Is a cursor or second stored procedure required for a set of dynamic SQL statements with dynamic table names
我想迭代一个临时文件 table,其中包含我从 sysobjects 中检索到的 table 行名称:
select s.name from sysobjects s where s.type='U'
我的温度 table 可能有:
#tab_name
myTable1
myTable2
myTable3
...
有了这些 table 个名字,我想从每个 table 中动态地 select:
select table_name from @myTable --each row's value
来自#tab_name
的每个值
换句话说,从我的一组具有 table 名称的行中,我有另一个语句在单独的 select 语句中动态使用该 table 名称。如果没有游标或第二个存储过程,这可能吗?我以为我可以直接使用 sysobjects 来完成,但是没有临时文件就不行 table.
我有这个,但我看不出有什么办法可以遍历我的 select 的结果 table:
create table #mytablename (app_name varchar(255))
insert into #mytablename
select s.name from sysobjects s where s.type='U'
declare @end int, @current int
declare @my_app nvarchar(255)
declare @sql nvarchar(max)
set @current=0
select app_name from #mytablename
select @end = @@ROWCOUNT from #mytablename
select @end --so I can see it
while @current < @end --need a cursor with fetch
begin
set @current=@current+1
--no way to traverse the rows one at a time, so it will pick the last row.
select @my_app = app_name from #mytablename
select @sql = 'select trim(substring(left(app_name,len(app_name)-7),10,100)) from ' + @my_app
exec(@sql)
end
它本身不相关,但不要关注“app_name”与“tab_name”。在我的例子中,table 是一个真实应用程序的名称,因此它可以满足我的需要。这就是我在 SQL 中操作字符串的原因。 table 名称包含我需要的数据。
我已经阅读了很多答案,但我特别想知道是否可以在没有游标或第二个存储过程的情况下执行此操作。
编辑:原始值为Excel 电子表格选项卡名称。当我进行批量导入时,它使每个选项卡都有一个 table 名称。每个工作表(现在 tables)中都有相同的数据,即其中每个 sheets/tables 都有统计信息的应用程序的名称。
我的计划是查询所有 tables,获取该应用程序名称并将该应用程序名称放在正确的统计信息旁边以识别它。
例如,
Sheet 1 在从工作表导入的行中有应用程序名称“我的应用程序”。每个电子表格都有这个。因此,每个导入的 table 都有这个。如果我可以读取每个 table,那么我就可以获得每个应用程序名称。
如果我做了联合,那么我必须从 40+ table 手动 select,但我想动态使用 table 名称。
当导入发生时,工作表的前几行包含伪 header 信息(不是真正的 header 可以成为列名的数据,只是应用程序名称、日期等数据, 时间等等。统计数据本身有 headers 但它们向下几行,我也必须修复它。
它使 table 和行看起来像:
Table1:
Columns: F1, F2, F3...
Rows: appname: my app, null, null...
1/1/2019, null, null
some other metadata, null, null
stat header, null, null
thedata, stat1, stat2...
thedata
thedata
Table2:
Columns: F1, F2, F3...
Rows: appname: app, null, null...
1/1/2018, null, null
some other metadata, null, null
stat header, null, null
thedata, stat1, stat2...
thedata
thedata
...
Table40:
Columns: F1, F2, F3...
Rows: appname: app, null, null...
1/1/2018, null, null
some other metadata, null, null
stat header, null, null
thedata, stat1, stat2...
thedata
thedata
我无法控制电子表格。 F1、F2等由导入过程给出。 Table1、2等是导入给的工作表的名称,它们有空格、美元符号、破折号、刻度线等。
在 SSIS 中处理可能更好。
编辑:
为简单起见,最终结果将是单个 table,其中包含 app_name,列名为“app_name”
New table:
app_name
myapp0
myapp1
myapp2
...
实际上,我会用 app_name 更新 table,做很多删除等等,所以我不想让它复杂化。如果我能从另一个 table 的结果(sysobjects)中看到如何动态使用 table 名称,那么我就可以完成剩下的工作。我不知道如何做动态 table name 部分,除非使用游标。
您可以在临时 table 中包含 identity column
作为 CURSOR
的替代方法。根据您的查询进行了少量修改,如下所示:
create table #mytablename (app_name varchar(255), count_loop int identity(1,1))
insert into #mytablename
select s.name from sysobjects s where s.type='U'
declare @max int, @current int
declare @my_app nvarchar(255)
declare @sql nvarchar(max)
set @current=1
select @max = MAX(count_loop) from #mytablename
while @current < @max
begin
select @my_app = app_name from #mytablename where count_loop = @current
select @sql = 'select trim(substring(left(app_name,len(app_name)-7),10,100)) from ' + @my_app
exec(@sql)
set @current=@current+1
end
我想迭代一个临时文件 table,其中包含我从 sysobjects 中检索到的 table 行名称:
select s.name from sysobjects s where s.type='U'
我的温度 table 可能有:
#tab_name
myTable1
myTable2
myTable3
...
有了这些 table 个名字,我想从每个 table 中动态地 select:
select table_name from @myTable --each row's value
来自#tab_name
的每个值换句话说,从我的一组具有 table 名称的行中,我有另一个语句在单独的 select 语句中动态使用该 table 名称。如果没有游标或第二个存储过程,这可能吗?我以为我可以直接使用 sysobjects 来完成,但是没有临时文件就不行 table.
我有这个,但我看不出有什么办法可以遍历我的 select 的结果 table:
create table #mytablename (app_name varchar(255))
insert into #mytablename
select s.name from sysobjects s where s.type='U'
declare @end int, @current int
declare @my_app nvarchar(255)
declare @sql nvarchar(max)
set @current=0
select app_name from #mytablename
select @end = @@ROWCOUNT from #mytablename
select @end --so I can see it
while @current < @end --need a cursor with fetch
begin
set @current=@current+1
--no way to traverse the rows one at a time, so it will pick the last row.
select @my_app = app_name from #mytablename
select @sql = 'select trim(substring(left(app_name,len(app_name)-7),10,100)) from ' + @my_app
exec(@sql)
end
它本身不相关,但不要关注“app_name”与“tab_name”。在我的例子中,table 是一个真实应用程序的名称,因此它可以满足我的需要。这就是我在 SQL 中操作字符串的原因。 table 名称包含我需要的数据。
我已经阅读了很多答案,但我特别想知道是否可以在没有游标或第二个存储过程的情况下执行此操作。
编辑:原始值为Excel 电子表格选项卡名称。当我进行批量导入时,它使每个选项卡都有一个 table 名称。每个工作表(现在 tables)中都有相同的数据,即其中每个 sheets/tables 都有统计信息的应用程序的名称。
我的计划是查询所有 tables,获取该应用程序名称并将该应用程序名称放在正确的统计信息旁边以识别它。
例如,
Sheet 1 在从工作表导入的行中有应用程序名称“我的应用程序”。每个电子表格都有这个。因此,每个导入的 table 都有这个。如果我可以读取每个 table,那么我就可以获得每个应用程序名称。
如果我做了联合,那么我必须从 40+ table 手动 select,但我想动态使用 table 名称。
当导入发生时,工作表的前几行包含伪 header 信息(不是真正的 header 可以成为列名的数据,只是应用程序名称、日期等数据, 时间等等。统计数据本身有 headers 但它们向下几行,我也必须修复它。
它使 table 和行看起来像:
Table1:
Columns: F1, F2, F3...
Rows: appname: my app, null, null...
1/1/2019, null, null
some other metadata, null, null
stat header, null, null
thedata, stat1, stat2...
thedata
thedata
Table2:
Columns: F1, F2, F3...
Rows: appname: app, null, null...
1/1/2018, null, null
some other metadata, null, null
stat header, null, null
thedata, stat1, stat2...
thedata
thedata
...
Table40:
Columns: F1, F2, F3...
Rows: appname: app, null, null...
1/1/2018, null, null
some other metadata, null, null
stat header, null, null
thedata, stat1, stat2...
thedata
thedata
我无法控制电子表格。 F1、F2等由导入过程给出。 Table1、2等是导入给的工作表的名称,它们有空格、美元符号、破折号、刻度线等。
在 SSIS 中处理可能更好。
编辑:
为简单起见,最终结果将是单个 table,其中包含 app_name,列名为“app_name”
New table:
app_name
myapp0
myapp1
myapp2
...
实际上,我会用 app_name 更新 table,做很多删除等等,所以我不想让它复杂化。如果我能从另一个 table 的结果(sysobjects)中看到如何动态使用 table 名称,那么我就可以完成剩下的工作。我不知道如何做动态 table name 部分,除非使用游标。
您可以在临时 table 中包含 identity column
作为 CURSOR
的替代方法。根据您的查询进行了少量修改,如下所示:
create table #mytablename (app_name varchar(255), count_loop int identity(1,1))
insert into #mytablename
select s.name from sysobjects s where s.type='U'
declare @max int, @current int
declare @my_app nvarchar(255)
declare @sql nvarchar(max)
set @current=1
select @max = MAX(count_loop) from #mytablename
while @current < @max
begin
select @my_app = app_name from #mytablename where count_loop = @current
select @sql = 'select trim(substring(left(app_name,len(app_name)-7),10,100)) from ' + @my_app
exec(@sql)
set @current=@current+1
end