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 |
|
craigmac999
Starting Member
14 Posts |
Posted - 2007-08-15 : 07:16:32
|
| hi i have a stored procedure which i want to add a if statement to thw where clasuewhere Code = 'ghht'if @category <> '%' and catid = @categoryORDER BY Codebut i am not sure how to do the if statement in sql? |
|
|
pootle_flump
1064 Posts |
Posted - 2007-08-15 : 07:30:37
|
| HiThat does not make sense I am afraid. Since this is a table there will be lots of different values for catid. As such the statement does not simply evaluate to true or false. |
 |
|
|
craigmac999
Starting Member
14 Posts |
Posted - 2007-08-15 : 07:50:11
|
| ok i have this select state belowand if the parameter @category = % then i need to select all where Code = 'ddgg' else select rows that St_Cat_1 = @categoryhow can i do this>? |
 |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2007-08-15 : 08:01:47
|
| http://www.timfanelli.com/images/no_idea.jpgwhere (@category = '%' and code = 'ddgg')OR (@category <> '%' and st_cat_1 = @category)Jim |
 |
|
|
craigmac999
Starting Member
14 Posts |
Posted - 2007-08-15 : 09:14:49
|
| ok thanks i now have the code below which works if a "1" is not supplied, but when a "1" is supplied it is showing no results, i need it to show all categoriesFROM dbo.STOCK WHERE Code = 'AE' AND Level <> 0AND (@category <> '1' and st_cat_1 = @category)ORDER BY Code so if the @category = 1 only have WHERE Code = 'AE' AND Level <> 0if the @category <> 1 have WHERE Code = 'AE' AND Level <> 0 and st_cat_1 = @category |
 |
|
|
Ifor
Aged Yak Warrior
700 Posts |
Posted - 2007-08-15 : 09:25:42
|
| [code]WHERE Code = 'AE' AND [Level] <> 0 AND ( @category = 1 OR st_cat_1 = @category )[/code] |
 |
|
|
craigmac999
Starting Member
14 Posts |
Posted - 2007-08-16 : 03:07:08
|
| ok i am getting an error with thatwhat i need is IF @category has a value of 1 then WHERE Code = 'AE' AND STS.St_Level = 0 ELSEWHERE Code = 'AE' AND STS.St_Level = 0 AND st_cat_1 = @categoryORDER BY STS.St_Code i dont think AND (@category = 1 OR st_cat_1 = @category) is working correctly? |
 |
|
|
pootle_flump
1064 Posts |
Posted - 2007-08-16 : 06:10:41
|
| What's your error? |
 |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2007-08-16 : 06:27:58
|
| WHERE (Code = 'AE' AND STS.St_Level = 0 AND @category = 1)OR (Code = 'AE' AND STS.St_Level = 0 AND st_cat_1 = @category AND @category <> 1 |
 |
|
|
|
|
|