Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm not able execute my stored procedure which is
SQL
@Name Nvarchar(250),
@Country Nvarchar(250),
@State Nvarchar(250),
@Speciality Nvarchar(250),
@Email Nvarchar(250),
@Grade Nvarchar(250)

as

BEGIN

DECLARE @sql AS NVarchar(4000)

    SET @sql = 'SELECT * FROM Doctorslist where (1=1)'
    
    if @Name is not null
    set @sql = @sql + ' And (DoctorName = @Name)'
    
    if @Country is not null
    set @sql = @sql+ ' And (Country = @Country)'
        
    if @State is not null
    set @sql = @sql + ' And (State = @State)'
        
    if @Speciality is not null
    set @sql = @sql + ' And (Speciality = @Speciality)'
 
EXEC(@sql)
END

While executing this stored procedure i'm getting error like this

SQL
Msg 137, Level 15, State 2, Line 1
Must declare the scalar variable "@Country".

(1 row(s) affected)


So can anybody help me to solve this problem?
Posted
Comments
Ankur\m/ 7-Aug-13 2:22am    
Before EXEC(@sql), add print @sql and see the query that is generated. Try to execute that query in the query analyzer and see what's wrong.

Replace:
SQL
set @sql = @sql + ' And (DoctorName = @Name)'

with:
SQL
set @sql = @sql + ' And (DoctorName =''' +  @Name + ''')'

Do the same in next lines.
 
Share this answer
 
Comments
Ankur\m/ 7-Aug-13 6:48am    
Correct, 5!
Maciej Los 7-Aug-13 6:55am    
Thank you ;)
Try with modified code

SQL
SET @sql = 'SELECT * FROM Doctorslist where (1=1)'

    if @Name is not null
    set @sql = @sql + ' And (DoctorName ='''+ @Name+''')'

    if @Country is not null
    set @sql = @sql+ ' And (Country = '''+@Country +''')'

    if @State is not null
    set @sql = @sql + ' And (State ='''+ @State+''')'

    if @Speciality is not null
    set @sql = @sql + ' And (Speciality ='''+ @Speciality+''')'
 
Share this answer
 
Comments
Ankur\m/ 7-Aug-13 6:48am    
5!
Look at the code where you execute the SP: you have either not set the @Country parameter, or you have misspelled it.
 
Share this answer
 
Comments
Maciej Los 7-Aug-13 2:30am    
I suppose that is a Store Procedure and variables @Country, @Name, @State, etc. are input parameters.
Cheers!
Maciej
Thanks to all Actually I solved this problem by adding like this

SQL
@DoctorName varchar(255),
@Degree varchar(255),
@Speciality varchar(255),
@TelNumber varchar(255),
@District varchar(255),
@State varchar(255),
@Country varchar(255),
@Email varchar(255)

AS

BEGIN

DECLARE @sql AS NVarchar(4000)

    SET @sql = 'select DoctorName,Degree,Speciality,TelNumber,Taluk,District,State,Country from Doctorslist where (1=1)'
    
    if @DoctorName is not null
    set @sql = @sql + ' And DoctorName =''' + @DoctorName + ''''
    
    if @Degree is not null
    set @sql = @sql + ' And Degree =''' + @Degree + ''''
    
    if @Speciality is not null
    set @sql = @sql + ' And Speciality =''' + @Speciality + ''''
    
    if @TelNumber is not null
    set @sql = @sql + ' And TelNumber =''' + @TelNumber + ''''
    
    if @District is not null
    set @sql = @sql + ' And District =''' + @District + ''''
        
    if @State is not null
    set @sql = @sql + ' And State =''' + @State + ''''
    
    if @Country is not null
    set @sql = @sql + ' And Country =''' + @Country + ''''
        
    if @Email is not null
    set @sql = @sql + ' And Email =''' + @Email + ''''
 
EXEC(@sql)

END
Mr. Maciej Los is helped me to do this changes.
 
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