Solved

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

Posted on 2006-10-30
8
454 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
Independent Software Vendors: 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how the fundamental information of how to create a table.

733 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