Click here to Skip to main content
15,917,964 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
m executing sql query but i keep on getting this error "Incorrect syntax near '-'."

this sql is for retriving data of a range date.

here is my sql

SQL
ALTER PROCEDURE [dbo].[viewMaterialByDate]
@fromDate date,
@toDate date
AS
Select MaterialID,Material,Topic  FROM StudyMaterial,Topic
WHERE Topic.TopicID= StudyMaterial.TopicID and 
 [Date] >=@fromDate and [Date] <= @toDate
Posted

try the below as you might forgot to add braces to the parameters:

SQL
ALTER PROCEDURE [dbo].[viewMaterialByDate]
(@fromDate date,
@toDate date)
AS
Select MaterialID,Material,Topic  FROM StudyMaterial,Topic
WHERE Topic.TopicID= StudyMaterial.TopicID and
 [Date] >=@fromDate and [Date] <= @toDate
 
Share this answer
 
Comments
W Balboos, GHB 26-Aug-13 9:16am    
I noticed that, too (you and I were posting together). Not knowing the database he was using, I deferred to the ALTER as an indication that CREATE already worked - perhaps his database accepts that syntax as it seems unambiguous,
Since you're at the stage of an ALTER PROCEDURE, I presume the SP is part of the database (odd that you name a SP a view).

That being said, it must be the data entry that is causing the error - do you have a sample of an entry that fails? Have you tried this with hard-coded dates as the range or is this the result of something like a program loop (which would require you looking into your data feed)?

Another thought: why not an INNER JOIN between StudyMaterial and Topic tables since you require a match for all of your query return values?

 
Share this answer
 
The issue may be with the date values which you are passing because there is nothing else in your query which may cause an error. Try giving hard-coded values for testing to the same query and then run it; if it works then it is obviously the issue with date parameters.
 
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