2

I'm trying to generate a security matrix as a report for my application.

Looking at this question, it is almost exactly what I want.

SQL Server: Examples of PIVOTing String data

I've changed the solution

SELECT Action,
   MAX( CASE data WHEN 'View' THEN data ELSE '' END ) ViewCol, 
   MAX( CASE data WHEN 'Edit' THEN data ELSE '' END ) EditCol
FROM t
GROUP BY Action

to adapt to my solution.

SELECT RefRoleId ,
       MAX( CASE Permission WHEN 'AdministerErrors' THEN 'X' ELSE '' END ) 'Administer Errors'
 FROM RolePermission
 GROUP BY RefRoleId

The problem I can see though is that I would need to know exactly which permissions I have and hard code them as columns into the query. I would need a new

MAX( CASE Permission WHEN 'AdministerErrors' THEN 'X' ELSE '' END ) 'Administer Errors'

for every single permission.

Is there a way for me to add the columns I want displayed to an SQL query via a query?

1

1 Answer 1

1

Yes, you can build your pivot dynamically, automatically adding as many columns as you need, based on the data in the table. Step-by-step instructions: http://www.simple-talk.com/community/blogs/andras/archive/2007/09/14/37265.aspx

The article doesn't mention SQL Server 2008, but it works with that version as well.

Sign up to request clarification or add additional context in comments.

Comments

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.