In SQL Server 2005, you may encounter pivot scenarios where you need to handle an unknown number of columns. Handling this situation can be tricky using static perspective syntax. However, there are two possible solutions:
1. Dynamic SQL method (not recommended)
Although you stated that you want to avoid dynamic SQL, it is still a viable option in this case. The answers provided in the topics "Pivot Tables and Column Joins" and "PIVOT in SQL 2005" contain some examples of vulnerable dynamic SQL that you can avoid, as well as an alternative way to generate SQL in a stored procedure.
2. No need for dynamic SQL (not feasible)
Unfortunately, in SQL Server 2005, there is no way to handle pivots with an unknown number of columns without using dynamic SQL. The pivot syntax requires specifying column names, which requires hardcoding unknown values.
The above is the detailed content of How Can I Pivot Data with an Unknown Number of Columns in SQL Server 2005?. For more information, please follow other related articles on the PHP Chinese website!