Wednesday, March 28, 2012

How to make or add a table with a field with default value

I am doing a shopping basket type demo.

I have difficulties going from table with fields like (Name,ProductCode) to table(Name, ProductCode, NumberOfItems).

One way could be adding just Name and ProductCode fields and let NumberOfItems come automatically. It could have some common value like 1.

How should I do this with VB.

Do you mean that if you insert a record but don't supply values for all the fields that you want the database to default the value? This is easy to do in SQL Server, here's an example of setting a default value of 1

create table junk ( SomeIntColumn smallint NOT NULL DEFAULT 1)

You can also create your own named defaults, then bind them, eg:


create default blank as ' '
go
create default zero as 0
go
create default today as getdate()
go

create table junk(SomeDateColumn datetime not null)

go

exec sp_bindefault today, 'junk.SomeDatecolumnt

go

This binds a default value named "today' to SoemDateColumn in table junk

No comments:

Post a Comment