Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2006-10-30
8
Medium Priority
?
467 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 29

Accepted Solution

by:
Nightman earned 2000 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
LVL 143

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 143

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 143

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

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

609 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