Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

dynamic sql in a stored procedure using group by

Status
Not open for further replies.

drallabs

Technical User
Mar 2, 2006
28
US
can anyone tell me why the following stored procedure errors out?

Create Procedure StandEdit_ErrorCheck
@TableName VarChar(100)
AS

Declare @SQL VarChar(1000)

--Check for Duplicate Standids
SELECT @SQL = 'SELECT standid FROM '
SELECT @SQL = @SQL + @TableName + 'GROUP BY standid '
--GROUP BY standid
--HAVING count(*) > 1

Exec ( @SQL)

GO

StandEdit_ErrorCheck klstandedits

Server: Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'BY'.
 
Add a space between the apostrophe and Group.

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Still getting errors?

To be a little 'safer' with regards to tables that may contain spaces in the name, you should add the square brackets around the table name, like this...

Code:
Create Procedure StandEdit_ErrorCheck
    @TableName VarChar(100)
AS

Declare @SQL VarChar(1000)

--Check for Duplicate Standids
SELECT @SQL = 'SELECT standid FROM '
SELECT @SQL = @SQL [!]+ '[' [/!]+ @TableName + '[!]] [/!]GROUP BY standid '
--GROUP BY standid
--HAVING count(*) > 1

Exec ( @SQL)

GO

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top