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 October 29th, 2004, 04:49 AM
Registered User
 
Join Date: Oct 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default Problem in ALTER TABLE statement

Hi All,

I am using an ALTER TABLE statement with using variables. e.g.
"
DECLARE @constrName varchar(255)

ALTER TABLE Test1 DROP CONSTRAINT @constrName
"
it is giving me the following error:
"Server: Msg 170, Level 15, State 1, Line 3
Line 3: Incorrect syntax near '@constrName'."

Is it because of i am using variable in alter stmt.

please guide me on the same. its very urgent. your help will be highly appriciated.

Regards,
Deepak


 
Old October 29th, 2004, 07:04 AM
Friend of Wrox
 
Join Date: Jun 2004
Posts: 449
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via MSN to r_ganesh76
Default

use
   Exec "ALTER TABLE Test1 DROP CONSTRAINT " + @constrName

Regards
Ganesh
 
Old October 29th, 2004, 07:09 AM
Friend of Wrox
 
Join Date: Jun 2004
Posts: 449
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via MSN to r_ganesh76
Default

Sorry
Exec 'ALTER TABLE Test1 DROP CONSTRAINT ' + @constrName

double quotes typo

Regards
Ganesh
 
Old November 1st, 2004, 12:09 AM
Registered User
 
Join Date: Oct 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Its not working. It is giving same error even i am hardcoding the values:
I wrote:
------------------------------------------------------------------------------------------------
Exec 'ALTER TABLE Test1 DROP CONSTRAINT DF_Test1_Val4'

Error:
Server: Msg 170, Level 15, State 1, Line 1
Line 1: Incorrect syntax near 'ALTER TABLE Test1 DROP CONSTRAINT DF_Test1_Val4'.
------------------------------------------------------------------------------------------------

I am trying to run the query using query analyzer. Later i will use it with the SQL Task in DTS Package.

 
Old November 1st, 2004, 07:51 AM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 625
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to jemacc
Default

You cannot use a variable name directly as a table name. You must use Dynamic SQL, like this: This example is for a trigger but can be used for your particular situation.

DECLARE @Table_Name varchar(70)
Declare @SQL Varchar(500)

DECLARE curTables CURSOR FOR
select name from sysobjects where type = 'u'

OPEN curTables

FETCH NEXT FROM curTables into @Table_Name
WHILE (@@FETCH_STATUS = 0)
  begin
    Set @SQL = 'Alter table ' + @Table_Name + ' Disable trigger all'
    EXEC (@SQL)
    FETCH NEXT FROM curTables into @Table_Name
  end
CLOSE curTables
DEALLOCATE curTables


Jaime E. Maccou
Applications Analyst





Similar Threads
Thread Thread Starter Forum Replies Last Post
ALTER TABLE CHECKING moooon MySQL 2 May 5th, 2006 01:56 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.