Link to home
Start Free TrialLog in
Avatar of dotnet0824
dotnet0824

asked on

creating a small db script (.sql file)

how do i create this as database script .sql file. If I execute individually in query analzyer it works fine but if I execute it as bulk i get error
 ALTER TABLE [dbo].[Contacts]
   ALTER COLUMN  C_TicketFK  int  NULL
 
ALTER TABLE [dbo].[Contacts]
        ADD [Customer_ID] [int] NULL

Error
Column names in each table must be unique. Column name 'Customer_ID' in table 'dbo.Contacts' is specified more than once.
Avatar of jason_woods
jason_woods

You have extra brackets around your second "int"
 ALTER TABLE [dbo].[Contacts]
   ALTER COLUMN  C_TicketFK  int  NULL
 
ALTER TABLE [dbo].[Contacts]
        ADD [Customer_ID] int NULL

Open in new window

Avatar of dotnet0824

ASKER

hi,
I changed it. still if u select everything and execute as bulk u get the same error . If invidual block run then there is no problem.  I want to create and place this bulk in .SQL File to be run later
Have you tried using a semicolon ";" after each execution?
same error after this  INCREASED POINTS

 ALTER TABLE [dbo].[Contacts]
   ALTER COLUMN  C_TicketFK  int  NULL
 
ALTER TABLE [dbo].[Contacts]
        ADD [Customer_ID] int NULL;
SOLUTION
Avatar of jason_woods
jason_woods

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Avatar of Anthony Perkins
Anthony Perkins
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
acperkins is right. You need the GO command in MSSQL... Here it is without brackets or nulls (smaller file I guess)
ALTER TABLE dbo.Contacts
   ALTER COLUMN  C_TicketFK  int
 
GO
 
ALTER TABLE dbo.Contacts
   ADD Customer_ID int

Open in new window

acperkins is right. You need the GO command in MSSQL
Kind of depends on where you are executing it.  
Putting the semicolon at the end of each SQL statement is a very good practice to get in the habit of.  
I have a couple of SQL query tools that let me run as many SQL statements as I wish without the "GO" between them.  So, one pertinent question is, "What query tool are you using?"
Another would be, "Can you provide the table design prior to the attempted execution of the statements?
If you have gotten the second (Customer_ID) statement to execute once, you may be getting a valid message telling you that you've already created the column.  On the other hand, if you are deleting the Customer_ID column just so you can try to execute these statements over and over, perhaps you shoud further explain your real purpose . . . is it to learn how to set up the ALTERTABLE command so that you can script a whole series of changes?