Click here to Skip to main content
15,920,217 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All


I am trying to add new column to existing table but i am getting error , i have in all possible ways

my code is
SQL
ALTER TABLE TRTemplate
ADD ( CreatedbyId bigint not null,
      ModifiedId bigint not null,
      CreatedDate date not null,
      ModifiedDate date not null);


and tryed 

ALTER TABLE TRTemplate
ADD column  CreatedbyId bigint not null,
ADD column  ModifiedId bigint not null,
ADD column  CreatedDate date not null,
ADD column   ModifiedDate date not null;


how to it please anybody help me.......
Posted
Updated 27-Oct-14 21:57pm
v2
Comments
Robert Welliever 28-Oct-14 3:56am    
I'm pretty sure you can only do one at a time:
ALTER TABLE dbo.TRTemplate ADD column CreatedbyId Bigint not null;
ALTER TABLE dbo.TRTemplate ADD column ModifiedId Bigint not null;
....
CHill60 28-Oct-14 4:00am    
You should always let us know what the actual error is

syntax error

SQL
ALTER TABLE TRTemplate
ADD CreatedbyId bigint null,
 ModifiedId bigint null,
 CreatedDate date null,
 ModifiedDate date null;


if you want to add not null to your column you have to truncate the table .
 
Share this answer
 
v4
Comments
Maciej Los 28-Oct-14 12:27pm    
+5!
King Fisher 29-Oct-14 0:16am    
Thank you. but I was right,I was Posted Exactly What Richard Deeming Posted Sir.Is anything wrong with That Answer?
Maciej Los 29-Oct-14 2:17am    
Sorry, King_Fisher. It's terrible mistake. I was sure i'm adding my solution, but i edited yours. I reject changes made by me.
King Fisher 29-Oct-14 2:33am    
its ok sir, i thought i was Wrong :)
Maciej Los 29-Oct-14 5:21am    
No, it's my fault. Forgive me, my Friend ;)
The SQL syntax for ALTER TABLE (http://www.w3schools.com/sql/sql_alter.asp[^]) doesn't allow for multiple lines. You have to do it line by line:
SQL
ALTER TABLE TRTemplate
ADD column  CreatedbyId bigint not null;
ALTER TABLE TRTemplate
ADD column  ModifiedId bigint not null;
ALTER TABLE TRTemplate
ADD column  CreatedDate date not null;
ALTER TABLE TRTemplate
ADD column  ModifiedDate date not null;
 
Share this answer
 
Comments
Maciej Los 28-Oct-14 12:28pm    
+5
Richard Deeming 28-Oct-14 12:45pm    
The MS SQL syntax[^] does allow you to add multiple columns in one statement.
The ALTER TABLE syntax for MS SQL Server[^] allows you to add multiple columns with a single statement. You just need to get the syntax right. :)

Your first code block is the closest - you just need to remove the parentheses from around the column list:
SQL
ALTER TABLE TRTemplate
ADD 
    CreatedbyId bigint not null,
    ModifiedId bigint not null,
    CreatedDate date not null,
    ModifiedDate date not null
;
 
Share this answer
 
Comments
Maciej Los 28-Oct-14 12:53pm    
5ed!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900