Solved

if statments

Posted on 2012-03-23
1
289 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

920 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now