I have a stored procedure that accepts a parameter called @UserCurrency.
SELECT ItemID,
CASE WHEN @UserCurrency=p.ItemCurrency THEN
p.ItemPrice
ELSE
CASE WHEN p.ItemPrice = 999999999 THEN
p.ItemPrice
ELSE
p.ItemPrice * CASE WHEN @UserCurrency = 'GBP' THEN c.GBP
WHEN @UserCurrency = 'USD' THEN c.USD
WHEN @UserCurrency = 'EUR' THEN c.EUR
END
END
END As Price,
FROM Items p
LEFT JOIN CurrencyRates c
ON c.CurrencyFrom = p.ItemCurrency
Each item in the database has an ItemPrice (DECIMAL 10,0) that may be in one of several currencies (EUR, GBP, USD, ZAR, etc) depending on the item's location.
The CurrencyRates table has rows DECIMAL(5,2):
CurrencyFrom EUR USD GBP
EUR null 1.36 0.81
GBP 1.17 1.6 null
USD 0.71 null 0.63
On the website, a user can choose in which currency they wish to display items.
I check to see if the UserCurrency matches the ItemCurrency in my stored procedure. If YES, then I just select the ItemPrice as it requires no conversion. If NO, then I look up a table of currency conversion values to calculate the correct price to display through a JOIN.
My problem is that I can always get the values to work if I run the procedure on SSMS using the exact same values passed in by the web page. But if I view it on the webpage, the first Item has an ItemPrice but none of the subsequent records do.
If I remove the full CASE WHEN and just select ItemPrice on its own, all of the prices are shown on the website correctly. It seems to be the CASE WHEN tripping up ASP in some weird way.
SELECT ItemID,
ItemPrice
FROM Items
I have read that empty fields in an ASP recordset can be caused by a NVARCHAR(MAX), but there's no NVARCHAR(MAX) in my SELECT.
CONNECTION STRING
SET connect = server.CreateObject ("ADODB.connection")
dbstring = "Provider=SQLNCLI11;DataTypeCompatibility=80;Server=theserver.com; Database=thedatabase;uid=theuser;pwd=thepassword"
connect.Open dbstring
RECORDSET OPEN
items.open "ItemStoredProcedure " & var_PAGENUMBER & "," & var_ITEMSPERPAGE & "," & var_SORTING & "," & var_USER_CURRENCY
LOOP
IF NOT items.EOF THEN
items.MOVEFIRST
DO WHILE NOT items.EOF
response.write items("Price")
items.MOVENEXT
LOOP
END IF
What is going on? Please help!
FULL DISCLOSURE
- There are nvarchar(max) fields within the Items table
- I do want to select nvarchar(max) fields in this same procedure, but for the purposes of testing this problem they are completely commented out
- There are other int and varchar fields in the select but I haven't included them for clarity