Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7834203
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T13:05:30+00:00 2026-06-02T13:05:30+00:00

I recently found an issue whereby I wanted to use the sp_MSforeachtable stored proc

  • 0

I recently found an issue whereby I wanted to use the sp_MSforeachtable stored proc to select all tables with the word Transcode in the table name, and to run some SQL on those tables. I managed to write some code which worked, but not perfectly – for those tables which I’d hoped it would gracefully skip over (i.e. those which did not have transcode in the name) it instead threw errors due to certain expected columns (which only exist in the transcode tables) not existing on those tables. The issue seems to be that all SQL is parsed when the stored proc is called, rather than parsing the SQL only when required (e.g. when a condition is met).

The following code works as expected:

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

However, when I then try to add functionality, I get errors from code which would never be run; e.g.

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
'

This time I get the same output as the first one for those where the tablename contains the word Transcode, but for those where it doesn’t instead of seeing Ignored, I see:

Msg 207, Level 16, State 1, Line 9

Invalid column name col3

I’m pretty sure this is down to the way the dynamic SQL is parsed, but it’s undesirable behaviour. Has anyone come across this before / is there a simple workaround?

This is not urgent as in my case thanks to the columns not existing the errors had the same effect as the if statement anyway, and the valid lines were able to run successfully, but I’m keen to learn in case I need to do something similar soon where this behaviour would cause issues.

Thanks in advance,

JB

ps. code to replicate this behaviour’s included below:

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T13:05:31+00:00Added an answer on June 2, 2026 at 1:05 pm

    For one, I recommend staying away from undocumented and unsupported procedures like sp_MSForEachTable. They can be changed or even removed from SQL Server at any time, and this specific procedure may have the same symptoms reported by many against sp_MSForEachDb. (See some background here and here.)

    Here is how I would do it:

    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;
    

    The nice thing about this is it’s easy to validate the output before executing.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recently found an interesting issue. When using SetEnvironmentVariable, I can use Process Explorer
I've recently found a very nonobvious solution for a Microsoft CryptoAPI issue. Because of
I have recently found a hole in my SELECT statement for a private messaging
The obvious answer is to use Charset.defaultCharset() but we recently found out that this
recently found an article in http://hashtwo.com/blog/integrating-a-file-browser-into-ckeditor-cakephp to integrate a file browser into ckeditor (file
I recently found myself writing a piece of code that executed a Core Data
I recently found an article online that told me about this: RewriteRule ^mock-up/([^/]+)/([^/]+) /mock-up/index.php?page=$1&section=$2
I recently found out about C# extension methods and wrote this one: /// <summary>
I recently found out about n-grams and the cool possibility to compare frequency of
I recently found this post . It basically says that Eclipse has a modified

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.