Click here to Skip to main content
15,893,663 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i have a tables 'users' in which all user information is listed and a column named status whose data type is int.the other table is status in which only two columns i.e. statusid and statusname, the statusid is autoincremented and is referenced in the users table as foreign key. the statusname contains values as pending, approved etc.

Now i have to set the default value of status in users table depending on vlaue of Status table status id as pending as default value in user table

please help me
Posted

Hi
Goto Design view of the particular table (DATABASE-->Table-->Design) then look Default Valur or Binding in Column Properties. Here you can specify the default value.

Regards
Willington
 
Share this answer
 
Comments
hinaiqbal 23-Feb-13 1:00am    
you don't understand my point.... through using this it will set the default value for column but my default value comes from the other table that is stored there as default value.. is it possible to write the select query in the default value or binding because i tried but its not working:(
You need to create Default Constraint for that column (assuming statusid for pending is 1):
SQL
ALTER TABLE dbo.users ADD CONSTRAINT
DF_users_statusid DEFAULT 1 FOR statusid
 
Share this answer
 
Comments
hinaiqbal 23-Feb-13 1:04am    
thanks
HI,

You can try like this:

SQL
ALTER TABLE {TABLENAME}
ADD {COLUMNNAME} {TYPE} {NULL|NOT NULL}
CONSTRAINT {CONSTRAINT_NAME} DEFAULT {DEFAULT_VALUE}


Add Default Value to Existing Column

SQL
-- Add default to existing column DateOfHire:
ALTER TABLE [dbo].[Employees] ADD  DEFAULT (getdate()) FOR [DateOfHire]

-- Add default value to existing column IsTerminated
ALTER TABLE [dbo].[Employees] ADD  DEFAULT ((0)) FOR [IsTerminated]


Add Default Value with Create Table

SQL
CREATE TABLE [dbo].[Employees]
(
    [EmployeeID] [INT] IDENTITY(1,1) NOT NULL,
    [FirstName] [VARCHAR](50) NULL,
    [LastName] [VARCHAR](50) NULL,
    [SSN] [VARCHAR](9) NULL,
    -- Add default of zero
    [IsTerminated] [bit] NOT NULL DEFAULT ((0)) ,
    -- Add default of getdate()
    [DateAdded] [datetime] NULL DEFAULT (getdate()),
    [Comments] [VARCHAR](255) NULL,
    [DateOfHire] [datetime] NULL
)



Hope this will give you some idea...

Thanks
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900