Solved

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

Posted on 2006-10-30
8
458 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 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
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

Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

Question has a verified solution.

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

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
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.

691 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