Solved

if statments

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

There are some very powerful Data Management Views (DMV's) introduced with SQL 2005. The two in particular that we are going to discuss are sys.dm_db_index_usage_stats and sys.dm_db_index_operational_stats.   Recently, I was involved in a discu…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

757 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

17 Experts available now in Live!

Get 1:1 Help Now