Click here to Skip to main content
15,884,836 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Sample SP
My parameter should be like - 'DOB BETWEEN ''1/12/1980'' AND ''1/1/2080''' (Check double quotes)
-- SelectEmp 'DOB BETWEEN ''1/12/1980'' AND ''1/1/2080'''
SQL
ALTER PROCEDURE SelectEmp
	-- Add the parameters for the stored procedure here
	@DOB nvarchar(max)
AS
BEGIN
	SET NOCOUNT ON;
DECLARE @SQL VARCHAR(MAX)
set @Sql = 'select * from emp where '+  @DOB
print @Sql
exec  (@Sql)
END
GO


My .Net Code :-
C#
cn.Open();
               SqlCommand cmd = new SqlCommand("SelectEmp", cn);
               cmd.CommandType = CommandType.StoredProcedure;
               cmd.Parameters.Add("@DOB", SqlDbType.VarChar, 1000);
               cmd.Parameters["@DOB"].Value = "DOB BETWEEN "+"'''"+ "1/12/1980"+"''"+" AND "+ "'''"+ "1/1/2000"+"''"+"";
               SqlDataReader dr;
               dr = cmd.ExecuteReader();
               while (dr.Read())
               {
                   Response.Write("Hi");
               }


I am gettinh Error Incorrect Syntax

Can someone please help me ..
Posted
Updated 11-Apr-11 8:55am
v2

Try to modify the line something like this.
cmd.Parameters["@DOB"].Value = "DOB BETWEEN " + "'" + "1/12/1980" + "'" + " AND " + "'" + "1/1/2000" + "'";


Seems like there are extra '/" quotes.
 
Share this answer
 
Try to modify the line something like this.
cmd.Parameters["@DOB"].Value = "DOB BETWEEN " + "'" + "1/12/1980" + "'" + " AND " + "'" + "1/1/2000" + "'";


Seems like there are extra '/" quotes.
 
Share this answer
 
Hi It worked ...Thanks a lot...
I was thinking too much by Seeing

'DOB BETWEEN ''1/12/1980'' AND ''1/1/2080''' (Check double quotes)
 
Share this answer
 
When I use the string from your line:
C#
cmd.Parameters["@DOB"].Value = "DOB BETWEEN "+"'''"+ "1/12/1980"+"''"+" AND "+ "'''"+ "1/1/2000"+"''"+"";

and, using code, put it in a TextBox I get:
DOB BETWEEN '''1/12/1980'' AND '''1/1/2000''


Hopefully this will help you to spot the error(s)
 
Share this answer
 
Passing a partial SQL statement isn't usually the correct way. This could fail because of several reasons especially when dates are involved. Use 2 separate parameters in your procedure:
SQL
ALTER PROCEDURE SelectEmp
    -- Add the parameters for the stored procedure here
    @DOBstart date,
    @DOBend date
AS
BEGIN
    SET NOCOUNT ON;
    SELECT * FROM EMP WHERE DOB BETWEEN @DOBstart AND @DOBend;
END
GO
 
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