473,848 Members | 1,801 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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)c ts(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].[GetConstraintNa me]
(
@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 "newConstra int" as varchar(255).

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

Declare @conName sysname

exec GetConstraintNa me @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(@newConstr aint AS varchar(255)) + ') FOR ' + @columnName
exec(@sql)
Jul 20 '05 #1
3 13575
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.goo gle.com...
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_SCH EMA.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
10586
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
21275
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 from data type char to data type varbinary, table 'test.dbo.testalter', column 'col1'. Use the CONVERT function to run this query.
2
21363
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 similar. here is a rough idea of what the stored procedure should do. (I do not know the syntax to accomplish this can anyone help or explain this? Thanks much, CBL
3
2294
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
4145
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: Alter Table x Alter column y default 0;
0
2793
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 nvarchar(max) null ; The event_description column was created with a default value ''. Now this constraint is not allowing me to alter the column type. It throws an exception, Msg 5074, Level 16, State 1, Line 1
6
7365
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 an object. Is there any way around this? I reference the UDF in over 100 tables, do I have to go to each table, remove the all references alter the function then edit each 100 tables again? How clumsy can it be? Barry
11
4111
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 (feature?) below. At some point I'm sure I'll be able to laugh about this, akin to forgeting a semi-colon in C/C++, but right now it's frustrating (time to sleep on it for a while). Problem-- For some reason I get the error when trying to save files...
2
23414
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 2005. am not getting any prob when i exec ALTER TABLE TABLE_NAME ADD COLUMN DATATYPE SIZE CONSTRAINT VALUE. but getting probs when exec ALTER TABLE TABLE_NAME ALTER COLUMN_NAME DATATYPE SIZE VALUE. Help me, thanx in advance
0
9893
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9738
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11001
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10664
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10722
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9500
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7894
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5919
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3175
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.