Skip to content
Advertisement

SQL procedure group by parameter value

I want to use group by functionality with dynamic value.

Ex:

When I code like this I get the error == >

Each GROUP BY expression must contain at least one column that is not an outer reference.

Any workaround for this?

Advertisement

Answer

You need to be very careful with this type of code as it is prone to SQL injection.

Always use QUOTENAME on identifiers. And if the column name is coming from user code then verify it.

Because @DUPLICATE_COUNTS is not in scope in the dynamic part, you need to re-declare it as an OUTPUT parameter.

To verify the column name, use this code:

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement