|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Monday, May 06, 2013 1:26 PM
Points: 2,359,
Visits: 3,292
|
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Sunday, December 09, 2012 10:15 PM
Points: 4,
Visits: 139
|
|
| Very nice! I will probably use this at some time. Thanks heaps!
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Thursday, July 22, 2010 8:59 AM
Points: 110,
Visits: 952
|
|
Have you considered building the update string inside the loop, but executing it after you have iterated all the columns? O(1) < O(n)
Also, do you have any comments/caveats on the security implication of dynamic SQL used in this process?
|
|
|
|
|
SSCommitted
      
Group: General Forum Members
Last Login: Tuesday, May 29, 2012 11:22 AM
Points: 1,755,
Visits: 4,652
|
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Monday, May 06, 2013 1:26 PM
Points: 2,359,
Visits: 3,292
|
|
Yes, in my very first early version. Then I realized that I could potentially come across the 8000 character limit for varchars.
N 56°04'39.16" E 12°55'05.25"
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Monday, May 06, 2013 1:26 PM
Points: 2,359,
Visits: 3,292
|
|
Thanks Ryan. I have tried to keep the dynamic SQL executions to a minimum for obvious speed reasons. What I think I have provided is a base for creating crosstabs/pivots for users to learn from and hopefully, evolve with. There are some things to do with the code to make it "universal". But I thought it would be best to show how to start from the beginning. To make the code "universal" you first have to build a dynamic query for preaggregating the data in #Aggregates. That is not very hard to to! Also, you must change the parameters to the future stored procedure to allow two fully qualified names such as RemoteServer1.OwnerLocal.ThatTable.ThisField for the rows and columns. Even the CellData field in #Aggregates could be taken from a parameter this way and called with 'SUM(x)' or 'COUNT(y)'. You catch my drift. An example could be EXEC dbo.CrossTabPivot 'Table1.OfficeName', 'Table2.Category', 'COUNT(t)', ...
N 56°04'39.16" E 12°55'05.25"
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Monday, May 06, 2013 1:26 PM
Points: 2,359,
Visits: 3,292
|
|
Yes, SQL injection could possible be an issue here, if the data in ColumnText is written such way. But since the beginning of the UPDATE-statement is hardwired with "UPDATE", I right now can't see a way to manipulate the statement to run SQL injection code.
N 56°04'39.16" E 12°55'05.25"
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Wednesday, February 18, 2009 7:48 AM
Points: 178,
Visits: 4
|
|
I think it is worth mentioning the restrictions of that method such as: - MAX number of columns (?), - MAX total row length (8K). I've used similar method using cursor instead of column table.
|
|
|
|
|
SSC Eights!
      
Group: General Forum Members
Last Login: 2 days ago @ 12:05 PM
Points: 915,
Visits: 168
|
|
Perfect timing! I was able to use this logic for a report today. It made short work out of an otherwise arduous task.
Thanks.
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Monday, May 06, 2013 1:26 PM
Points: 2,359,
Visits: 3,292
|
|
Max number of columns are restricted to what datatype you use for #Aggregate column. The total size for a row is 8,060 bytes. Using 60 bytes for RowText leaves us 8,000 bytes for the rest of the columns and since INT is 4 bytes, we can potentially have 2,000 columns. If you prefer SMALLINT (2 bytes) as #Aggregate column, you could theoretically have 4,000 columns.
N 56°04'39.16" E 12°55'05.25"
|
|
|
|