473,395 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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)
Jul 20 '05 #1
3 13531
Why not make @newConstraint character.
Then you can get the column type in the SP and cater for numeric and
character columns and dates.
Jul 20 '05 #2
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" <sq***@hotmail.com> wrote in message
news:7d**************************@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.

Jul 20 '05 #3
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
and COLUMN_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!
Jul 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Paul Sampson | last post by:
Hi, I'm trying to run the ALTER TABLE command using a dynamic string for the table, like so: DECLARE @TableName CHAR SET @TableName = 'Customers' ALTER TABLE @TableName ADD ...blah
1
by: Bruce | last post by:
Hi, I want to change the datatype of an existing column from char to varbinary. When I run the "Alter Table" statement, I get the following error message - Disallowed implicit conversion...
2
by: me | last post by:
I would like to add an Identity to an existing column in a table using a stored procedure then add records to the table and then remove the identity after the records have been added or something...
3
by: Rajesh Kumar Mallah | last post by:
Hi, Looks like alter table does not tells about the indexes it dropped PG version: 7.4.3 Regds mallah.
3
by: Kimi | last post by:
Hi, I'm trying to change a column on a DB2 table so that it has a default value but I cannot get it to work. Does anyone have any experience in this area. I'm trying with this statement: ...
0
by: spatik | last post by:
Hi, I have a tableevent] with a columnevent_description] as ntext and default value ''. Now, I want to change the column type to nvarchar(max) using this script, alter table alter column ...
6
by: Barry | last post by:
In sqlserver 2000 I have a UDF which works fine but I want to make a change to it. When I do an ALTER FUNCTION ... I get an error saying that I can't alter the function because it is referenced by...
11
by: raylopez99 | last post by:
Keep in mind this is my first compiled SQL program Stored Procedure (SP), copied from a book by Frasier Visual C++.NET in Visual Studio 2005 (Chap12). So far, so theory, except for one bug...
2
by: vijaialphonse | last post by:
i have a table which has 3 columns, one of three is set to default value 0. Now i have to change data type of that particular column and its default value using sql query. am working with sql server...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.