Solved

default value for a column does not work

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
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.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

705 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now