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 2005 Forums
 Transact-SQL (2005)
 Case in Select Statement, or better idea

Author  Topic 

besadmin
Posting Yak Master

116 Posts

Posted - 2010-05-24 : 15:58:24
So I am selecting some columns and one I need to do a case on with a select from another server, here is the query I have so far.


Select
itn
, Scale_Weight
, Customer_Tracking_Number
, CASE --Tracking Number
When Tracking_Number IS NOT NULL Then
Tracking_Number
When Tracking_Number IS NULL Then
(SELECT do.order_id, d.pro_nbr
FROM Server.DB.dbo.delivery_arc d
JOIN Server.DB.dbo.deliveryorders_arc do ON d.delivery_nbr = do.delivery_nbr
WHERE do.order_id = @Criteria) --'005436624'
END
, Ship_Date
, Carrier_Name
, Shipment_Number
, Package_Charge
From BES_PackageTrackingDetails
WHERE shipment_number = @Criteria
Order By itn


The error this give me is:

Msg 116, Level 16, State 1, Procedure BESsp_PackageTrackingDetailPageTESTsmm, Line 27
Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.

The part I am concerned with is the CASE --Tracking Number value.

Any help is greatly appreciated.

Thanks a lot!

vijayisonly
Master Smack Fu Yak Hacker

1836 Posts

Posted - 2010-05-24 : 16:01:26
You are selecting two values "do.order_id, d.pro_nbr " and assigning it to a single field "Tracking_Number"...which one corresponds to the tracking number?
Go to Top of Page

besadmin
Posting Yak Master

116 Posts

Posted - 2010-05-24 : 16:04:08
oh yea, sorry. I forgot to edit that. The one I want to use will be d.pro_nrb.

Thanks!
Go to Top of Page

vijayisonly
Master Smack Fu Yak Hacker

1836 Posts

Posted - 2010-05-24 : 16:09:10
Try this
Select a.itn
, a.Scale_Weight
, a.Customer_Tracking_Number
, coalesce(a.Tracking_Number,d.pro_nrb)
, a.Ship_Date
, a.Carrier_Name
, a.Shipment_Number
, a.Package_Charge
From BES_PackageTrackingDetails a
inner join Server.DB.dbo.deliveryorders_arc do on a.shipment_number = do.order_id
inner join Server.DB.dbo.delivery_arc d on d.delivery_nbr = do.delivery_nbr
WHERE a.shipment_number = @Criteria
Order By itn
Go to Top of Page

besadmin
Posting Yak Master

116 Posts

Posted - 2010-05-24 : 16:19:00
I think I figured it out just by taking that extra column out of the select. This query you provided gave me the same desired results as well though! So, thanks a lot for your quick help! Very much appreciated!
Go to Top of Page

vijayisonly
Master Smack Fu Yak Hacker

1836 Posts

Posted - 2010-05-24 : 16:36:47
Np. You are welcome
Go to Top of Page
   

- Advertisement -