小编典典

sp_MSforeachtable-动态SQL的解析

sql

最近,我发现了一个问题,我想使用sp_MSforeachtable存储的proc选择表名称中带有Transcode单词的所有表,并在这些表上运行一些SQL。我设法写了一些有效但不能完美执行的代码-
对于那些我希望它能正常跳过的表(即那些名称中没有转码的表),它却由于某些预期的列而抛出了错误(仅存在于转码表中)不存在于这些表中。问题似乎是调用存储的proc时将解析所有SQL,而不是仅在需要时(例如,满足条件时)解析SQL。

下面的代码按预期工作:

exec sp_MSforeachtable '
print ''Table being tested: ?''
if exists (select 1 where ''?'' like ''%Transcode%'')
begin
    print ''    Do Something''
end
else
begin
    print ''    Ignored''
end
'

但是,当我尝试添加功能时,我从永远不会运行的代码中得到错误;例如

exec sp_MSforeachtable '
print ''Table being tested: ?''
if exists (select 1 where ''?'' like ''%Transcode%'')
begin
    print ''    Do Something''

    insert ? (col1, col2, col3)
    select col1, col2, 1
    from ?
    where col3 = 0

end
else
begin
    print ''    Ignored''
end
'

这次,对于那些表名包含单词Transcode的人,我得到与第一个相同的输出,但是对于那些没有看到而不是Ignored的人,我看到了:

消息207,第16级,州1,第9行

无效的列名col3

我很确定这取决于动态SQL的解析方式,但这是不受欢迎的行为。有没有人遇到过这个/是否有一个简单的解决方法?

这并不紧急,因为在我的情况下,由于不存在这些列,错误仍然与if语句具有相同的效果,并且有效行能够成功运行,但是我很想学习以防万一我需要做点什么很快就会在类似的地方出现此问题。

提前致谢,

JB

ps。复制此行为的代码如下:

create table DemoTranscode1 (id bigint identity(1,1) primary key clustered, col1 nvarchar(10) not null, col2 nvarchar(10)not null, col3 bit not null)
go
create table DemoTable1 (id bigint identity(1,1) primary key clustered, col1 nvarchar(10) not null, col2 nvarchar(10)not null)
go
create table DemoTranscode2 (id bigint identity(1,1) primary key clustered, col1 nvarchar(10) not null, col2 nvarchar(10)not null, col3 bit not null)
go
create table DemoTranscode3 (id bigint identity(1,1) primary key clustered, col1 nvarchar(10) not null, col2 nvarchar(10)not null, col3 bit not null)
go
insert DemoTranscode1
select 'example1', 'demo', 0
union select 'example2', 'demo', 0
union select 'example3', 'demo', 0
union select 'example4', 'demo', 0
insert DemoTable1 select col1, col2 from DemoTranscode1
insert DemoTranscode2 select col1, col2, col3 from DemoTranscode1
insert DemoTranscode3 select col1, col2, col3 from DemoTranscode1

阅读 183

收藏
2021-05-16

共1个答案

小编典典

首先,我建议不要使用未记录和不受支持的过程sp_MSForEachTable。可以随时更改它们,甚至可以从SQL
Server中删除它们,并且此特定过程可能具有许多人针对所报告的相同症状sp_MSForEachDb。(请参见此处此处的一些背景知识,以及他们无意在此处进行修复,记录或支持的证据。)

这是我的处理方式:

DECLARE @sql NVARCHAR(MAX);
SELECT @sql = N'';

SELECT @sql = @sql + 'INSERT ' 
  + QUOTENAME(SCHEMA_NAME([schema_id]))
  + '.' + QUOTENAME(name) + ' (col1, col2, col3)
  SELECT col1, col2, 1 FROM '
  + QUOTENAME(SCHEMA_NAME([schema_id]))
  + '.' + QUOTENAME(name)
  + ' WHERE col3 = 0;'
FROM sys.tables 
WHERE name LIKE '%Transcode%';

PRINT @sql;
-- EXEC sp_executesql @sql;

这样做的好处是在执行之前很容易验证输出。

2021-05-16