Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Nulls and defaults

Status
Not open for further replies.
Jun 27, 2001
837
US
I have a sp which populates a table. the SP resides on the same server/db as the table. Several of the values are assigned nulls and then inserted into the table. I have 4 defaults on some of these fields. However, instead of loading the default it puts a blank. ANy ideas
 
If you want to use the default value for a column, then you shouldn't be inserting ANYTHING in to that column (even a value of null). Take a look at this code...

Code:
[COLOR=blue]Create[/color] [COLOR=blue]Table[/color] #TestDefault(Id [COLOR=blue]Int[/color], Data [COLOR=blue]VarChar[/color](100) [COLOR=blue]Default[/color] [COLOR=red]'This is blank'[/color])

[COLOR=blue]Insert[/color] [COLOR=blue]Into[/color] #TestDefault [COLOR=blue]Values[/color](1, NULL)
[COLOR=blue]Insert[/color] [COLOR=blue]Into[/color] #TestDefault(Id) [COLOR=blue]Values[/color](2)

[COLOR=blue]Select[/color] * [COLOR=blue]from[/color] #TestDefault

[COLOR=blue]Drop[/color] [COLOR=blue]Table[/color] #TestDefault

Notice that the first insert does insert a value (NULL) for the data column, whearas the second one does not insert a value.

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Oh... Yeah... I didn't really help you solve your problem, did I. Sorry.

What you could do is get the default values for the column that you are inserting and insert that instead of null. You can determine what the default value is by using the information_schema.columns view.

Code:
[COLOR=blue]Select[/color] * 
[COLOR=blue]from[/color]   Information_Schema.Columns 
[COLOR=blue]Where[/color]  Table_Name = [COLOR=red]'[!]YourTableNameHere[/!]'[/color]

There will be a column returned named [!]Column_Default[/!].

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
or you could make the parameters of the sp not required and then only insert the ones you need.

Questions about posting. See faq183-874
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top