October 1, 2012 at 1:18 am
Hi Guys,
I have created a Default Constraint for my table. But this is not reflected on my table.
EX: Column ImportDate has a Datetime Datatype with NOT NULL Constraint.
I have created a Default Constranit for this as GETDATE()
. But while checking the query this Constraint not getting reflected on my table.
Every time it shows NULL.
Can any one help on this?
October 1, 2012 at 2:54 am
Can you post the definition of the table? This will make it clear.
Select the table, 'Script table as' > 'Create to' > 'new query editor window' & post the SQL.
October 1, 2012 at 3:32 am
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[tblGen](
[PROCESS_ID] [bigint] NOT NULL,
[Gen_FLAG] [bit] NOT NULL CONSTRAINT [DF_FLAG] DEFAULT ((0)),
[Gen_DATE] [datetime] NOT NULL CONSTRAINT [DF_DATE] DEFAULT (getdate())
) ON [PRIMARY]
October 1, 2012 at 3:44 am
I can't see ImportDate there?
October 1, 2012 at 3:49 am
laurie-789651 (10/1/2012)
I can't see ImportDate there?
You could see Gen_DATE has the import date. Please let me know if any.
Thanks.
October 1, 2012 at 3:55 am
It looks OK to me:
You couldn't get NULL in Gen_DATE when it's defined as NOT NULL.
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF OBJECT_ID('dbo.tblGen') IS NOT NULL
DROP TABLE dbo.tblGen;
CREATE TABLE [dbo].[tblGen](
[PROCESS_ID] [bigint] NOT NULL,
[Gen_FLAG] [bit] NOT NULL CONSTRAINT [DF_FLAG] DEFAULT ((0)),
[Gen_DATE] [datetime] NOT NULL CONSTRAINT [DF_DATE] DEFAULT (getdate())
) ON [PRIMARY]
insert into dbo.tblGen ( PROCESS_ID ) values ( 6703 ) ;
select * from dbo.tblGen;
/*
PROCESS_ID Gen_FLAG Gen_DATE
-------------------- -------- -----------------------
6703 0 2012-10-01 10:52:51.937
(1 row(s) affected)
*/
October 1, 2012 at 3:58 am
sqlusers (10/1/2012)
Hi Guys,I have created a Default Constraint for my table. But this is not reflected on my table.
EX: Column ImportDate has a Datetime Datatype with NOT NULL Constraint.
I have created a Default Constranit for this as
GETDATE()
. But while checking the query this Constraint not getting reflected on my table.Every time it shows NULL.
Can any one help on this?
Could it be that you are looking at records that were created before you added the DEFAULT constraint?
My thought question: Have you ever been told that your query runs too fast?
My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.
Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
Since random numbers are too important to be left to chance, let's generate some![/url]
Learn to understand recursive CTEs by example.[/url]
[url url=http://www.sqlservercentral.com/articles/St
October 1, 2012 at 4:04 am
But you couldn't apply a NOT NULL constraint to a column containing nulls, could you?
October 1, 2012 at 4:30 am
dwain.c (10/1/2012)
sqlusers (10/1/2012)
Hi Guys,I have created a Default Constraint for my table. But this is not reflected on my table.
EX: Column ImportDate has a Datetime Datatype with NOT NULL Constraint.
I have created a Default Constranit for this as
GETDATE()
. But while checking the query this Constraint not getting reflected on my table.Every time it shows NULL.
Can any one help on this?
Could it be that you are looking at records that were created before you added the DEFAULT constraint?
Looks like you may be right there...
October 1, 2012 at 4:33 am
dwain.c (10/1/2012)
sqlusers (10/1/2012)
Hi Guys,I have created a Default Constraint for my table. But this is not reflected on my table.
EX: Column ImportDate has a Datetime Datatype with NOT NULL Constraint.
I have created a Default Constranit for this as
GETDATE()
. But while checking the query this Constraint not getting reflected on my table.Every time it shows NULL.
Can any one help on this?
Could it be that you are looking at records that were created before you added the DEFAULT constraint?
That is true. I was suggesting that maybe the DEFAULT constraint was added after some INSERTs were made, and that wouldn't of course affect data already saved. I didn't notice that the column the OP was referring to was NOT NULL.
My thought question: Have you ever been told that your query runs too fast?
My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.
Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
Since random numbers are too important to be left to chance, let's generate some![/url]
Learn to understand recursive CTEs by example.[/url]
[url url=http://www.sqlservercentral.com/articles/St
October 1, 2012 at 4:37 am
dwain.c (10/1/2012)
dwain.c (10/1/2012)
sqlusers (10/1/2012)
Hi Guys,I have created a Default Constraint for my table. But this is not reflected on my table.
EX: Column ImportDate has a Datetime Datatype with NOT NULL Constraint.
I have created a Default Constranit for this as
GETDATE()
. But while checking the query this Constraint not getting reflected on my table.Every time it shows NULL.
Can any one help on this?
Could it be that you are looking at records that were created before you added the DEFAULT constraint?
That is true. I was suggesting that maybe the DEFAULT constraint was added after some INSERTs were made, and that wouldn't of course affect data already saved. I didn't notice that the column the OP was referring to was NOT NULL.
I still reckon you're right though.
Note to OP: If you add a default constraint to an existing column that allows nulls, SQL Server does not backfill the values to existing rows - you have to do that yourself!:-)
Viewing 11 posts - 1 through 10 (of 10 total)
You must be logged in to reply to this topic. Login to reply