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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Stored Procedure Syntax -- Group By 1

Status
Not open for further replies.

SidTheSquid

Programmer
Sep 25, 2002
34
0
0
CA
Hi, I have the following stored procedure:

CREATE PROCEDURE spSelectGroupCount
@ColName varchar(20)
AS
Select @ColName, count(1) From traffic_log group by @ColName

All I want was to pass in a column name and it counts number of times a hit was found for a particular entry. I am getting the syntax error
"Error 164: Group BY expression must refer to a column name that appear in the select list"

I dont understand why i get this error. Anybody care to explain and give me some sql guidance :)

Sid
 
You have to use
Code:
EXEC
or
Code:
EXECUTE
to run a dynamically created
Code:
SELECT
statement.

Try:

Code:
CREATE PROCEDURE spSelectGroupCount
  @ColName     varchar(20)
AS
EXECUTE ('Select  ' + @ColName + ', count(1) From traffic_log group by ' +  @ColName )


-dave
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top