Solved

default value for a column does not work

Posted on 2010-09-14
5
230 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
  • 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
In this article I will describe the Detach & Attach 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 Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

776 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