我最近发现了一个问题,我希望使用sp_MSforeachtable存储过程来选择表名中包含单词Transcode的所有表,并在这些表上运行一些SQL。我设法编写了一些可行的代码,但并不完美 - 对于那些我希望能够优雅地跳过的表(即那些没有代码转换代码的表),它反而会抛出由于某些预期的列造成的错误(只有存在于转码表中)不存在于这些表上。问题似乎是调用存储过程时解析所有SQL,而不是只在需要时解析SQL(例如满足条件时)。sp_MSforeachtable - 解析动态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
'
这一次我得到的输出作为第一个为那些表名包含单词转码相同,但对于那些它并不代替见状忽略,我看到:
消息207,级别16,状态1,行9
无效的列名COL3
我敢肯定这是倒在动态SQL被解析的方式,但这是不受欢迎的行为。有没有人遇到过/有没有简单的解决方法?
这不是紧急的,因为在我的情况下,由于列不存在,错误与if语句具有相同的效果,并且有效的行能够成功运行,但是我希望在I需要尽快做类似的事情,这种行为会导致问题。
由于提前,
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
非常感谢亚伦 - 简单得多:)。 PS。我还发现另一个选项 - 嵌套动态SQL(Mikael Eriksson的答案在这里: http://stackoverflow.com/questions/9679997/sql-server-sp-msforeachtable-usage-to-select-only-those-tables-which- meet-some-c) - 但正如你指出的那样,如果proc不支持,最好不要使用。再次感谢,JB – JohnLBevan 2012-04-23 14:03:34