May 26, 2011 at 8:30 am
I have to create one table which has 4 columns like:
Col1, col2 , col3,col4
now i want that when i create table then at design time i want to do something that when ever i insert values in first 2 columns then the third column should populate with a values as col3=col1+2 and col4=col2+1 .
this value should be default even if i m not entering values in col3 and col4.
I think at design time i have to use default keyword but i don't know how to use it.
May 26, 2011 at 8:35 am
These are called computer columns. Here is bol reference.
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
May 26, 2011 at 11:00 am
Is this what you want?
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[T_computedCols](
[Col1] [int] NOT NULL,
[Col2] [int] NOT NULL,
[col3] AS ([col1]+[col2]),
[col4] AS ([col2]+(1))
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[T_computedCols] ADD CONSTRAINT [DF_T_computedCols_Col1] DEFAULT ((0)) FOR [Col1]
GO
ALTER TABLE [dbo].[T_computedCols] ADD CONSTRAINT [DF_T_computedCols_Col2] DEFAULT ((0)) FOR [Col2]
GO
Viewing 3 posts - 1 through 2 (of 2 total)
You must be logged in to reply to this topic. Login to reply