Tuesday, March 20, 2012

ALTER TABLE DEFAULT

/* for the google index */
ALTER TABLE
DEFAULT COLUMN
DEFAULT VALUE

I've worked out several stored procedures for altering the default column
values in a table. They were compiled from books and code snippets found
here. It was a pain to work out so I've decided to share my work and
research here. This post is just my way of saying thanks to several others
here for posting with their wisdom and intelligence.

Michael
simpsonAT(dot)cts(dot)com

This procedure gets the constraint name. If you use the design view to
setup a default value, you won't know the system assigned constraint name.
This proc makes it an non issue. This code was gleened from this news
group.
CREATE PROCEDURE [DBO].[GetConstraintName]
(
@.tablename sysname,
@.columnName sysname,
@.constraintName sysname OUTPUT
)
as
SELECT
@.constraintName = o1.name
FROM
sysobjects o1
INNER JOIN
syscolumns c ON o1.id = c.cdefault
INNER JOIN
sysobjects o2 ON o1.parent_obj = o2.id
WHERE (o2.name = @.tablename) AND (c.name = @.columnName)

This procedure changes the default value for a column that is a numeric. It
uses the previously define stored procedure to get the constraint name. A
text version of this procedure can be created by removing the cast, defining
the input parameter "newConstraint" as varchar(255).

CREATE PROCEDURE [dbo].[ChangeIntConstraint]
(
@.tableName sysname,
@.columnName sysname,
@.newConstraint int
)
AS

Declare @.conName sysname

exec GetConstraintName @.tableName, @.columnName, @.constraintName = @.conName
OUT

declare @.sql nvarchar(1024)

set @.sql = 'ALTER TABLE ' + @.tableName + ' drop constraint ' + @.conName
exec(@.sql)

set @.sql = 'ALTER TABLE ' + @.tableName + ' ADD CONSTRAINT ' + @.conName + '
DEFAULT (' + CAST(@.newConstraint AS varchar(255)) + ') FOR ' + @.columnName
exec(@.sql)Why not make @.newConstraint character.
Then you can get the column type in the SP and cater for numeric and
character columns and dates.|||I am by no means an expert. If you want to post that change here, please do.
I think I know what you mean, but it would take even more time for me to
hack it out.

I did a Google news search on the subject. I saw that it had been asked
numerous times in the past. Some people responded with dropping and
recreating the table. I was just sharing what took me several hours to hash
out.

Michael

"Nigel Rivett" <sqlnr@.hotmail.com> wrote in message
news:7d6610e9.0311021918.19e6c409@.posting.google.c om...
> Why not make @.newConstraint character.
> Then you can get the column type in the SP and cater for numeric and
> character columns and dates.|||Haven't tested it but this sould be quite close. Change the constraint
value parameter to character

The relies on dates being passed in in a good format - e.g. 'yyyymmdd
hh:mm:ss'

declare @.datatype varchar(10)
select @.datatype = case when DATA_TYPE like '%char%' then 'char'
when DATA_TYPE like '%date%' then 'date'
else 'numeric'
from INFORMATION_SCHEMA.COLUMNS
where TABLE_NAME = @.tableName
andCOLUMN_NAME = @.columnName

select @.sql = 'ALTER TABLE ' + @.tableName
select@.sql = @.sql +' ADD CONSTRAINT ' + @.conName
if @.datatype = numeric
select@.sql = @.sql + ' DEFAULT (' + @.newConstraint + ')'
else
select@.sql = @.sql + ' DEFAULT (''' + @.newConstraint + ''')'
select@.sql = @.sql + ' FOR ' + @.columnName

Nigel Rivett
www.nigelrivett.net

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

No comments:

Post a Comment