?
Solved

if statments

Posted on 2012-03-23
1
Medium Priority
?
295 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 12

Accepted Solution

by:
sachitjain earned 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

If you having speed problem in loading SQL Server Management Studio, try to uncheck these options in your internet browser (IE -> Internet Options / Advanced / Security):    . Check for publisher's certificate revocation    . Check for server ce…
by Mark Wills PIVOT is a great facility and solves many an EAV (Entity - Attribute - Value) type transformation where we need the information held as data within a column to become columns in their own right. Now, in some cases that is relatively…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

764 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