SQL server 2005 query not running
Before posting this question, I have tried so many things but that was not helpful for me. I want to rename the column of table at sql server 2005, following query I have run at sql server2005:
1) ALTER TABLE Details RENAME COLUMN AccountID TO UID; but it gives me the error: Incorrect syntax near the keyword 'COLUMN'.
2开发者_高级运维)I have added one new column in the table by query: ALTER TABLE Details ADD BID uniqueidentifier; and then I want to set the coulmn property to not null .
How can i do that?
Thanks in advance AS
Use sp_Rename 'TableName.Column', 'NewColumnName', 'COLUMN'
.
In order to do your second part of the question, you'll need to do:
ALTER TABLE myTable
ADD myColumn UNIQUEIDENTIFIER NOT NULL DEFAULT 'some default value'
If you don't want to specify a default value, you'll have to first create the column with NULL
. Once the column is created, you can then populate with your desired values and then re-alter the column to NOT NULL
.
1) Instead of using ALTER TABLE
, why not use sp_rename
? For example:
EXEC sp_rename 'Details.[AccountID]', 'title', 'UID'
2) You can use ALTER TABLE Details ALTER COLUMN BID NOT NULL
, but you'll probably want to specify a default value for it also, if the table already has data in it.
精彩评论