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

  • Home
  • SEARCH
  • 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 8476403
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T18:10:07+00:00 2026-06-10T18:10:07+00:00

I’m new to SQLServer scripting (normally being a C++ developer), and would really appreciate

  • 0

I’m new to SQLServer scripting (normally being a C++ developer), and would really appreciate a bit of assistance.

I am attempting to perform a “find and replace” update on all tables in a SQLServer database that contain a ‘PROJID’ column. I am really struggling to find a way to do this that doesn’t report to me:

Msg 207, Level 16, State 1, Line 1 Invalid column name ‘PROJID’.

The statement I am executing is:

EXEC
(
  'IF EXISTS(SELECT * FROM sys.columns WHERE name = N''PROJID'' AND Object_ID = Object_ID(N''' + @TableName + '''))' +
  ' BEGIN' +
  ' UPDATE ' + @TableName + 
  ' SET ' + @ColumnName + ' = REPLACE(' + @ColumnName + ',''' + @ReplaceIDStr + ''',''' + @FindIDStr + ''')' +
  ' WHERE ' + @ColumnName + ' LIKE ''' + @ReplaceIDStr + '''' + ' AND PROJID = ''1000''' +
  ' END'
)

I have also tried using:

'IF COL_LENGTH(''' + @TableName + ''',''PROJID'') IS NOT NULL' +

instead of the column-exist check above. This also still gives me the “Invalid Column Name” messages.

I would be happy to take the column-exist check outside of the ‘Exec’ statement, but I’m not sure how to go about doing this either.

  • 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-10T18:10:09+00:00Added an answer on June 10, 2026 at 6:10 pm

    Output the results of this query to text. Don’t forget to change the values of the variables! Take the result of this and run it.

    SET NOCOUNT ON
    
    DECLARE @ColumnName VARCHAR(200) = 'ReplaceColumn'
        , @ReplaceIdStr VARCHAR(200) = 'ExampleReplaceIdStr'
        , @FindIdStr VARCHAR(200) = 'ExampleFindIdStr'
    
    PRINT 'BEGIN TRAN'
    PRINT 'SET XACT_ABORT ON'
    
    SELECT
        'UPDATE ' + C.TABLE_NAME + CHAR(13)
        + 'SET ' + @ColumnName + ' = REPLACE(' + @ColumnName + ', ''' + @ReplaceIdStr + ''', ''' + @FindIdStr + ''')' + CHAR(13)
        + 'WHERE ' + @ColumnName + ' LIKE ''%' + @ReplaceIdStr + '%'' AND PROJID = ''1000''' + CHAR(13)
    FROM INFORMATION_SCHEMA.COLUMNS C
    WHERE C.COLUMN_NAME = 'PROJID'
    
    PRINT 'COMMIT TRAN'
    
    SET NOCOUNT OFF
    

    EDIT: Also, some reasoning: You said you want update all tables where they contain a column called PROJID. Your first query just says that if the table @TableName has a PROJID column, then update @ColumnName on it. But it doesn’t guarantee that it has @ColumnName on it. The query I gave doesn’t check that either, because I’m assuming that all tables that have PROJID also have @ColumnName. If that isn’t the case, let me know and I can update the answer to check that. That you’re getting an Invalid Column Name error points to @ColumnName not existing.

    Your query would have updated one table (@TableName) at most, whereas the one I gave you will update every table that has PROJID. I hope that’s what your going for.

    EDIT 2: Here is a version that would run it all at once:

    DECLARE @ColumnName VARCHAR(200) = 'Value'
        , @ReplaceIdStr VARCHAR(200) = 'ExampleReplaceIdStr'
        , @FindIdStr VARCHAR(200) = 'ExampleFindIdStr'
    
    
    DECLARE @Sql NVARCHAR(MAX)
    DECLARE UpdateCursor CURSOR FOR
    SELECT
        'UPDATE ' + C.TABLE_NAME
        + ' SET ' + @ColumnName + ' = REPLACE(' + @ColumnName + ', ''' + @ReplaceIdStr + ''', ''' + @FindIdStr + ''')'
        + ' WHERE ' + @ColumnName + ' LIKE ''%' + @ReplaceIdStr + '%'' AND PROJID = ''1000'''
    FROM INFORMATION_SCHEMA.COLUMNS C
    WHERE C.COLUMN_NAME = 'PROJID'
    
    OPEN UpdateCursor
    
    FETCH NEXT FROM UpdateCursor
    INTO @Sql
    
    WHILE @@FETCH_STATUS = 0
    BEGIN
        EXEC sp_executesql @Sql
    
        FETCH NEXT FROM UpdateCursor
        INTO @Sql
    END
    
    
    CLOSE UpdateCursor
    DEALLOCATE UpdateCursor
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Seemingly simple, but I cannot find anything relevant on the web. What is the
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example

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.