What you want to see on DotNetFunda.com ?
DotNetFunda.Com Logo
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 49497 |  Welcome, Guest!   Register  Login
 Home > Forums > Sql Server > Alter Default set value for a column ...
Vasanthmvp

Alter Default set value for a column

Replies: 3 | Posted by: Vasanthmvp on 11/2/2012 | Category: Sql Server Forums | Views: 402 | Status: [Member] | Points: 10  


Hi All,
Can someone demonstrate me on
-- How to change the default set value of a column (Default constraint enforced on it) with an example in sql server 2008.
No links plz..

Regards,

Awesome Coding !! :)


Reply | Reply with attachment | Alert Moderator

 Responses below this adGet hundreds of .NET Tips and Tricks videos

 Replies

Pandians
Pandians  
Posted on: 11/2/2012 8:44:23 AM
Level: Silver | Status: [Member] [MVP] | Points: 50

Resolved

Check It Out!

/*Creating a sample table with Default constraint on it*/
Create Table Tbl_DefaultSample

(
ID Int Identity(1,1) Primary Key,
Name Varchar(100),
Gender Varchar(10) Default 'Male',
Age Int
)
Go
/*Using default value on "Gender" column*/
Insert Tbl_DefaultSample(Name,Age) Values('Aequea',25)

Go
/*
ID Name Gender Age
1 Aequea Male 25
*/
/*Explicitly inserting value on "Gender" column*/
Insert Tbl_DefaultSample(Name,Gender,Age) Values('Salino','Female',20)

Go

/*
ID Name Gender Age
1 Aequea Male 25
2 Salino Female 20
*/
/*DEFAULT constraint can not be changed. It has to be DROPPED and Recreated*/
SELECT Name FROM SYS.DEFAULT_CONSTRAINTS

WHERE parent_object_id = OBJECT_ID('Tbl_DefaultSample')
GO
/*Drop the Default constraint*/
ALTER TABLE Tbl_DefaultSample DROP CONSTRAINT DF__Tbl_Defau__Gende__14E61A24

GO
/*Creating Default constraint again with different value*/
ALTER TABLE Tbl_DefaultSample ADD CONSTRAINT DF__Tbl_Defau__Gende__14E61A24 DEFAULT 'Female' FOR Gender

GO
/*Using default value on "Gender" column*/
Insert Tbl_DefaultSample(Name,Age) Values('Setaceo',19)

Go

/*
ID Name Gender Age
1 Aequea Male 25
2 Salino Female 20
3 Setaceo Female 19
*/
You got any idea ?

Cheers
www.SQLServerbuddy.blogspot.com
iLink Multitech Solutions

Vasanthmvp, if this helps please login to Mark As Answer. | Reply | Alert Moderator 

Vasanthmvp
Vasanthmvp  
Posted on: 11/2/2012 8:52:44 AM
Level: Starter | Status: [Member] | Points: 25

Hello Sir,
Very good explanation.. Thanks.
I got the solution this way sir..
Is there any other way without dropping the constraint.. the way we alter a column.

Regards,

Awesome Coding !! :)

Vasanthmvp, if this helps please login to Mark As Answer. | Reply | Alert Moderator 

Saratvaddilli
Saratvaddilli  
Posted on: 11/3/2012 1:38:00 AM
Level: Bronze | Status: [Member] | Points: 25

Hi vasanthmvp good question ,
we can't alter directly with out dropping the default constraint on it
ALTER TABLE TableName ALTER COLUMN ColumnName DEFAULT (10)
Error message is displayed trying to execute this: "Incorrect syntax near the keyword 'DEFAULT'".
Mistake.

go through this link it might give you better idea
http://bypsoft.blogspot.in/2007/10/changing-default-column-values-sql.html

Thanks and Regards
V.SaratChand
Show difficulties that how difficult you are

Vasanthmvp, if this helps please login to Mark As Answer. | Reply | Alert Moderator 

Reply - Please login to reply


Click here to login & reply

About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/22/2013 7:12:02 AM