Groups | Blog | Home
all groups > sql server new users > february 2007 >

sql server new users : Default Value or Binding in SQL SERVER 2005 for int


Mumtaz
2/26/2007 10:11:31 AM
When i try to define a Default Value or Binding for an int field in sql servr
2005 while creating a new table in designer, i type either 0 or (0) but on
saving the table it gets converted to ((0)) and is not accessible by the code.

I have installed sp2 for sql server 2005 and still it gives me the same
result.
Any help will be great.

Steve
2/26/2007 5:54:18 PM
[quoted text, click to view]

--I ran this code
CREATE TABLE dbo.MyDefaultInt (ID int,MyInt int DEFAULT 0)
INSERT INTO dbo.MyDefaultInt (ID)VALUES(1)
INSERT INTO dbo.MyDefaultInt (ID)VALUES(1)
SELECT * FROM dbo.MyDefaultInt
--I got this result
ID MyInt
1 0
1 0
--This is waht I see in the table designer
--Its worked this way for several versions

--What do you mean by "not accessible by the code"?
Steve
2/26/2007 5:55:52 PM
[quoted text, click to view]

--I ran this code
CREATE TABLE dbo.MyDefaultInt (ID int,MyInt int DEFAULT 0)
INSERT INTO dbo.MyDefaultInt (ID)VALUES(1)
INSERT INTO dbo.MyDefaultInt (ID)VALUES(1)
SELECT * FROM dbo.MyDefaultInt
--I got this result
ID MyInt
1 0
1 0
--This is waht I see in the table designer ((0)) ...left this out the
first time
--Its worked this way for several versions

--What do you mean by "not accessible by the code"?
Mumtaz
2/27/2007 7:08:13 AM
When i try to create a new record in the program i get error message that
null is not allowed for that field. I should not get this error message as if
Steve
2/27/2007 10:47:35 AM
[quoted text, click to view]

If you insert nothing into a field that has a defaul value the default
is used.
If you try to insert NULL into a field that has a defaul value and a
NOT NULL constraint you will get an error. The default is not inserted
by design.
If you insert NULL into a field that has a defaul value and does not
have a NOT NULL constraint NULL will be inserted.
Mumtaz
2/28/2007 8:33:32 AM
Hi Steve

Many thanks, you have explained it very well to me. Will you be kind enough
Steve
2/28/2007 7:55:10 PM
[quoted text, click to view]

Other than inserting nothing,,,,
Given this table use this INSERT:
INSERT INTO dbo.MyDefaultInt(ID,MyInt)VALUES(4,DEFAULT)

CREATE TABLE [dbo].[MyDefaultInt](
[ID] [int] NULL,
[MyInt] [int] NULL DEFAULT ((0))
) ON [PRIMARY]
AddThis Social Bookmark Button