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 2000 Forums
 SQL Server Development (2000)
 Best method to handle this where statement....

Author  Topic 

M.E.
Aged Yak Warrior

539 Posts

Posted - 2002-11-27 : 19:47:38
I don't want to use dynamic sql for this...


I have 3 variables coming into a stored procedure.
@REI
@DB
@ASU

I then do an insert into a table with data from a different one.

Heres the first part....

insert into tableA(value)
select valueA from MyTable
where mytable.REI = @REI

and heres where the interesting part comes in the next part of the code looks kinda like this... (psuedo code here)
if @db is not null Mytable.DB = @DB
else
mytable.ASU = @ASU
(pretty much if @DB has a value, the where statement points to mytable.DB If @DB is null then use mytable.ASU)

The only method I have come up with is

If @DB is not null then
Insert into --blah blah blah.. Use mytable.db = @DB
else
insert into --blah blah blah.. Use Mytable.ASU = @ASU

Is there any other way to handle this other then dynamic SQL or an if statement? I know I could make the insert statement into 2 stored procs...
if @db is not null exec storedprocDB @REI = @REI, @DB=@DB
else exec storedprocASU @REI = @REI, @ASU=@ASU



-----------------------
SQL isn't just a hobby, It's an addiction

robvolk
Most Valuable Yak

15732 Posts

Posted - 2002-11-27 : 20:04:36
How about:

insert into tableA(value)
select valueA from MyTable
where mytable.REI = @REI
AND IsNull(@DB, @ASU)=CASE WHEN @DB IS NULL THEN mytable.ASU ELSE mytable.DB END


Go to Top of Page

M.E.
Aged Yak Warrior

539 Posts

Posted - 2002-11-28 : 13:14:48
Beauty Rob.... Need to remember the uses of isnull. Thanks

Mark

-----------------------
SQL isn't just a hobby, It's an addiction
Go to Top of Page
   

- Advertisement -