Click here to Skip to main content
15,899,754 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
SQL
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER proc [dbo].[sp_Category122] @id int,@mode char(1)
as
declare @query varchar(5000)
set @query = 'Select * from quest_categories where cat_id = ' + convert(varchar(max),@id)
if @mode ='a'
	begin	
		declare @query1 varchar(5000)
		set @query1=' and cat_name = ''sql'''
		set @query = @query + @query1
	end
Exec(@query)
print(@query)


http://xneuron.wordpress.com/2007/12/05/reusable-data-layer-in-c/
Posted
Updated 22-Feb-11 23:03pm
v3
Comments
Sunasara Imdadhusen 21-Feb-11 6:40am    
Added code formatting!!
Prerak Patel 21-Feb-11 6:54am    
seems ok. do you get any errors? what is the problem?
Manfred Rudolf Bihy 21-Feb-11 8:54am    
Depends on what you're trying to achieve. What is the exact semantic of mode being equal to 'a'?

In order to avoid sql injection I would write something like this:
SQL
ALTER proc [dbo].[sp_Category122] @id int,@mode char(1)
as
declare @query nvarchar(4000)
set @query = 'Select * from quest_categories where cat_id = @id'
if @node='a'
   set @query = @query + ' and cat_name=''sql'''
---
exec sp_executesql @query, N'@id int', @id
 
Share this answer
 
Comments
Yusuf 21-Feb-11 8:57am    
Good answer
_Ashish 1-Mar-11 3:27am    
Good point
tried to get rid of dynamic sql

SQL
SELECT
    *
FROM
    quest_categories
WHERE
    cat_id = @id
    AND ((@mode = 'a' AND cat_name = 'sql') OR @mode <> 'a'))
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900