Wednesday, March 7, 2012

Alter column to set default

I know that the correct syntax to set the default on a column in SQL Server 2005 is:

Alter Table <TableName> Add Constraint <ConstraintName> Default <DefaultValue> For <ColumnName>

But from what I can gather, the SQL-92 syntax is:

Alter Table <TableName> Alter Column <ColumnName> Set Default <DefaultValue>

This generates an error on SQL Server 2005.

Am I wrong about the standard syntax for this statement? If this is the standard, why doesn't SQL Server 2005 support it? I am trying to avoid code that will only work on certain database managers.

Thanks.

SQL Server in only entry level SQL-92 compliant. There are however some features that are full level and so on. The alter table syntax to add a default is not supported yet. If you want to use code that works on various database systems then it is best to stick to CREATE TABLE DDL with basic SQL-92 syntax. This will have a better chance of executing against more database systems. So define the defaults/constraints etc as part of the CREATE TABLE itself. You have to use ALTER TABLE on a case-by-case basis. The syntax differences are huge between ANSI SQL standard, SQL Server, Oracle and DB2 for various DDLs.

No comments:

Post a Comment