Solved

SQL - Default Values

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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

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.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
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.

749 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