?
Solved

SQL - Default Values

Posted on 2014-10-15
5
Medium Priority
?
439 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 24

Accepted Solution

by:
Phillip Burton earned 600 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 35

Assisted Solution

by:ste5an
ste5an earned 1400 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

Get MongoDB database support online, now!

At Percona’s web store you can order your MongoDB database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card. Handle your MongoDB database support now!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
I have a large data set and a SSIS package. How can I load this file in multi threading?
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Suggested Courses

801 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