Solved

creating a small db script (.sql file)

Posted on 2009-04-14
8
226 Views
Last Modified: 2012-05-06
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.
0
Comment
Question by:dotnet0824
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 4

Expert Comment

by:jason_woods
ID: 24142298
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

0
 

Author Comment

by:dotnet0824
ID: 24142431
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
0
 
LVL 4

Expert Comment

by:jason_woods
ID: 24142445
Have you tried using a semicolon ";" after each execution?
0
Free Webinar: AWS Backup & DR

Join our upcoming webinar with experts from AWS, CloudBerry Lab, and the Town of Edgartown IT to discuss best practices for simplifying online backup management and cutting costs.

 

Author Comment

by:dotnet0824
ID: 24142782
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;
0
 
LVL 4

Assisted Solution

by:jason_woods
jason_woods earned 50 total points
ID: 24143182
You shouldn't have to declare Null and the semicolons will separate the two transactions.
ALTER TABLE dbo.Contacts
   ALTER COLUMN  C_TicketFK  int;
 
ALTER TABLE dbo.Contacts
   ADD Customer_ID int;

Open in new window

0
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 150 total points
ID: 24143782
Try it this way:

 ALTER TABLE [dbo].[Contacts]
   ALTER COLUMN  C_TicketFK  int  NULL
 
GO

ALTER TABLE [dbo].[Contacts]
        ADD [Customer_ID] int NULL;
0
 
LVL 4

Expert Comment

by:jason_woods
ID: 24143810
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

0
 
LVL 22

Expert Comment

by:8080_Diver
ID: 24144344
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?
0

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question