Hi,
I am new to SQL Server 2005. Please help
what is the problem with this code..?
ALTER TABLE AM_Master
ALTER COLUMN Last_Updated datetime NOT NULL DEFAULT getdate();
Go
The error is like
Msg 156, Level 15, State 1, Line 2
Incorrect syntax near the keyword 'DEFAULT'.As with the CHECK constraint, the expression for a DEFAULT constraint must be included within brackets.
Your example will then be:
ALTER TABLE AM_Master
ALTER COLUMN Last_Updated datetime NOT NULL DEFAULT (getdate());
Go|||Try this:
ALTER TABLE AM_Master
ALTER COLUMN Last_Updated datetime NOT NULL CONSTRAINT [DF_AM_Master] DEFAULT (getdate());
Go|||Indeed, as T'zozo suggested above, you may wish to name your constraints to make your physical schema easier to manage.
Regards,|||Thanks for your help...
but when i used the sql above following comments were returned;
1. ALTER TABLE AM_Master
ALTER COLUMN Last_Updated datetime NOT NULL DEFAULT (getdate());
Error Message
Msg 156, Level 15, State 1, Line 2
Incorrect syntax near the keyword 'DEFAULT'.
2.ALTER TABLE AM_Master
ALTER COLUMN Last_Updated datetime NOT NULL CONSTRAINT [DF_AM_Master] DEFAULT (getdate());
Error message:
Msg 156, Level 15, State 1, Line 2
Incorrect syntax near the keyword 'CONSTRAINT'.
... Could you please tell why this happens...?
2.|||I was mistaken. It happens occasionally. It appears that you cannot actually modify a DEFAULT definition of a table column using the alter command statement.
From the documentation:
ALTER COLUMN
Specifies that the named column is to be changed or altered. ALTER COLUMN is not allowed if the compatibility level is 65 or lower. For more information, see sp_dbcmptlevel (Transact-SQL).
The modified column cannot be any one of the following:
...
Associated with a default definition. However, the length, precision, or scale of a column can be changed if the data type is not changed.
Perhaps you can do this using Enterprise Manager.
Regards,|||Robert, Robert, Robert...
EM / SSMS are evil ;)
[removed, because it was rubbish]|||I edited my post after you made that reply, which is the reason why your post doesn't make much sense to people who never read my original message, in which I stated something about using Enterprise Manager to make DDL changes rather than searching for that elusive DDL definition.
That view came from a year or two ago when, using SQL Server, I realised that I could perform a task, relating to object manipulation, in Enterprise Manager but not using direct SQL.|||CREATE TABLE tableName (
columnName int
CONSTRAINT constraintName DEFAULT 1
)
GO
ALTER TABLE tableName
DROP CONSTRAINT constraintName
GO
ALTER TABLE tableName
ADD CONSTRAINT constraintName DEFAULT 2
FOR columnName
GO
DROP TABLE tableName
GO
No comments:
Post a Comment