Thursday, 15 May 2014

stored procedures - Dynamic Pivot (in SQL Server 2005) -


For Microsoft SQL 2005, I am writing an archived procedure and I want to create a dynamic SQL Pivot:

pre> Book SELECT Book.ISBN, book.Name BookMurn.StockLocation book at INNER SM Pivot (COUNT (sm.NumberOfBooks)) for Bookid = sm.bookid PIVOT (sm.StockLocation IN (...)

Favorite with me (...):

  Select the location of the stock from the stock location  

and all the locations in the process ([location1], [location 2] etc. There is no hardcod in it, but SQL does not accept it.

How do I solve it?

You can not do it in pure SQL, you can use dynamic SQL You have to use and create a real SQL that you want to execute.

You can do this:

  DECLARE @sql VARCHAR (8000) SET @sql = 'location space for SIP' ('cursor cursor ... loop cursor ... SET @sql = @ Sql +' '+ + column_name +' '', '// and loop EXEC (@sql) )  

No comments:

Post a Comment