0

I have a stored procedure (join two tables and select where condition @GID), a want to convert table result from rows to columns. I use a dynamic pivot query.

My stored procedure:

enter image description here

After I try using pivot

enter image description here

I want result like this:

GROUP_MOD_ID   ADD  EDIT  DELETE  ETC...
---------------------------------------    
G02            1     1    0      ....

Can you give me some advice about this ?

Thank you.

1 Answer 1

1

It's because you're using the batch delimiter to separate your queries. This means the scope of @GID is incorrect. Remove the semi colon after:

DECLARE @pivot_cols NVARCHAR(MAX);

You don't need to use batch delimiters in this case. The logical flow of the procedure means you can omit them without any problems.

EDIT:

Here's the edited code that I've devised:

ALTER PROCEDURE GET_COLUMN_VALUE @GID CHAR(3)
AS
BEGIN

    DECLARE @PivotCols NVARCHAR(MAX)

    SELECT @PivotCols = STUFF((SELECT DISTINCT ' , ' + QUOTENAME(B.FUNCTION_MOD_NAME)
    FROM FUNCTION_GROUP AS A
    JOIN FUNCTION_MOD B
    ON A.FUNCTION_MOD_ID = B.FUNCTION_MOD_ID
    WHERE A.GROUP_MOD_ID = @GID
    FOR XML PATH (' '), TYPE).value(' . ', 'NVARCHAR(MAX) '), 1, 1, ' ')

    DECLARE @PivotQuery NVARCHAR(MAX)

    SET @PivotQuery = '
    ;WITH CTE AS (
    SELECT A.GROUP_MOD_ID, B.FUNCTION_MOD_NAME, CAST(ALLOW AS BIT) AS ALLOW
    FROM FUNCTION_GROUP AS A
    JOIN FUNCTION_MOD AS B
    ON A.FUNCTION_MOD_ID = B.FUNCTION_MOD_ID)
    SELECT GROUP_MOD_ID, '+@PivotCols+'
    FROM CTE
    PIVOT (MAX(ALLOW) FOR FUNCTION_MOD_NAME IN ('+@PivotCols')) AS PIV'
    PRINT @PivotQuery
    EXEC (@PivotQuery)

END

EDIT2:

You should execute this stored procedure like so:

EXEC GET_COLUMN_VALUE @GID='G02'
Sign up to request clarification or add additional context in comments.

7 Comments

Dear JohnnyBell.It is not work .I create procedure is ok, but i run GET_COLUMN_VALUE G02 It error Msg 102, Level 15, State 1, Line 4 Incorrect syntax near 'A'. Please see it
Looking at your PIVOT query, you've replicated the JOIN line twice, which will create a syntax error. Put this line before your Exec (@pivot_query) so you can see what it's trying to execute: PRINT @pivot_query Also, you should be calling your procedure like so: EXEC GET_COLUMN_VALUE @GID='G02'
So great , same debugging SQL and see error. Thank you . You can try it and share for me . I try but it still not working . GET_COLUMN_VALUE 'G02'
I can't replicate your error, because I don't have any DDL. If you posted sample data and the actual query text rather than images I might be able to help you. When you print the @pivot_query variable, you can see where the syntax is wrong.
This way, i want to run store procedure as GET_COLUMN_VALUE G02 and after put PRINT @pivot_query , i get error follow SELECT A.GROUP_MOD_ID, [Add],[Delete],[Edit] FROM (SELECT A.GROUP_MOD_ID,B.FUNCTION_MOD_NAME,CAST(ALLOW AS BIT) ALLOW FROM FUNCTION_GROUP A INNER JOIN FUNCTION_MOD B ON A.FUNCTION_MOD_ID=B.FUNCTION_MOD_ID) FUNCTION_GROUP A INNER JOIN FUNCTION_MOD B ON A.FUNCTION_MOD_ID=B.FUNCTION_MOD_ID PIVOT (MAX(ALLOW) FOR B.FUNCTION_MOD_NAME IN([Add],[Delete],[Edit])) AS P; Msg 102, Level 15, State 1, Line 4 Incorrect syntax near 'A'.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.