Solved

Stored Procedure Microsoft OLE DB Provider for ODBC Drivers error '80040e14' Incorrect syntax

Posted on 2006-10-30
8
442 Views
Last Modified: 2008-03-10
Please tell me what is wrong with this stored procedure


Microsoft OLE DB Provider for ODBC Drivers error '80040e14'

[Microsoft][ODBC SQL Server Driver][SQL Server]Line 9: Incorrect syntax near 'Class_Events'.

If I do the same exact query in query analyzer it works I have no idea what is going wrong


CREATE PROCEDURE usp_Students_Report_Enroll
@class_number varchar(32),
@dept_id varchar(32)=0,
@sortby varchar(128) = 'last_name, first_name',
@oby varchar(32) = 'ASC'

AS
DECLARE @s varchar(1024)
SET @s = ' SELECT     dbo.department.department_name,dbo.bureau.bureau_name, dbo.Instructors.Instructor_name, dbo.Classes.deleted_class, dbo.Course_Definition.mandatory, dbo.Employee.lastname, dbo.Employee.firstname, dbo.Classes.Class_Number,
                          (SELECT     TOP 1 Class_events.Event_Type
                            FROM          dbo.Class_Events
                            WHERE      dbo.Class_Events.Class_Number = dbo.Classes.Class_Number
                            ORDER BY CASE WHEN dbo.Class_Events.event_type = ''Passed Course'' THEN 1 WHEN dbo.Class_Events.event_type = ''Past Test'' THEN 2 WHEN
                                                    dbo.Class_Events.event_type = ''Attempted Test'' THEN 3 ELSE 4 END DESC) AS Event_Type,
                      CASE WHEN (dbo.Classes.ongoing =0) THEN Classes.Class_Start_Date WHEN dbo.classes.ongoing =1 THEN
                          (SELECT     TOP 1 Class_events.Event_Date
                            FROM          dbo.Class_Events
                            WHERE      dbo.Class_Events.Class_Number = dbo.Classes.Class_Number
                            ORDER BY CASE WHEN dbo.Class_Events.event_type = ''Passed Course'' THEN 1 WHEN dbo.Class_Events.event_type = ''Past Test'' THEN 2 WHEN
                                                    dbo.Class_Events.event_type = ''Attempted Test'' THEN 3 ELSE 4 END) END AS completion_date,
                      CASE WHEN dbo.Classes.Status =''Classroom'' THEN ''Web-Assisted'' WHEN dbo.classes.status = ''OPEN'' THEN ''Web-Based'' ELSE ''All Other'' END AS Type,
                       CASE WHEN
                      dbo.Classes.ongoing =0 THEN Classes.Class_Start_Date WHEN dbo.Classes.ongoing=1 THEN Activity.Start_Date END AS Start_date,
                      dbo.Classes.teacher, dbo.Classes.Class_Start_Date, dbo.Classes.department_id, dbo.Classes.teacher AS Expr1, dbo.Students.Student_Number,
                      dbo.Students.Bureau_ID, dbo.Course_Definition.Course_Description, dbo.Course_Definition.Standard_Hours, dbo.Classes.Course_Number
FROM         dbo.Students INNER JOIN
                      dbo.Activity ON dbo.Students.Student_Number = dbo.Activity.Student_Number INNER JOIN
                  dbo.bureau ON dbo.Students.Bureau_ID=dbo.bureau.bureau_id INNER JOIN
                      dbo.Employee ON dbo.Students.employee_id = dbo.Employee.employee_ID INNER JOIN
                      dbo.Classes ON dbo.Activity.Class_Number = dbo.Classes.Class_Number INNER JOIN
                      dbo.Course_Definition ON dbo.Course_Definition.Course_Def_Number = dbo.Classes.Course_Number INNER JOIN
                      dbo.department ON dbo.Classes.department_id = dbo.department.department_id INNER JOIN
             dbo.Instructors ON dbo.Classes.Instructor_Number=dbo.Instructors.Instructor_number
WHERE     dbo.classes.Class_Number=' + @class_number
SET @s = @s +'  order by ' + @sortby
if @oby <> ''
      SET @s = @s + ' ' + @oby

EXEC(@s)
GO
0
Comment
Question by:Scripter25
  • 4
  • 3
8 Comments
 
LVL 29

Accepted Solution

by:
Nightman earned 500 total points
ID: 17837160
change
DECLARE @s varchar(1024)
to
DECLARE @s nvarchar(4000)
0
 
LVL 29

Expert Comment

by:Nightman
ID: 17837172
your string is too long, so the SQL statement is getting truncated - resulting in a syntax error (because it is incomplete)

In future for debugging, replace EXEC (@s) with PRINT @s and see the output of what you would be executing.
0
 

Author Comment

by:Scripter25
ID: 17837198
You are correct thank you It is the small things that we over look while in a rush
0
 
LVL 29

Expert Comment

by:Nightman
ID: 17837218
don't laugh, but I once spent over an hour trying to figure out why my new sp didn't execute correctly - turned out I was connected to the wrong server ;o)
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 17837221
any better with this:

CREATE PROCEDURE usp_Students_Report_Enroll
@class_number varchar(32),
@dept_id varchar(32)=0,
@sortby varchar(128) = 'last_name, first_name',
@oby varchar(32) = 'ASC'

AS
DECLARE @s varchar(1024)
SET @s = ' SELECT     d.department_name, b.bureau_name, i.Instructor_name, c.deleted_class, cd.mandatory, e.lastname, e.firstname, c.Class_Number,
                          (SELECT     TOP 1 ce.Event_Type
                            FROM          dbo.Class_Events ce
                            WHERE      ce.Class_Number = c.Class_Number
                            ORDER BY CASE WHEN ce.event_type = ''Passed Course'' THEN 1
                                                    WHEN ce.event_type = ''Past Test'' THEN 2
                                                    WHEN ce.event_type = ''Attempted Test'' THEN 3
                                                    ELSE 4 END DESC
                        ) AS Event_Type,
                      CASE WHEN c.ongoing =0 THEN c.Class_Start_Date
                               WHEN c.ongoing =1 THEN
                          (SELECT     TOP 1 ce.Event_Date
                            FROM          dbo.Class_Events ce
                            WHERE      ce.Class_Number = c.Class_Number
                            ORDER BY CASE WHEN ce.event_type = ''Passed Course'' THEN 1
                                                     WHEN ce.event_type = ''Past Test'' THEN 2
                                                     WHEN ce.event_type = ''Attempted Test'' THEN 3
                                                     ELSE 4 END
                           )
                   END AS completion_date,
                   CASE WHEN c.Status =''Classroom'' THEN ''Web-Assisted''
                            WHEN c.status = ''OPEN'' THEN ''Web-Based''
                            ELSE ''All Other''
                      END AS Type,
                    CASE WHEN c.ongoing =0 THEN c.Class_Start_Date
                             WHEN c.ongoing=1 THEN a.Start_Date
                       END AS Start_date,
                      c.teacher, c.Class_Start_Date, c.department_id, s.Student_Number,
                      s.Bureau_ID, cd.Course_Description, cd.Standard_Hours, c.Course_Number
FROM         dbo.Students s
INNER JOIN  dbo.Activity a
  ON s.Student_Number = a.Student_Number
INNER JOIN dbo.bureau b
   ON s.Bureau_ID=b.bureau_id
INNER JOIN dbo.Employee e
   ON s.employee_id = e.employee_ID
INNER JOIN  dbo.Classes c
   ON a.Class_Number =  c.Class_Number
INNER JOIN   dbo.Course_Definition cd
   ON cd.Course_Def_Number = c.Course_Number
INNER JOIN  dbo.department d
  ON c.department_id = d.department_id
INNER JOIN  dbo.Instructors i
  ON c.Instructor_Number=i.Instructor_number
WHERE     c.Class_Number=' + @class_number

SET @s = @s +'  order by ' + @sortby
if @oby <> ''
     SET @s = @s + ' ' + @oby

EXEC(@s)
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 17837238
I see the issue is already solved :)

good job, Nightman
0
 
LVL 29

Expert Comment

by:Nightman
ID: 17837308
Thanks angelIII

statement length is the first thing I check with dynamic SQL syntax errors - it's almost ALWAYS overlooked ;)
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 17837333
yes, I usually check it also...

Scripter25, with my suggestion you might learn how to use table alias names, which makes your query shorter and usually more readable...
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now