Solved

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

Posted on 2006-10-30
8
444 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Suggested Solutions

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

863 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

27 Experts available now in Live!

Get 1:1 Help Now