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
 General SQL Server Forums
 New to SQL Server Programming
 switch case is there

Author  Topic 

sunsanvin
Master Smack Fu Yak Hacker

1274 Posts

Posted - 2007-10-04 : 03:03:05
Dear all,
is there any switch case like we have in C language....
in sql server?

Vinod
Even you learn 1%, Learn it with 100% confidence.

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-10-04 : 03:07:40
[code]DECLARE @a INT,
@b INT

SELECT @a = 3,
@b = 5

-- Before
SELECT @a,
@b

-- Magic?
SELECT @a = @a ^@b,
@b = @a ^@b,
@a = @a ^@b

-- After
SELECT @a,
@b[/code]


E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

sunsanvin
Master Smack Fu Yak Hacker

1274 Posts

Posted - 2007-10-04 : 03:25:06
Dear Peso, can you please explain me claerly........


please....

Vinod
Even you learn 1%, Learn it with 100% confidence.
Go to Top of Page

sunsanvin
Master Smack Fu Yak Hacker

1274 Posts

Posted - 2007-10-04 : 03:27:24
actually it is swapping....
but i'd like to pass one parameter in switch and based on that, i've to pass values in cases...
for matched one, i need the result

Vinod
Even you learn 1%, Learn it with 100% confidence.
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-10-04 : 03:29:16
Do you have an example, please!?
Almost 350 posts and still clear as mud.


E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

sunsanvin
Master Smack Fu Yak Hacker

1274 Posts

Posted - 2007-10-04 : 03:35:35
Here is the example

swith (grade)
{
case 1 : printf('Fall'); break;
case 2 : printf('bad'); break;
case 3 : printf('good'); break;
case 4 : printf('very good'); break;
default : printf('you have inputed bad');
break;
}

Vinod
Even you learn 1%, Learn it with 100% confidence.
Go to Top of Page

sunsanvin
Master Smack Fu Yak Hacker

1274 Posts

Posted - 2007-10-04 : 03:37:27
i've to pass the value for grade

Vinod
Even you learn 1%, Learn it with 100% confidence.
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-10-04 : 03:45:52
This is EXACTLY the same thing as you posted here
SELECT" rel="nofollow">http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=90448
SELECT	CASE @Grade
WHEN 1 THEN 'Fall.'
WHEN 2 THEN 'Bad.'
WHEN 3 THEN 'Good.'
WHEN 4 THEN 'Very good.'
ELSE 'You have inputed bad.'
END
OR DO YOU MEAN SOME PROCEDURAL IF?[code]IF @Grade = 1
BEGIN
RAISERROR('Fall.', 16, 1)
RETURN
END
ELSE IF @Grade = 2
BEGIN
RAISERROR('Bad.', 16, 1)
RETURN
END
ELSE IF @Grade = 3
BEGIN
RAISERROR('Good.', 16, 1)
RETURN
END
ELSE IF @Grade = 4
BEGIN
RAISERROR('Very good.', 16, 1)
RETURN
END
ELSE
BEGIN
RAISERROR('You have inputed bad.', 16, 1)
RETURN
END



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

jackv
Master Smack Fu Yak Hacker

2179 Posts

Posted - 2007-10-04 : 03:46:52
There is the CASE command , of which there is the simple and searched variety, e.g
SELECT myCol =
CASE type
WHEN 1 THEN 'Fall'
WHEN 2 THEN 'Bad'
ELSE ''
END

FROM myTable


Jack Vamvas
--------------------
Search IT jobs from multiple sources- http://www.ITjobfeed.com/SQL
Go to Top of Page

sunsanvin
Master Smack Fu Yak Hacker

1274 Posts

Posted - 2007-10-04 : 03:53:36
Sorry for troubling you Peso, but the thing is our team is trying to convert the scripts from sql server to oracle to support our applicatio oracle DB also. there i got the doubt regarding wether switch is there or not...that's why the post.

thank you for the great help

Vinod
Even you learn 1%, Learn it with 100% confidence.
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-10-04 : 03:58:59
quote:
Originally posted by sunsanvin

...but the thing is our team is trying to convert the scripts from sql server to oracle to support our applicatio oracle DB also.
The scripts you post has nothing to do with Microsoft SQL Server.
They look more like C/C++/C# code to me.



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-10-04 : 05:16:55
quote:
Originally posted by Peso

DECLARE	@a INT,
@b INT

SELECT @a = 3,
@b = 5

-- Before
SELECT @a,
@b

-- Magic?
SELECT @a = @a ^@b,
@b = @a ^@b,
@a = @a ^@b

-- After
SELECT @a,
@b



E 12°55'05.25"
N 56°04'39.16"



or
DECLARE	@a INT,
@b INT

SELECT @a = 3,
@b = 5

-- Before
SELECT @a,
@b

-- Magic?
SELECT @a = @a + @b,
@b = @a - @b,
@a = @a - @b

-- After
SELECT @a,
@b


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-10-04 : 05:19:01
quote:
Originally posted by sunsanvin

Sorry for troubling you Peso, but the thing is our team is trying to convert the scripts from sql server to oracle to support our applicatio oracle DB also. there i got the doubt regarding wether switch is there or not...that's why the post.

thank you for the great help

Vinod
Even you learn 1%, Learn it with 100% confidence.


switch is there in the form of CASE WHEN at both the places

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

sunsanvin
Master Smack Fu Yak Hacker

1274 Posts

Posted - 2007-10-04 : 05:29:23
Thank you Madhi

Vinod
Even you learn 1%, Learn it with 100% confidence.
Go to Top of Page
   

- Advertisement -