Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Problems using the ORDER BY clause in a SP

Author  Topic 

Gyto
Posting Yak Master

144 Posts

Posted - 2007-11-07 : 07:08:22
Hi there,

Whenever I run this stored procedure I get the error message:

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

[Microsoft][ODBC SQL Server Driver][SQL Server]Syntax error converting the varchar value 'INSERT INTO tProjectDetailReportData (ProjDesc, ProjAnticCompDate, ProjManager, ProjSponsor, ProjType, ProjDept) SELECT tProject.ProjDesc, tProject.ProjAnticCompDate, tProjectManager.ProjectManagerName, tProjectSponsor.ProjectSponsorName, tProjectType.ProjectType, tDepartment.Department FROM (((tDepartment RIGHT JOIN tProject ON tDepartment.Project... 



The SP code....
CREATE PROCEDURE spProjectDetailReport
(
@ProjectType Int,
@OrderBy varchar(50)
)

AS

DECLARE @SQL varchar(255)

SET @SQL = 'INSERT INTO tProjectDetailReportData (ProjDesc, ProjAnticCompDate, ProjManager, ProjSponsor, ProjType, ProjDept)
SELECT tProject.ProjDesc, tProject.ProjAnticCompDate, tProjectManager.ProjectManagerName, tProjectSponsor.ProjectSponsorName, tProjectType.ProjectType, tDepartment.Department
FROM (((tDepartment RIGHT JOIN tProject
ON tDepartment.ProjectDeptID = tProject.ProjDept)
LEFT JOIN tProjectManager
ON tProject.ProjManagerID = tProjectManager.ProjectManagerID)
LEFT JOIN tProjectSponsor
ON tProject.ProjSponsorID = tProjectSponsor.ProjectSponsorID)
LEFT JOIN tProjectType ON tProject.ProjTypeID = tProjectType.ProjectTypeID
WHERE (((tProjectType.ProjectTypeID) =' + @ProjectType + 'OR' + @ProjectType + '= 0))
ORDER BY' + @OrderBy

EXEC(@SQL)
GO


Apparently I have to do it this way in order to use the 'Order By' clause at the end, but ever since I changed it I've had that error.....any ideas why?

Thanks

Matt

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-11-07 : 07:11:35
Change
DECLARE @SQL varchar(255)

to
DECLARE @SQL varchar(8000)




E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2007-11-07 : 07:12:17
quote:
Originally posted by Gyto

Hi there,

Whenever I run this stored procedure I get the error message:

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

[Microsoft][ODBC SQL Server Driver][SQL Server]Syntax error converting the varchar value 'INSERT INTO tProjectDetailReportData (ProjDesc, ProjAnticCompDate, ProjManager, ProjSponsor, ProjType, ProjDept) SELECT tProject.ProjDesc, tProject.ProjAnticCompDate, tProjectManager.ProjectManagerName, tProjectSponsor.ProjectSponsorName, tProjectType.ProjectType, tDepartment.Department FROM (((tDepartment RIGHT JOIN tProject ON tDepartment.Project... 



The SP code....
CREATE PROCEDURE spProjectDetailReport
(
@ProjectType Int,
@OrderBy varchar(50)
)

AS

DECLARE @SQL varchar(8000)

SET @SQL = 'INSERT INTO tProjectDetailReportData (ProjDesc, ProjAnticCompDate, ProjManager, ProjSponsor, ProjType, ProjDept)
SELECT tProject.ProjDesc, tProject.ProjAnticCompDate, tProjectManager.ProjectManagerName, tProjectSponsor.ProjectSponsorName, tProjectType.ProjectType, tDepartment.Department
FROM (((tDepartment RIGHT JOIN tProject
ON tDepartment.ProjectDeptID = tProject.ProjDept)
LEFT JOIN tProjectManager
ON tProject.ProjManagerID = tProjectManager.ProjectManagerID)
LEFT JOIN tProjectSponsor
ON tProject.ProjSponsorID = tProjectSponsor.ProjectSponsorID)
LEFT JOIN tProjectType ON tProject.ProjTypeID = tProjectType.ProjectTypeID
WHERE (((tProjectType.ProjectTypeID) =' + @ProjectType + 'OR' + @ProjectType + '= 0))
ORDER BY' + @OrderBy

EXEC(@SQL)
GO


Apparently I have to do it this way in order to use the 'Order By' clause at the end, but ever since I changed it I've had that error.....any ideas why?

Thanks

Matt



Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-11-07 : 07:12:22
And change

WHERE (((tProjectType.ProjectTypeID) =' + @ProjectType + 'OR' + @ProjectType + '= 0))
ORDER BY' + @OrderBy

to

WHERE (((tProjectType.ProjectTypeID) =' + @ProjectType + ' OR ' + @ProjectType + '= 0))
ORDER BY ' + @OrderBy



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2007-11-07 : 07:12:46


Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2007-11-07 : 07:59:49
and change

WHERE (((tProjectType.ProjectTypeID) =' + @ProjectType + ' OR ' + @ProjectType + '= 0))

to

WHERE (((tProjectType.ProjectTypeID) =' + CONVERT(varchar(20), @ProjectType) + ' OR ' + CONVERT(varchar(20), @ProjectType) + '= 0))

Kristen
Go to Top of Page

Gyto
Posting Yak Master

144 Posts

Posted - 2007-11-07 : 12:11:31
Thanks guys, I've done all that and it appears to work :) Cheers!
Go to Top of Page
   

- Advertisement -