Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Advice needed implementing CASE within INSERT SELECT outer Join Query

Status
Not open for further replies.

abraxas

Programmer
Jan 15, 2001
75
AU
Hello,
Having recently given up using cursors for more thoughout queries I have recently encountered a problem. Due to a legacy table requiring "Yes"/"No" values instead of Bit 0/1 I need a CASE statement for a joined Insert Select query.
Here is a cut down
Insert Into products
(
prodkey,
special,
intprice
)
Select
p.prodkey,
Case p.special
When 1 Then 'Yes'
When 0 Then 'No'
End,
p.intprice
From
products2 as p
left outer join
products as pp
on p.apn = pp.apn
where
pp.apn is null

"This returns select list for insert... contains fewer items than insert list"

I've checked the list and the number of entries in insert list and select list are equal. Is there something wrong with my CASE syntax? I tried
@Special = Case p.Special but that returns an error too.

My update joins with case work perfectly but I can't seem to get the hang of returning a CASE scalar to the Select list.

Thank you very much

 
What happen if p.special = NULL?
Try:
Code:
Insert Into products
(prodkey,special,intprice)
Select p.prodkey,
Case WHEN p.special = 1 then
          'Yes'
     ELSE 'No' End,
p.intprice
From products2 as p
left outer join products as pp on p.apn = pp.apn
where pp.apn is null


Borislav Borissov
 
Really sorry folks, I was missing a comma off my select list. It has fifty entries and I'm pretty tired so I just didn't see it. Thank you anyway bborissov.

Guess I can sleep now.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top