SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Dyanmic Condition in Stored Procedure?
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

shaoen01
Yak Posting Veteran

78 Posts

Posted - 01/06/2006 :  04:57:55  Show Profile  Reply with Quote
What i want to do is use a parameter that takes in a condition like "NOT LIKE" or "LIKE". Because i have 2 applications that uses the same kind of data and the only difference between the 2 sql statements is "LIKE" and "NOT LIKE".

Is there way around this? Below is my stored procedure for "NOT LIKE":
CREATE PROCEDURE dbo.SEARCHSALEITEM
(
    @BRAND VARCHAR(100),
    @ITEMID VARCHAR(100),
    @ITEMNAME VARCHAR(100)
)
AS
SELECT *
FROM MSTRITEM AS M JOIN ITEMIMAG AS I
ON I.ITEMID = M.ITEMID
WHERE (M.ADDTOSALEITEM=1) AND 
((M.BRAND LIKE '%' + @BRAND + '%') OR (M.ITEMNAME LIKE '%' + @ITEMNAME + '%'))
AND ',' + @ITEMID + ',' NOT LIKE '%,' + CONVERT(VARCHAR(20), M.ITEMID) + ',%'

GO

madhivanan
Premature Yak Congratulator

India
22744 Posts

Posted - 01/06/2006 :  05:12:40  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote
If you want to use the same procedure, add an input parameter which accept value as Like or Not like.

CREATE PROCEDURE dbo.SEARCHSALEITEM_TEST
(
    @BRAND VARCHAR(100),
    @ITEMID VARCHAR(100),
    @ITEMNAME VARCHAR(100),
    @LIKE VARCHAR(10)

)
AS
If @LIKE='LIKE'
	SELECT *
	FROM MSTRITEM AS M JOIN ITEMIMAG AS I
	ON I.ITEMID = M.ITEMID
	WHERE (M.ADDTOSALEITEM=1) AND 
	((M.BRAND LIKE '%' + @BRAND + '%') OR (M.ITEMNAME LIKE '%' + @ITEMNAME + '%'))
	AND ',' + @ITEMID + ',' LIKE '%,' + CONVERT(VARCHAR(20), M.ITEMID) + ',%'

else If @LIKE='NOT LIKE'
	SELECT *
	FROM MSTRITEM AS M JOIN ITEMIMAG AS I
	ON I.ITEMID = M.ITEMID
	WHERE (M.ADDTOSALEITEM=1) AND 
	((M.BRAND LIKE '%' + @BRAND + '%') OR (M.ITEMNAME LIKE '%' + @ITEMNAME + '%'))
	AND ',' + @ITEMID + ',' NOT LIKE '%,' + CONVERT(VARCHAR(20), M.ITEMID) + ',%'
GO


Madhivanan

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

shaoen01
Yak Posting Veteran

78 Posts

Posted - 01/06/2006 :  06:33:07  Show Profile  Reply with Quote
It works fine! Thanks
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.09 seconds. Powered By: Snitz Forums 2000