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 8215443
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T11:47:17+00:00 2026-06-07T11:47:17+00:00

I’m trying to alter multiple SQL Server 2008 R2 tables at one time. This

  • 0

I’m trying to alter multiple SQL Server 2008 R2 tables at one time.

This is my code:

use DatabaseName
go

Declare @SchemaUsed varchar(20) = 'dbo'

create table #Tables
(
  TableName varchar(100), Processed int
)

insert into #Tables 
  select top 1 table_name, 0  
  from INFORMATION_SCHEMA.TABLES 
  where TABLE_SCHEMA = @SchemaUsed
  and table_type = 'Base Table'
  and (TABLE_NAME like 'PM%' )
  ORDER BY TABLE_NAME

DECLARE @TableName varchar(max)
DECLARE @SQL varchar(max)

WHILE EXISTS (select top 1 'x' from #Tables where Processed = 0)
BEGIN
    SET @TableName = (select top 1 TableName from #Tables where Processed = 0)

  Set @SQL = 'ALTER TABLE ' + @SchemaUsed + '.' + @TableName + ' ADD [identityID]  bigint IDENTITY(1, 1) NOT NULL '
 -- Set @SQL =     '''' + @SQL + ''''
  Print @SQL
 EXEC  @SQL;

    update #Tables
    set Processed = 1
    where TableName = @TableName
END

drop table #Tables

I can’t get this to work to save my life and get the following error:

Lookup Error – SQL Server Database Error: The name ‘ALTER TABLE
dbo.PM1GTVLV ADD [identityID] bigint IDENTITY(1, 1) NOT NULL ‘ is not
a valid identifier.

I’ve also tried multiple string variations and using sp_executesql as well.

Can someone point out where I’ve gone wrong?

  • 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-07T11:47:19+00:00Added an answer on June 7, 2026 at 11:47 am

    Try

    DECLARE @SQL NVARCHAR(MAX);
    
    EXEC sp_executesql @SQL;
    

    Instead of EXEC @sql.

    As an aside, this is a much more usable version of the same code IMHO:

    DECLARE @SchemaUsed VARCHAR(20) = 'dbo';
    
    DECLARE @sql NVARCHAR(MAX) = N'';
    
    SELECT @sql += CHAR(13) + CHAR(10) + N'ALTER TABLE ' 
     + QUOTENAME(@SchemaUsed) + '.'
     + QUOTENAME(name) + ' ADD [identityID]
       BIGINT IDENTITY(1,1) NOT NULL;'
    FROM sys.tables
    WHERE SCHEMA_NAME([schema_id]) = @SchemaUsed
    AND name LIKE 'PM%';
    
    PRINT @sql;
    
    --EXEC sp_executesql @sql;
    

    Or even better:

    DECLARE @SchemaUsed VARCHAR(20) = 'dbo';
    
    DECLARE @sql NVARCHAR(MAX) = N'';
    
    SELECT @sql += CHAR(13) + CHAR(10) + N'ALTER TABLE ' 
     + QUOTENAME(@SchemaUsed) + '.'
     + QUOTENAME(name) + ' ADD [identityID]
       BIGINT IDENTITY(1,1) NOT NULL;'
    FROM sys.tables AS t
    WHERE SCHEMA_NAME([schema_id]) = @SchemaUsed
    AND name LIKE 'PM%'
    AND NOT EXISTS (SELECT 1 FROM sys.columns AS c
     WHERE [object_id] = t.[object_id]
     AND c.is_identity = 1);
    
    PRINT @sql;
    
    --EXEC sp_executesql @sql;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,

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.