Hi fellow-developers,
I am literally hitting a wall trying to get the syntax right for this case statement.
Could someone please point out where I am going wrong?
Thanks in advance :)
DECLARE @cStr AS varchar(50)
DECLARE @nNum INT
SET @nNum = 0
SET @cStr = 'XX'
SET @nNum = (year(getdate()) - 2007)
print ' num P = ' + cast(@nNum as char)
SELECT CASE
WHEN (@nNum = 0) THEN SET @cStr = @cStr + 'CY'
WHEN (@nNum <> 0) THEN RAISERROR ('xxx', 16, 1)
WHEN (@nNum >= 10) THEN RAISERROR ('yyy.', 16, 1)
WHEN (@nNum < 10) THEN SET @cStr = @cStr + Replicate ('P', @nNum) + 'Y'
END
3 3315 Motoma 3,237
Recognized Expert Specialist
I think what you really want is this: -
DECLARE @cStr AS varchar(50)
-
DECLARE @nNum INT
-
-
SET @nNum = 0
-
SET @cStr = 'XX'
-
SET @nNum = (year(getdate()) - 2007)
-
print ' num P = ' + cast(@nNum as char)
-
-
-
IF (@nNum <> 0) RAISERROR ('xxx', 16, 1)
-
IF (@nNum >= 10) RAISERROR ('yyy.', 16, 1)
-
-
SELECT CASE
-
WHEN (@nNum = 0) THEN @cStr + 'CY'
-
WHEN (@nNum < 10) THEN @cStr + Replicate ('P', @nNum) + 'Y'
-
END
-
You see, you had a SELECT statement with a CASE statement inside. At runtime, this will be replaced with the actual case that it falls under. Your code would have generated the equivalent of -
SELECT SET @cStr = @cStr + 'CY'
-
Which is not a valid SELECT statement.
Thank you very much for your response, The procedure works perfectly :) Thanks also for providing an explanation on why an error was being returned.
Regards,
-AM
I think what you really want is this: -
DECLARE @cStr AS varchar(50)
-
DECLARE @nNum INT
-
-
SET @nNum = 0
-
SET @cStr = 'XX'
-
SET @nNum = (year(getdate()) - 2007)
-
print ' num P = ' + cast(@nNum as char)
-
-
-
IF (@nNum <> 0) RAISERROR ('xxx', 16, 1)
-
IF (@nNum >= 10) RAISERROR ('yyy.', 16, 1)
-
-
SELECT CASE
-
WHEN (@nNum = 0) THEN @cStr + 'CY'
-
WHEN (@nNum < 10) THEN @cStr + Replicate ('P', @nNum) + 'Y'
-
END
-
You see, you had a SELECT statement with a CASE statement inside. At runtime, this will be replaced with the actual case that it falls under. Your code would have generated the equivalent of -
SELECT SET @cStr = @cStr + 'CY'
-
Which is not a valid SELECT statement.
Motoma 3,237
Recognized Expert Specialist
You are welcome. I am glad everything worked out for you.
Thank you very much for your response, The procedure works perfectly :) Thanks also for providing an explanation on why an error was being returned.
Regards,
-AM
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Dave |
last post by:
I am getting a syntax error on
Select Case Now
Case > "2/2/2004 12:30:00 AM"
response.redirect "votingover.asp"
case else
m=0
End Select
|
by: Kevin |
last post by:
Hi, I am getting a syntax error
Microsoft VBScript compilation error '800a03ea'
Syntax error
On the code below. The error references the "End Select" line
Can anyone help me with what I am...
|
by: Michael Hill |
last post by:
Is this a valid sql statement:
SELECT
case field1
when 'first' then 1
when 'second' then 1
else null
end
FROM mytable
|
by: Penny |
last post by:
Hi all,
My browser throws this Select Case block back at me pointing out a syntax
error on the line: 'Case < 251', between the word 'Case' and the '<' symbol.
***************************
...
|
by: Marco Lazzeri |
last post by:
What about a SELECT syntax like that?
SELECT
CASE WHEN bar.foo_id IS NULL THEN bar.*
ELSE foo.*
FROM foo, bar
Anyway, I need a SELECT query that gets data from a FIRST TABLE if a
specific...
| |
by: Benny Raymond |
last post by:
I'm confused as to how fallthrough is limited in switch. For example
the following works:
string switch_test = "a";
switch (switch_test)
{
case "a":
case "b":
case "c":
doSomething(a);
|
by: CJM |
last post by:
I'm getting a syntax error with a Select Case statement:
Select Case CSng(rs.fields("Field1"))
Case 0
Response.Write "Test1"
Case Is < 0 <<< Syntax Error...
|
by: enfuego |
last post by:
Hey kids, got a problem. Here's the code:
Rev.techCompany CASE WHEN techCompany LIKE 'North_IR' THEN 'NorthIR'
WHEN techCompany LIKE 'South_IR' THEN 'SouthIR'
WHEN techCompany LIKE...
|
by: arunmenon |
last post by:
Dear Folks,
I have an query which something looks like this
select ILTRDJ AS YEAR, DATEPART(MONTH,ILTRDJ) AS MONTH,
FMSTR.IMITM AS CODE,FMSTR.IMSRP6_UDC AS DESCRIPTION, FTRANS.ILDCT =
CASE...
|
by: Adem |
last post by:
C/C++ language proposal:
Change the 'case expression' from "integral constant-expression" to "integral expression"
The C++ Standard (ISO/IEC 14882, Second edition, 2003-10-15)
says under...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |