Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 if the parameter is null then insert the default

Author  Topic 

danasegarane76
Posting Yak Master

242 Posts

Posted - 2007-08-21 : 09:31:59
I need to insert default value if the local variable is null.The following gives error.Could any one help on this....

create table testdefault (id int,name varchar(10),required bit default 0)

insert testdefault values (1,'aabaxq',default) --no error

declare @req varchar(1)
insert testdefault values (1,'aabaxq',coalesce(@req,default)) -error

or
declare @req varchar(1)
insert testdefault values (1,'aabaxq',isnull(@req,default))--error

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-08-21 : 09:42:11
If you know the default value is zero, use that.
insert testdefault select 1,'aabaxq',isnull(@req,0)



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

danasegarane76
Posting Yak Master

242 Posts

Posted - 2007-08-21 : 09:45:47
Some times I know the default.But Some times I dont know ?
is there any other method ?
Shall i try like this ?

select column_default from information_schema.columns where table_name='testdefault' and column_name='required'


Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-08-22 : 01:48:03
If @var is null
insert testdefault(col1,col2) values (1,'aabaxq')
else
insert testdefault(col1,col2,col3) values (1,'aabaxq',@var)


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -