Question : Pivot Rows to Columns without Summing Sql 2000

Hey All,

Im pretty sure what i need to do is a pivot table concept but for the life of me i cant figure it out. Here's the problem.

I have 2 queries. The first gets summary data (well say firstname, lastname for this example). Then, as it is written right now, the code loops through a list of which contains time stamps a status changed and selects each distinct status as its own column with an empty string value. Im trying to convert this dynamic SQL into a stored proc.

So here is an example of how it is right now.

query += "SELECT FirstName, LastName, ";
statuses = GetStatuses('statusId');
foreach(string status in statuses)
{
     query += " ' ' as " + status['statusName'] + ", ";
}
query += "FROM Users";

GetStatuses Looks like this
SELECT DISTINCT status_name as statusName
                            FROM statuses_workflow
                            WHERE work_status = @StatusId                                     
                            GROUP BY status_name
                             HAVING count(status_id) > @limit

I need to make this into one query i can put in a stored procedure and eliminate the C# code that wraps the status part.

Im pretty sure i need to pivot the results of the inner query so each row (status_name) becomes a column. I''m not sure how. Any ideas?

Answer : Pivot Rows to Columns without Summing Sql 2000

HI

This code should remain dynamic.
You can not generate column names of a query dynamically in a stored procedure.
the reason is that all SP content is compiled , that means that all tables , columns , types are validated against the database , so , when the SP is executed, the SP is not parsed for its syntax, rather, it is immediately executed.
But, if your query column names are dynamic, the SP can not be compiled , because it "has no idea" what will be the columns and can not validate it.
Random Solutions  
 
programming4us programming4us