Solved

SQL - Default Values

Posted on 2014-10-15
5
427 Views
Last Modified: 2014-10-15
I am trying to add a column to a table and have the getdate() assigned each time a new row is insert. SQL below:

alter table sometable add EntryDateTime datetime not null default getdate()

If I specify "null" instead of "not null" then all new rows get "null' value. If I specify "not null" then all attempts to insert to the table get a "EntryDateTime cannot be null" error.

It is acting as if I have not specified a default value though I'm not sure why.
0
Comment
Question by:dthansen
5 Comments
 
LVL 24

Accepted Solution

by:
Phillip Burton earned 150 total points
ID: 40382172
It works for me. Maybe your Insert to Table code is suspect.

Please post the table structure and your Insert code.
0
 
LVL 32

Assisted Solution

by:ste5an
ste5an earned 350 total points
ID: 40382193
Yup, same here.

USE tempdb;
GO

CREATE TABLE Test
    (
      ID INT 
    );
GO

ALTER TABLE Test ADD DateCol DATETIME NOT NULL DEFAULT GETDATE();
GO

INSERT  INTO Test
        ( ID )
VALUES  ( 1 );

SELECT  *
FROM    Test;
GO

DROP TABLE Test;

Open in new window

0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 40382262
>> If I specify "null" instead of "not null" then all new rows get "null' value
   Check for any triggers on that table. also you need to ensure that the alter table statement ran successfully. otherwise it will change all the existing records with the current datetime value
>>If I specify "not null" then all attempts to insert to the table get a "EntryDateTime cannot be null" error.
can you script the table with all the constraints and triggers  and post it here
0
 

Author Comment

by:dthansen
ID: 40382292
Unfortunately the insert code is an SSIS package from another group that I have no access to.

At least I'm not crazy.

Thanks,
Dean
0
 

Author Closing Comment

by:dthansen
ID: 40382296
Thanks!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now