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 2005 Forums
 Other SQL Server Topics (2005)
 Syntax error

Author  Topic 

Neeno
Starting Member

1 Post

Posted - 2010-12-31 : 04:23:24
Hi, I am new to SQL queries. Can anyone please help me id the error in the code below, all I get in sql is error near keyword 'where'


SET @SQL = @SQL + ' LEFT JOIN (SELECT DivisionContact.DivisionID, Max(DivisionContact.ContactName) AS ContactName ' + char(13)
SET @SQL = @SQL + 'FROM ' + @sDatabaseNameBeginWith + '_DB.dbo.DivisionContact AS DivisionContact ' + char(13)
SET @SQL = @SQL + 'GROUP BY DivisionContact.DivisionID) AS TDivisionContact ON Divisions.DivisionID = TDivisionContact.DivisionID, ' + char(13)

SET @SQL = @SQL + 'WHERE OEMP.EmployeeID > 10 ' + char(13)

dataguru1971
Master Smack Fu Yak Hacker

1464 Posts

Posted - 2010-12-31 : 07:39:35
remove the comma highlighted below after DivisionID in red below.

Declare @SQL varchar(1000)
Declare @sDatabaseNameBeginWith varchar (6)
SET @SQL = ''
Set @sDatabaseNameBeginWith = 'FooBar'
SET @SQL = @SQL + ' LEFT JOIN (SELECT DivisionContact.DivisionID, Max(DivisionContact.ContactName) AS ContactName ' + char(13)
SET @SQL = @SQL + 'FROM ' + @sDatabaseNameBeginWith + '_DB.dbo.DivisionContact AS DivisionContact ' + char(13)
SET @SQL = @SQL + 'GROUP BY DivisionContact.DivisionID) AS TDivisionContact ON Divisions.DivisionID = TDivisionContact.DivisionID, ' + char(13)

SEt @SQL = @SQL + 'WHERE OEMP.EmployeeID > 10 ' + char(13)

Print @SQL


Always a good idea to use PRINT and then view the results or paste into Query Analyzer to check resulting syntax. In the above, i set @SQL = '' only to allow the remaining



Poor planning on your part does not constitute an emergency on my part.

Go to Top of Page
   

- Advertisement -