Solved

if statments

Posted on 2012-03-23
1
291 Views
Last Modified: 2012-03-27
if
(
select distinct left(bill_id,2) from openquery(PP_NEW,
'select distinct s.sku_no,l.item_no,s.bill_id,i.ship_dt,i.po_no
from invoice as i  left outer join line_it as l
on i.invoice_no=l.invoice_no
left outer join sku as s
on i.ship_id=s.bill_id and
l.item_no=s.item_no
where i.ship_dt>={04/01/12} and
i.bill_id=[CWALMF] and
s.type=[S]
')
where ship_dt='2012-04-01'
and po_no in ('7953090880','7953090866')
)
='WM'

begin

select distinct bill_id from openquery(PP_NEW,
'select bill_id
from sku
where left(bill_id,2)=[WM]
')

end

else

begin

select distinct bill_id from openquery(PP_NEW,
'select bill_id
from sku
where bill_id=[CWALMF]
')
End


WM02795
WM03219
WM02564
WM01525
WM05488
WM02808
WM01969


I need the query to go through the data returned from the query of the if statment and evaluate if the result for ‘WM’ is TRUE to run one query and if it is FALSE to run a different query, the problem with this structure is that  I do not get the else also one po_no will return TRUE for left(bill_id,2)=’WM’ and the other will return FALSE
And I only get in the result set the first query

select distinct bill_id from openquery(PP_NEW,
'select bill_id
from sku
where left(bill_id,2)=[WM]

and  I expect to get information for the second query too.
0
Comment
Question by:GadFriedman
1 Comment
 
LVL 12

Accepted Solution

by:
sachitjain earned 500 total points
ID: 37760636
Does it help you?

declare @po_no_tbl table (SNo int identity(1,1), po_no varchar(12))
declare @bill_no_tbl table (bill_id varchar(12))

insert into @po_no_tbl (po_no) values ('7953090880')
insert into @po_no_tbl (po_no) values ('7953090866')

declare @i int, @cnt int, @po_no varchar(12)
set @i = 1
select @cnt = COUNT(SNo) from @po_no_tbl

while @i <= @cnt
begin
      select @po_no = po_no from @po_no_tbl where SNo = @i
      if
      (
            select distinct left(bill_id,2) from openquery(PP_NEW,
            'select distinct s.sku_no,l.item_no,s.bill_id,i.ship_dt,i.po_no
            from invoice as i  left outer join line_it as l
            on i.invoice_no=l.invoice_no
            left outer join sku as s
            on i.ship_id=s.bill_id and
            l.item_no=s.item_no
            where i.ship_dt>={04/01/12} and
            i.bill_id=[CWALMF] and
            s.type=[S]
            ')
            where ship_dt='2012-04-01'
            and po_no  = @po_no
      ) ='WM'
      begin
            insert into @bill_no_tbl (bill_no)
            select distinct bill_id from openquery(PP_NEW,
            'select bill_id
            from sku
            where left(bill_id,2)=[WM]
            ')
      end
      else
      begin
            insert into @bill_no_tbl (bill_no)
            select distinct bill_id from openquery(PP_NEW,
            'select bill_id
            from sku
            where bill_id=[CWALMF]
            ')
      End
      set @i = @i + 1
end
select * from @bill_no_tbl
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will describe one method to parse a delimited string into a table of data.   Why would I do that you ask?  Let's say that you need to pass multiple parameters into a stored procedure to search for.  For our sake, we'll say that we wa…
I've encountered valid database schemas that do not have a primary key.  For example, I use LogParser from Microsoft to push IIS logs into a SQL database table for processing and analysis.  However, occasionally due to user error or a scheduled task…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question