Solved

SQL - Default Values

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

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

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

688 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