Click here to Skip to main content
15,909,898 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi,

i am writing the stored procedure like

SQL
ALTER proc [dbo].[testmultiplevalues](@emailid varchar(max)=null, @currlocation varchar(max)=null,@gender varchar(max)=null)
as
begin
declare @selquery varchar(max)
declare @concat varchar(max)
set @concat = ' '
set @selquery='select Emailid, currentlocation,Gender from userdetails where '
if(@emailid is not null)
begin
set @selquery=@selquery + 'Emailid=' +@emailid
set @concat=' and '
end
if(@currlocation is not null)
begin
set @selquery=@selquery + @concat + 'currentlocation=' +@currlocation
set @concat=' and '
end
if(@gender is not null)
begin
set @selquery =@selquery + @concat +'Gender=' + @gender
end
exec @selquery
end


proc created successfully.but when iam executing the proc iam getting the error as


Msg 2812, Level 16, State 62, Procedure testmultiplevalues, Line 22
Could not find stored procedure 'select Emailid, currentlocation,Gender from userdetails where Emailid=nsaritha34@gmail.com and currentlocation=Kolkata and Gender=Female'.



please help me
Posted
Updated 17-Nov-11 21:49pm
v2
Comments
Mehdi Gholam 18-Nov-11 3:49am    
EDIT -> fixed formatting
Mehdi Gholam 18-Nov-11 3:51am    
This is a bad use of stored procedures, you are better off just creating the query and executing it as normal.

put your sql variable at the end of the code, inside the braces like.

SQL
exec (@selquery)


follow the like to learn more about the EXEC,EXEC() and EXECUTE.

http://blog.sqlauthority.com/2007/09/13/sql-server-difference-between-exec-and-execute-vs-exec-use-execexecute-for-sp-always/[^]


mark as answer if solves your problem, it motivates :)
 
Share this answer
 
v2
In addition to the other answers, you also need to string qualify some of your variables

Your SQL is currently being built as...

SQL
select Emailid, currentlocation,Gender from userdetails where Emailid=nsaritha34@gmail.com and currentlocation=Kolkata and Gender=Female


It would need to read more like

SQL
select Emailid, currentlocation,Gender from userdetails where Emailid='nsaritha34@gmail.com' and currentlocation='Kolkata' and Gender='Female'
 
Share this answer
 
Obviously you have no clue as to what you are doing...
Your query does not make optimal use of query caching (which makes it slower), it's prone to SQL Injection and it's impossible to read.
Actually, what you are doing there makes no sense at all.
I suggest you read up on Stored Procedures for beginners[^].
After that you might see the error of your ways[^]...
 
Share this answer
 
Try this

SQL
alter proc [dbo].[testmultiplevalues](@emailid varchar(max)=null, @currlocation varchar(max)=null,@gender varchar(max)=null)
as
begin
    declare @selquery varchar(max)
    declare @concat varchar(max)
    set @concat = ' '
    set @selquery='select Emailid, currentlocation,Gender from userdetails where '
    if(@emailid is not null)
    begin
        set @selquery=@selquery + 'Emailid=' +char(39)+@emailid+ char(39)
        set @concat=' and '
    end
    if(@currlocation is not null)
    begin
        set @selquery=@selquery + @concat + 'currentlocation=' +char(39)+@currlocation+char(39)
        set @concat=' and '
    end
    if(@gender is not null)
    begin
        set @selquery =@selquery + @concat +'Gender=' + char(39)+@gender+char(39)
    end
    exec sp_executesql @selquery
end
 
Share this answer
 
v2
i missed brackets at exec (variable)
 
Share this answer
 

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