| Author |
Topic |
|
sqlnovice123
Constraint Violating Yak Guru
262 Posts |
Posted - 2010-03-04 : 09:48:18
|
| Hi,I am trying to create the below lookup parameter proc which should display values as below so the front end can pick up the values based on the key.I get the error belowMsg 4145, Level 15, State 1, Procedure splkup_BuySell_AvgCoverRatio, Line 25An expression of non-boolean type specified in a context where a condition is expected, near 'THEN'. Am I missing a condition? Thanks for your help.Key ValueBid BidOffer OfferBoth BothDECLARE @BuyOrSell TABLE ( [Key] varchar(50), [Value] varchar(50) )--SELECT DISTINCT IsBuy FROM TradeINSERT INTO @BuyOrSellSELECT lookup_key = IsBuy, lookup_val = IsBuy FROM TradeWHERE ISBuy = CASE WHEN 1 THEN 'Bid' WHEN 0 THEN 'Offer' ELSE 'ALL' END SELECT lookup_key = bs.[Key], lookup_val = bs.[Value]FROM @BuyOrSell bs |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-04 : 09:51:52
|
| you should have a condition specified for CASECASE <some condition> WHEN 1 THEN 'Bid' WHEN 0 THEN 'Offer' ELSE 'ALL' END------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2010-03-04 : 09:52:13
|
| INSERT INTO @BuyOrSellSELECT lookup_key = IsBuy,lookup_val = IsBuy FROM TradeWHERE ISBuy = CASE [value] WHEN 1 THEN 'Bid' WHEN 0 THEN 'Offer' ELSE 'ALL' ENDMadhivananFailing to plan is Planning to fail |
 |
|
|
sqlnovice123
Constraint Violating Yak Guru
262 Posts |
Posted - 2010-03-04 : 10:38:29
|
| Question:IsBuy has value 1 0r 0. I get the below error for the query below. Should I use the convert ?Msg 245, Level 16, State 1, Procedure splkup_BuySell_AvgCoverRatio, Line 20Conversion failed when converting the varchar value 'Bid' to data type bit.DECLARE @BuyOrSell TABLE ( [Key] varchar(50), [Value] varchar(50) )--SELECT DISTINCT IsBuy FROM TradeINSERT INTO @BuyOrSellSELECT lookup_key = IsBuy, lookup_val = IsBuy FROM Trade trdWHERE IsBuy = CASE [IsBuy] WHEN 1 THEN 'Bid' WHEN 0 THEN 'Offer' ELSE 'ALL' END |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-04 : 10:51:31
|
| what re you trying to do in WHERE clause? can you explain that?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
sqlnovice123
Constraint Violating Yak Guru
262 Posts |
Posted - 2010-03-04 : 10:59:05
|
| Sure. I want to check for the value of IsBuy. When it is 1 I want to replace with 'Bid' When 0 I want to replace with 'Offer'I want the final result of the proc to look like soKey ValueBid BidOffer OfferBother Both |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-04 : 11:06:42
|
| how Bother became Both?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
sqlnovice123
Constraint Violating Yak Guru
262 Posts |
Posted - 2010-03-04 : 11:20:28
|
| Oops, It is Both, typo error, apologies. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-04 : 11:29:55
|
| ok. so whats the relevance of CASE expression in WHERE. what check are you doing there?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
sqlnovice123
Constraint Violating Yak Guru
262 Posts |
Posted - 2010-03-04 : 12:00:15
|
| The reason for CASE is that I want to restrict to three rows values which will be passed to an in-house GUI via the column "Key".I tried the below query but I get too many rows. I will try other options but any help is appreciated.SELECT lookup_key = CASE trd.IsBuy WHEN 1 THEN 'Bid' WHEN 0 THEN 'Offer' ELSE 'ALL' END, lookup_val = CASE trd.IsBuy WHEN 1 THEN 'Bid' WHEN 0 THEN 'Offer' ELSE 'ALL' ENDFROM Trade trdFor example:DECLARE @SingleOrList TABLE ( [Key] varchar(50), [Value] varchar(50) )INSERT INTO @SingleOrList VALUES ('All Inquiries','All Inquiries')INSERT INTO @SingleOrList VALUES ('Single Inquiry','Single Inquiries')INSERT INTO @SingleOrList VALUES ('Lists','Lists') SELECT lookup_key = yn.[Key], lookup_val = yn.ValueFROM @SingleOrList yngives the below output but I do not have aconditional bit value to check for in a base table so it is simple. The one I am having an issue with is a little tricky. Key ValueAll Inquiries All InquiriesSingle Inquiry Single InquiriesLists Lists |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-04 : 12:02:55
|
| Whats the tricky one? post the query that you're having problem with not the one which is working fine------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
sqlnovice123
Constraint Violating Yak Guru
262 Posts |
Posted - 2010-03-04 : 12:11:19
|
| This is what i tried before and I get too much dataDECLARE @BuyOrSell TABLE ( [Key] varchar(50), [Value] varchar(50) )INSERT INTO @BuyOrSellSELECT lookup_key = CASE trd.IsBuy WHEN 1 THEN 'Bid' WHEN 0 THEN 'Offer' ELSE 'ALL' END, lookup_val = CASE trd.IsBuy WHEN 1 THEN 'Bid' WHEN 0 THEN 'Offer' ELSE 'ALL' ENDFROM Trade trd |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-04 : 12:18:28
|
| what do you mean by too much data? explain with example..plzz understand we cant see your system neither can we read your mind so unless you tell us with help of data what you're getting and what you expect we cant help you------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
sqlnovice123
Constraint Violating Yak Guru
262 Posts |
Posted - 2010-03-04 : 12:34:28
|
| I mean it continues to print while I would like to restrict to 3 rows of data. I tried doing joins to filter but this did not help.Key ValueBid BidOffer OfferBother BothKey ValueBid BidOffer OfferBother BothBid BidOffer OfferBother BothBid BidOffer OfferBother BothBid BidOffer OfferBother BothBid BidOffer OfferBother Both |
 |
|
|
sqlnovice123
Constraint Violating Yak Guru
262 Posts |
Posted - 2010-03-04 : 12:39:23
|
| I feel dumb. Sorry. I did a DISTINCT and I have the following. Now IsBuy from Trade has only 1 or 0 hence the ELSE case will never happen but I have it there anyways.SELECT DISTINCT lookup_key = CASE trd.IsBuy WHEN 1 THEN 'Bid' WHEN 0 THEN 'Offer' ELSE 'BOTH' END, lookup_val = CASE trd.IsBuy WHEN 1 THEN 'Bid' WHEN 0 THEN 'Offer' ELSE 'BOTH' ENDFROM Trade trdKey ValueOffer OfferBid Bid |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-04 : 12:41:59
|
| Do you think anybody can guess your problem from what you've explained so far?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|