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)
 put a select in a case?

Author  Topic 

mayerl
Yak Posting Veteran

95 Posts

Posted - 2010-05-21 : 08:27:25
Morning,

I'm not even sure this is going to work but I thought I would ask. I'm trying to base a select statement on a type like so:


select type,
case when type = 'SOShip' then (select * from vw_SH_PacklistSOShip)
when type = 'Shipment' then (select * from vw_SH_PacklistShip)
end
from Packlist_Header
where Packlist = '46194'


But when I run it I get this message:


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


Any thoughts would be great?

Thanks

Laura

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-05-21 : 08:32:50
You can do that in another way by using joins.
But is there a difference between vw_SH_PacklistSOShip and vw_SH_PacklistShip structure?

Please give table/view structure sample data and wanted output


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

mayerl
Yak Posting Veteran

95 Posts

Posted - 2010-05-21 : 09:13:09
well its a view that I inherited so I make no claims for its lack of style:


--vw_SH_PacklistShip

SELECT Packlist_Detail.Packlist,
Packlist_Detail.SO_Detail,
Packlist_Detail.Quantity,
Packlist_Detail.Promised_Qty,
Packlist_Detail.Backorder_Qty,
Packlist_Header.Ship_Via,
Packlist_Header.Ship_To,
Packlist_Header.Packlist_Date,
Packlist_Detail.Due_Date,
Customer.Accept_BO,
Packlist_Detail.Packlist_Detail,
Packlist_Detail.Order_Unit AS sOrderUnit,
Job.Job AS sJobSO,
dbo.GetBusinessLocationForJob(Job.Job) as Location,
(Select Sum(Delivery.Remaining_Quantity) From Delivery where Delivery.Job=Packlist_Detail.Job and Delivery.Packlist=Packlist_Detail.Packlist) as RemQty,
CASE WHEN Packlist_Detail.Job IS NOT NULL THEN Job.Part_Number ELSE Material_Trans.Material END AS sPartNumber,
CASE WHEN Packlist_Detail.Job IS NOT NULL THEN Job.Rev ELSE Material.Rev END AS sRev,
CASE WHEN Packlist_Detail.Job IS NOT NULL THEN Job.Description ELSE Material.Description END AS sDescription,
CASE WHEN Packlist_Detail.Job IS NOT NULL THEN 1 ELSE 2 END AS nUseExtDesc,
CASE WHEN Packlist_Detail.Job IS NOT NULL THEN Job.Customer_PO ELSE Material_Trans.PO_Number END AS sPO,
CASE WHEN Packlist_Detail.Job IS NOT NULL THEN Job.Customer_PO_LN ELSE Material_Trans.PO_Line END AS sPOLine,
'' AS sSOLine,
CASE WHEN Packlist_Detail.Job IS NOT NULL THEN Job.Terms ELSE Customer.Terms END AS sTerms,
CASE WHEN Packlist_Detail.Job IS NOT NULL THEN User_Values.Amount1 ELSE 0 END AS isItar,
Packlist_Detail.Note_Text,
Packlist_Header.Comment,
Job.Ext_Description AS ExtDesc1,
Material.Ext_Description AS ExtDesc2,
'' AS ExtDesc3,
'' AS ExtDesc4,
'' AS JODesc,
'' as OrigPL,
'' as Stock_UofM,
Packlist_Header.Invoiced,
Packlist_Detail.Tracking_Nbr AS sTrack,
'' AS sService
FROM Material RIGHT JOIN ((Packlist_Header LEFT JOIN (Customer RIGHT JOIN (Packlist_Detail LEFT JOIN Job ON Packlist_Detail.Job = Job.Job)
ON Customer.Customer = Job.Customer) ON Packlist_Header.Packlist = Packlist_Detail.Packlist)
LEFT JOIN Material_Trans ON Packlist_Detail.Material_Trans = Material_Trans.Material_Trans) ON Material.Material = Material_Trans.Material LEFT JOIN User_Values ON User_Values.User_Values = Job.User_Values


and the the other one:



-- vw_SH_PacklistSOShip]
SELECT Packlist_Detail.Packlist,
Packlist_Detail.SO_Detail,
Packlist_Detail.Quantity,
Packlist_Detail.Promised_Qty,
Packlist_Detail.Backorder_Qty,
Packlist_Header.Ship_Via,
Packlist_Header.Ship_To,
Packlist_Header.Packlist_Date,
Packlist_Detail.Due_Date,
Customer.Accept_BO,
Packlist_Detail.Packlist_Detail,
'' as sOrderUnit,
SO.Sales_Order AS sJobSO,
dbo.GetBusinessLocationForSODetail(Packlist_Detail.SO_Detail) as Location,
(Select Sum(Delivery.Remaining_Quantity) From Delivery where Delivery.SO_Detail=Packlist_Detail.SO_Detail and Delivery.Packlist=Packlist_Detail.Packlist And Delivery.Job Is Null) as RemQty,
CASE WHEN Customer_Part.Customer_Part Is Not Null THEN Customer_Part.Part_Number WHEN Customer_Part_1.Customer_Part Is Not Null THEN Customer_Part_1.Part_Number ELSE SO.Material END AS sPartNumber,
CASE WHEN Customer_Part.Customer_Part Is Not Null THEN Customer_Part.Rev WHEN Customer_Part_1.Customer_Part Is Not Null THEN Customer_Part_1.Rev ELSE SO_Detail.Rev END AS sRev,
CASE WHEN Customer_Part.Customer_Part Is Not Null THEN Customer_Part.Description WHEN Customer_Part_1.Customer_Part Is Not Null THEN Customer_Part_1.Description WHEN Material.Material Is Not Null THEN Material.Description ELSE SO_Detail.Description END AS sDescription,
CASE WHEN Customer_Part.Customer_Part Is Not Null THEN 4 WHEN Customer_Part_1.Customer_Part Is Not Null THEN 4 WHEN SO.SOD Is Not Null THEN 4 ELSE 3 END AS nUseExtDesc,
SO.Customer_PO AS sPO,
SO.SO_Line AS sPOLN,
'' AS sSOLine,
SO.Terms AS sTerms,
coalesce(User_Values.Amount1, 0) AS isItar,
Packlist_Detail.Note_Text,
Packlist_Header.Comment,
Customer_Part.Ext_Description AS ExtDesc1,
Customer_Part_1.Ext_Description AS ExtDesc2,
Material.Ext_Description AS ExtDesc3,
SO.ExtDesc4,
'' AS JODesc,
'' as OrigPL,
SO_Detail.Stock_UofM as Stock_UofM,
Packlist_Header.Invoiced,
Packlist_Detail.Tracking_Nbr AS sTrack,
'' AS sService
FROM ((Packlist_Header INNER JOIN ((((Packlist_Detail INNER JOIN (SELECT SO_Header.Customer, SO_Header.Customer_PO, SO_Header.Terms,
SO_Detail.Material, SO_Detail.SO_Line, SO_Detail.Sales_Order, SO_Detail.Line, Customer.Type, SO_Detail.SO_Detail AS SOD,
SO_Detail.Description, SO_Detail.Ext_Description AS ExtDesc4 FROM (SO_Header INNER JOIN (Packlist_Detail INNER JOIN SO_Detail ON Packlist_Detail.SO_Detail = SO_Detail.SO_Detail)
ON SO_Header.Sales_Order = SO_Detail.Sales_Order) INNER JOIN Customer ON SO_Header.Customer = Customer.Customer) AS SO
ON Packlist_Detail.SO_Detail = SO.SOD) LEFT JOIN Material ON SO.Material = Material.Material) LEFT JOIN Customer_Part ON
(SO.Material = Customer_Part.Material) AND (SO.Customer = Customer_Part.Customer)) LEFT JOIN Customer_Part AS Customer_Part_1 ON
(SO.Material = Customer_Part_1.Material) AND (SO.Type = Customer_Part_1.Customer_Type)) ON Packlist_Header.Packlist = Packlist_Detail.Packlist)
LEFT JOIN Customer ON SO.Customer = Customer.Customer) LEFT JOIN SO_Detail ON Packlist_Detail.SO_Detail = SO_Detail.SO_Detail
LEFT JOIN SO_Header on SO_Detail.Sales_Order = SO_Header.Sales_Order
LEFT JOIN User_Values on SO_Header.User_Values = User_Values.User_Values



and some data obfuscated of course:



46194 2976 3 6 0 HAND CARRIED 627 2010-04-13 00:00:00.000 NULL NULL 13265 each NULL N NULL NULL NULL NULL 2 NULL NULL NULL 0.00 NULL [DOEJ - 04/13/10 12:54:40 PM] NULL NULL 1 NULL
46194 2976 3 6 0 HAND CARRIED 627 2010-04-13 00:00:00.000 NULL 1 13265 200632 KN 0 T008347600 REV 000 NULL Window 4 K450336 001-01 Net 60 days 1.00 NULL [DOEJ - 04/13/10 12:54:40 PM] NULL NULL NULL NULL each 1 NULL
46194 2976 3 6 0 HAND CARRIED 627 2010-04-13 00:00:00.000 NULL 1 13265 200632 KN 0 T008347600 REV 000 NULL Window 4 K450336 001-01 Net 60 days 1.00 NULL [DOEJ - 04/13/10 12:54:40 PM] NULL NULL NULL NULL each 1 NULL
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-05-21 : 09:24:32
Try this, but I don't really know if it is right to join on column Packlist:
select * from
(
select
ph.type,
l.*
from Packlist_Header as ph
join vw_SH_PacklistSOShip as l
on l.Packlist = ph.Packlist and ph.type = 'SOShip'
where ph.Packlist = '46194'

union all

select
ph.type,
l.*
from Packlist_Header as ph
join vw_SH_PacklistShip as l
on l.Packlist = ph.Packlist and ph.type = 'Shipment'
where ph.Packlist = '46194'
) as dt



No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

mayerl
Yak Posting Veteran

95 Posts

Posted - 2010-05-21 : 09:47:09
Thanks for that, I was doing something similar in a larger view. So basically what your telling me is my tricky tricky way of doing it with a case wont work huh?
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-05-21 : 10:51:08
IMHO it would not work with a case.


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

lazerath
Constraint Violating Yak Guru

343 Posts

Posted - 2010-05-21 : 10:56:22
If you were returning a single column or expression from each subquery and only a single row, it would work. For instance, try replacing SELECT * with SELECT COUNT(*). If you want more than one column or more than one row, this technique will not work.
Go to Top of Page
   

- Advertisement -