Solved

SQL - Default Values

Posted on 2014-10-15
5
433 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 33

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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

In this article I will describe the Copy Database Wizard 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.
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
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
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

792 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