Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


stored procedure based on conditions


stored procedure based on conditions

Author
Message
Johann Montfort
Johann Montfort
SSC-Enthusiastic
SSC-Enthusiastic (169 reputation)SSC-Enthusiastic (169 reputation)SSC-Enthusiastic (169 reputation)SSC-Enthusiastic (169 reputation)SSC-Enthusiastic (169 reputation)SSC-Enthusiastic (169 reputation)SSC-Enthusiastic (169 reputation)SSC-Enthusiastic (169 reputation)

Group: General Forum Members
Points: 169 Visits: 107
Dear All

How can I write a stored procedure based on condition. At the moment, my code is not working. Here is my example

ALTER PROCEDURE [dbo].[FindStrings]
(
@uid bit, -- unique string
@str nvarchar(1000), --string to be searched
@ulang nvarchar(20), -- language id
@pid int -- projectid
)
AS

DECLARE @strSQL varchar(4000)

SET NOCOUNT ON

SELECT DISTINCT pr.fk_resourceId, p.pageId, p.pageTitle, lang.langCode AS Language
FROM PageResources AS pr INNER JOIN
Pages AS p ON pr.fk_pageId = p.pageId INNER JOIN
LString AS lc ON pr.fk_resourceId = lc.fk_resourceId INNER JOIN
Languages AS lang ON lc.fk_langID = lang.langId
WHERE (p.fk_projectId = @pid)

IF @ulang <> 'al'
AND lang.langCode = @ulang

IF @uid=0
AND lc. string LIKE '%' + @str + '%'
IF @uid=1
AND lc. string = @str

ORDER BY pr.fk_resourceId

Thanks for your help and time

Johann
Grant Fritchey
Grant Fritchey
SSCoach
SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)

Group: General Forum Members
Points: 19529 Visits: 32331
Johann Montfort (7/2/2008)
Dear All

How can I write a stored procedure based on condition. At the moment, my code is not working. Here is my example

ALTER PROCEDURE [dbo].[FindStrings]
(
@uid bit, -- unique string
@str nvarchar(1000), --string to be searched
@ulang nvarchar(20), -- language id
@pid int -- projectid
)
AS

DECLARE @strSQL varchar(4000)

SET NOCOUNT ON

SELECT DISTINCT pr.fk_resourceId, p.pageId, p.pageTitle, lang.langCode AS Language
FROM PageResources AS pr INNER JOIN
Pages AS p ON pr.fk_pageId = p.pageId INNER JOIN
LString AS lc ON pr.fk_resourceId = lc.fk_resourceId INNER JOIN
Languages AS lang ON lc.fk_langID = lang.langId
WHERE (p.fk_projectId = @pid)

IF @ulang <> 'al'
AND lang.langCode = @ulang

IF @uid=0
AND lc. string LIKE '%' + @str + '%'
IF @uid=1
AND lc. string = @str

ORDER BY pr.fk_resourceId

Thanks for your help and time

Johann


If I understand what you're asking for, you need a CASE statement in the where clause. You can't use IF conditions there. Something like this:

SELECT DISTINCT
pr.fk_resourceId,
p.pageId,
p.pageTitle,
lang.langCode AS Language
FROM PageResources AS pr
INNER JOIN Pages AS p
ON pr.fk_pageId = p.pageId
INNER JOIN LString AS lc
ON pr.fk_resourceId = lc.fk_resourceId
INNER JOIN Languages AS lang
ON lc.fk_langID = lang.langId
WHERE ( p.fk_projectId = @pid )
AND lang.langcode = CASE WHEN @ulang = 'al' THEN @ulang
ELSE lang.langCode
END
...



----------------------------------------------------
The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood...
Theodore Roosevelt

The Scary DBA
Author of: SQL Server Query Performance Tuning and SQL Server Execution Plans
Product Evangelist for Red Gate Software
Johann Montfort
Johann Montfort
SSC-Enthusiastic
SSC-Enthusiastic (169 reputation)SSC-Enthusiastic (169 reputation)SSC-Enthusiastic (169 reputation)SSC-Enthusiastic (169 reputation)SSC-Enthusiastic (169 reputation)SSC-Enthusiastic (169 reputation)SSC-Enthusiastic (169 reputation)SSC-Enthusiastic (169 reputation)

Group: General Forum Members
Points: 169 Visits: 107
Thanks Grant

I tried this

WHERE (p.fk_projectId = @pid)
AND lang.langcode =
CASE
WHEN @ulang <> 'al' THEN @ulang
END
AND lc.locstring =
CASE
WHEN @uid = 0 THEN LIKE '%' + @str + '%'
WHEN @uid = 1 THEN = @str
END

but its throwing an error near the LIKE

How can I fix that?

Thanks again
Johann Montfort
Johann Montfort
SSC-Enthusiastic
SSC-Enthusiastic (169 reputation)SSC-Enthusiastic (169 reputation)SSC-Enthusiastic (169 reputation)SSC-Enthusiastic (169 reputation)SSC-Enthusiastic (169 reputation)SSC-Enthusiastic (169 reputation)SSC-Enthusiastic (169 reputation)SSC-Enthusiastic (169 reputation)

Group: General Forum Members
Points: 169 Visits: 107
ok solved it

someone suggested this on another forum:-

AND @ulang <> 'al' AND lang.langCode = @ulang
and ((@uid=0 AND lc.string LIKE '%' + @str + '%') or (@uid=1 AND lc.string = @str))
Grant Fritchey
Grant Fritchey
SSCoach
SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)

Group: General Forum Members
Points: 19529 Visits: 32331
Great. If you did need to pursue the CASE statements, you can't use "= LIKE." So you'd have to change

AND lc.locstring = 



to

AND lc.locstring LIKE 



----------------------------------------------------
The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood...
Theodore Roosevelt

The Scary DBA
Author of: SQL Server Query Performance Tuning and SQL Server Execution Plans
Product Evangelist for Red Gate Software
Johann Montfort
Johann Montfort
SSC-Enthusiastic
SSC-Enthusiastic (169 reputation)SSC-Enthusiastic (169 reputation)SSC-Enthusiastic (169 reputation)SSC-Enthusiastic (169 reputation)SSC-Enthusiastic (169 reputation)SSC-Enthusiastic (169 reputation)SSC-Enthusiastic (169 reputation)SSC-Enthusiastic (169 reputation)

Group: General Forum Members
Points: 169 Visits: 107
thanks Grant

Johann
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search