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.
| Author |
Topic |
|
sqllover
Constraint Violating Yak Guru
338 Posts |
Posted - 2007-02-08 : 22:48:17
|
| hi, this is my procedure.CREATE procedure yard_retrieve(@yard varchar(50),@vessel varchar(50),@customer varchar(50))ASbeginIF EXISTS (select code_description from codeMaster where active='1' and code_description=@yard and category_code=(select category_code from categoryMaster where category_description='Yard') ) select 'found' as resultelseselect 'not found' as reaultend beginIF EXISTS (select code_description from codeMaster where active='1' and code_description=@vessel and category_code=(select category_code from categoryMaster where category_description='vessel'))select 'found' as reault1elseselect 'not found' as reault1end beginIF EXISTS (select code_description from codeMaster where active='1' and code_description=@customer and category_code=(select category_code from categoryMaster where category_description='customer'))select 'found' as reault2elseselect 'not found' as reault2endGOif i run this procedure i need the result as followingresult result1 result2found notfound foundis this posible to do using select case ? please help me to get my need please |
|
|
davidmal
Starting Member
19 Posts |
Posted - 2007-02-08 : 23:04:02
|
| Can't really get a feel for what exactly the procedure is doing or the DB schema but the SQL equvalent to select case is:CASE (field) WHEN (expression) THEN (returnvalue) [ELSE (returnvalue)] ENDHere's a simple example:SELECT title, price, Budget = CASE price WHEN price > 20.00 THEN 'Expensive' WHEN price BETWEEN 10.00 AND 19.99 THEN 'Moderate' WHEN price < 10.00 THEN 'Inexpensive' ELSE 'Unknown' END,FROM titles Hope that helps. |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-02-09 : 00:47:41
|
[code]CREATE procedure yard_retrieve(@yard varchar(50),@vessel varchar(50),@customer varchar(50))ASbegindeclare @result varchar(10), @result1 varchar(10), @result2 varchar(10)IF EXISTS (select code_description from codeMaster where active='1' and code_description=@yard and category_code=(select category_code from categoryMaster where category_description='Yard') )select @result = 'found'elseselect @result = 'not found' endbeginIF EXISTS (select code_description from codeMaster where active='1' and code_description=@vessel and category_code=(select category_code from categoryMaster where category_description='vessel'))select @result1 = 'found' elseselect @result1 = 'not found'endbeginIF EXISTS (select code_description from codeMaster where active='1' and code_description=@customer and category_code=(select category_code from categoryMaster where category_description='customer'))select @result2 = 'found' elseselect @result2 = 'not found'select @result as result, @result1 as result1, @result2 as result2endGO[/code] KH |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-02-09 : 01:21:27
|
| [code]CREATE FUNCTION dbo.fnYardRetrieve( @Yard VARCHAR(50), @Vessel VARCHAR(50), @Customer VARCHAR(50))RETURNS BITASBEGIN DECLARE @Found BIT IF EXISTS ( SELECT cd.Code_Description FROM CodeMaster AS cd INNER JOIN CategoryMaster AS ct ON ct.Category_Code = cd.Category_Code WHERE CASE WHEN cd.Code_Description = ISNULL(@Yard, cd.Code_Description) AND dt.Category_Description = 'Yard' THEN 1 WHEN cd.Code_Description = ISNULL(@Vessel, cd.Code_Description) AND ct.Category_Description = 'Vessel' THEN 1 WHEN cd.Code_Description = ISNULL(@Customer, cd.Code_Description) AND ct.Category_Description = 'Customer'THEN 1 ELSE 0 END = 1 AND cd.Active = '1' ) SELECT @Found = 1 RETURN ISNULL(@Found, 0)END[/code]Call with[code]SELECT CASE WHEN dbo.fnYardRetrieve(@Yard, @Vessel, @Customer) = 1 THEN 'Found' ELSE 'Not found' END[/code]Peter LarssonHelsingborg, Sweden |
 |
|
|
sqllover
Constraint Violating Yak Guru
338 Posts |
Posted - 2007-02-09 : 01:31:18
|
| hi peso and khtan thank you for answering |
 |
|
|
|
|
|
|
|