Solved

default value for a column does not work

Posted on 2010-09-14
5
234 Views
Last Modified: 2012-05-10
hi experts, i have this code

CREATE TABLE [certifica].[Actividad](
      [Codigo] [int] IDENTITY(1,1) NOT NULL,
      [CodigoActividad] [bigint] NOT NULL,
....
      [UsuarioModificacion] [varchar](16) NULL,
      [FechaModificacion] [datetime2](0) NULL,
      [SituacionRegistro] [char](1) NULL,
PRIMARY KEY CLUSTERED
(
      [CodigoActividad] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

ALTER TABLE [certifica].[Actividad] ADD  DEFAULT (getdate()) FOR [FechaCreacion]
GO

ALTER TABLE [certifica].[Actividad] ADD  DEFAULT ('1') FOR [SituacionRegistro]
GO

Insert records right through a web application and does not work the default for the column x
0
Comment
Question by:enrique_aeo
[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
  • 2
  • 2
5 Comments
 
LVL 60

Accepted Solution

by:
chapmandew earned 250 total points
ID: 33673212
default values only work if you do not specify a value in the field list when inserting.  If you explicitly insert NULL, then NULL will be inserted.
0
 
LVL 2

Expert Comment

by:IngCharlie
ID: 33673253
Change  the column FechaModificacion to datetime and set it to not NULL after this change set the default values
0
 
LVL 2

Expert Comment

by:IngCharlie
ID: 33673327
I ment [FechaCreacion] set it to not NULL and do not set its value in the insert SQL
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 33673361
it doens't matter if it is null or not for new values.  Just do not supply a value on the insert.
0
 
LVL 22

Expert Comment

by:8080_Diver
ID: 33673397
>set it to not NULL after this change set the default values
It really doesn't matter whether the column is set to NULL or NOT NULL.  What matters is to not provide any value for the columns when you are inserting data unless you want the columns set to whatever values you provide.
So, since the FechaCreacion should probably be allowed to default to the current datetime in virtually every case, don't even include that column in your INSERT statement.  (However, you may want to have a special process for entering data when there has been a delay and you want the creation date to reflect when it should have been created . . . in which case, you will need a process that does provide the FechaCreacion.
 
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

752 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