Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 22nd, 2007, 12:35 PM
Friend of Wrox
 
Join Date: Oct 2006
Posts: 114
Thanks: 0
Thanked 0 Times in 0 Posts
Default ALTER TABLE(s) within a LOOP

(For example) I want to add a "DateStamp" column to all of my tables, in one hit. The following query provides me with a list of those tables that are missing the column...

SELECT [NAME]
FROM sysobjects
WHERE [TYPE] = 'U'
AND id NOT IN
(
    SELECT SO.[ID]
    FROM sysobjects SO
    JOIN syscolumns SC ON SC.ID = SO.ID
    WHERE SC.[NAME] = 'DateStamp'
)


And I am aware that I can use this in the following format:

WHILE (SELECT COUNT(*)) > 0
BEGIN
  /*LOOP*/
END


In which I know how to word the "SELECT COUNT(*)" to create the loop and I also know the code the first part of the BEGIN to capture the table names into a variable, with:

SELECT TOP 1 @table = [name].....

But, that then doesn't work with:

ALTER TABLE @table ADD 'DateStamp', etc.,


Could someone point me in the right direction here, preferably for a simple solution (as I know that you can completely control a database from within the sys... tables, but I am cautious of how easy it would be to make a mistake there) ?

Regards,

Sean Anderson
__________________
Regards,

Sean Anderson
 
Old February 22nd, 2007, 12:40 PM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

You are probably going to have to use the execute statement, so:

exec "ALTER TABLE @table ADD 'DateStamp'"

this will evaluate the @table variable

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from planoie's profile^^
^^Modified text taken from gbianchi profile^^
================================================== =========
Technical Editor for: Professional Search Engine Optimization with ASP.NET
http://www.wiley.com/WileyCDA/WileyT...470131470.html

Discussion:
http://p2p.wrox.com/topic.asp?TOPIC_ID=56429
 
Old February 23rd, 2007, 05:43 AM
Friend of Wrox
 
Join Date: Oct 2006
Posts: 114
Thanks: 0
Thanked 0 Times in 0 Posts
Default

When I enter the following:

CREATE PROCEDURE ADD_FIELD (@table varchar(255), @field varchar(255), @type varchar(50)) AS
EXEC "ALTER TABLE @table ADD @field @type"


SQL Query Analyzer reports:

Cannot add rows to sysdepends for the current stored procedure because it depends on the missing object 'ALTER TABLE @table ADD @field @type'. The stored procedure will still be created.

When I execute the procedure with:

ADD_FIELD 'test', 'c2', 'varchar'

It reports:

Could not find stored procedure 'ALTER TABLE @table ADD @field @type'

I also tried the same procedure, but with single quotes and that wouldn't create the stored procedure at all.

It was closer with:

CREATE PROCEDURE ADD_FIELD (@table varchar(255), @field varchar(255), @type varchar(50)) AS
DECLARE @test varchar(255)
SELECT @test = 'ALTER TABLE ' + @table + ' ADD ' + @field + ' ' + @type
EXEC @test


which returned

Could not find stored procedure 'ALTER TABLE test ADD c2 varchar'

Regards,

Sean Anderson
 
Old February 23rd, 2007, 07:56 AM
Friend of Wrox
 
Join Date: Oct 2006
Posts: 114
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Voila!

It's amazing what you can stumble across. Here's the first test, but sure shows that it can be done.

DECLARE @statement nvarchar(255)

SELECT @statement = 'SELECT * FROM TEST'

EXECUTE sp_executesql @statement

If you want more on this, check Google for "sp_executesql" or Dynamic SQL.

Regards,

Sean Anderson
 
Old March 16th, 2007, 02:05 AM
Friend of Wrox
 
Join Date: May 2006
Posts: 246
Thanks: 0
Thanked 0 Times in 0 Posts
Default

http://www.sommarskog.se/dynamic_sql.html







Similar Threads
Thread Thread Starter Forum Replies Last Post
Help on 'Alter Table' command charuni Classic ASP Basics 0 April 14th, 2006 11:14 AM
Alter Table Permissions Colonel Angus SQL Server 2000 2 September 21st, 2005 07:29 AM
(oracle 8i)Alter Table <table> coalesce partition combo Oracle 3 October 13th, 2004 09:35 AM
ALTER TABLE Daniel Schaffer Access ASP 0 April 25th, 2004 12:47 PM
ALTER TABLE syntax barjah SQL Server 2000 1 October 29th, 2003 02:30 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.