Groups | Blog | Home
all groups > sql server programming > july 2004 >

sql server programming : 'select case' in stored procedure ?


Louis Davidson
7/20/2004 10:27:57 AM
You can use any T-SQL Syntax in a stored procedure. Give us a sample of
what you are trying to do and we can help you.

--
----------------------------------------------------------------------------
Louis Davidson (drsql@hotmail.com)
Compass Technology Management

Pro SQL Server 2000 Database Design
http://www.apress.com/book/bookDisplay.html?bID=266

Note: Please reply to the newsgroups only unless you are
interested in consulting services. All other replies will be ignored :)

[quoted text, click to view]

Raymond D'Anjou (raydan)
7/20/2004 11:18:46 AM
Example using the Northwind database:
select orderID, case when shippedDate is null then 'Not shipped' else
'Shipped' end from orders

[quoted text, click to view]

Narayana Vyas Kondreddi
7/20/2004 4:28:21 PM
A quick example:

SELECT EmpID, EmpName,
CASE Sex
WHEN 'm' THEN 'Male'
WHEN 'f' THEN 'Female'
ELSE 'Unspecified'
END AS 'Sex'
FROM Employees

--
HTH,
Vyas, MVP (SQL Server)
http://vyaskn.tripod.com/
Is .NET important for a database professional?
http://vyaskn.tripod.com/poll.htm


[quoted text, click to view]
Can I use select case in "stored procedure ???
If yes, can someone be kind to give me a sample ??
Thanks
From SQL-newbie


Agnes
7/20/2004 11:20:07 PM
Can I use select case in "stored procedure ???
If yes, can someone be kind to give me a sample ??
Thanks
From SQL-newbie

Narayana Vyas Kondreddi
7/21/2004 8:13:57 AM
Yes, something like this:

UPDATE TableName
SET ColumnName = CASE WHEN <Condition1> THEN <Value1> WHEN <Condition2> THEN
'Value2' ELSE 'Value3' END
WHERE <Some condition to select the rows>
--
Vyas, MVP (SQL Server)
http://vyaskn.tripod.com/


[quoted text, click to view]

Roji. P. Thomas
7/21/2004 12:03:57 PM
Yes

UPDATE Table
SET Column = CASE WHEN <condition> THEN value ELSE Someothervalue END


--
Roji. P. Thomas
Net Asset Management
https://www.netassetmanagement.com


[quoted text, click to view]

Agnes
7/21/2004 1:12:56 PM
I understand and try it later . Thanks
this syntax seems quit useful. as i don't need to create several store
procedure
"Narayana Vyas Kondreddi" <answer_me@hotmail.com> ¦b¶l¥ó
news:eXnTy4mbEHA.3596@tk2msftngp13.phx.gbl ¤¤¼¶¼g...
[quoted text, click to view]

Agnes
7/21/2004 2:06:23 PM
How about update statment ?
After i select * from table, i need to update it at once.. but It depends on
the condition,
Can update got the 'case' also ??

"Agnes" <agnes@dynamictech.com.hk> ¦b¶l¥ó
news:eiIlaFubEHA.2812@tk2msftngp13.phx.gbl ¤¤¼¶¼g...
[quoted text, click to view]

Agnes
7/21/2004 3:32:38 PM
I need to specified the column ??
infact, i got several column may be update (which depends on the case
parameter)
it seems wrong syntax if my code like this :-
update mytable
case @type
when 'A' then a = a + 1
when 'B' then b = b + 1
end

"Narayana Vyas Kondreddi" <answer_me@hotmail.com> ¦b¶l¥ó
news:u$7yJJvbEHA.2292@TK2MSFTNGP09.phx.gbl ¤¤¼¶¼g...
[quoted text, click to view]

Louis Davidson
7/21/2004 4:02:07 PM
This is why posting what you have in mind is so helpful.

No, you cannot do this. SQL is not that kind of language. You can easily
to this using something like:

update mytable
set A = case when @type = 'a' then A + 1 else A end,
set B = case when @type = 'b' then B + 1 else B end,
where ...

It could also be done using dynamic SQL, but that is very messy.


--
----------------------------------------------------------------------------
Louis Davidson (drsql@hotmail.com)
Compass Technology Management

Pro SQL Server 2000 Database Design
http://www.apress.com/book/bookDisplay.html?bID=266

Note: Please reply to the newsgroups only unless you are
interested in consulting services. All other replies will be ignored :)

[quoted text, click to view]

AddThis Social Bookmark Button