Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Converting Dynamic Pivot to Parameterized Query Expand / Collapse
Author
Message
Posted Thursday, August 16, 2012 6:03 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: 2 days ago @ 12:58 AM
Points: 1,127, Visits: 1,598
Hi

I have the following sample script which does a Dynamic Pivot on a table as follows:

--Creating Table

Create Table Ex
(InvoiceNumber int,
BillName Varchar(5) )


--Inserting Sample Data

Insert into Ex
Select 1, 'Amit'
Union ALL
Select 2, 'Amit'
Union ALL
Select 3, 'BBB'
Union ALL
Select 4, 'Amit'


--Dynamic Pivot

Declare @cols Varchar(max), @sql Varchar(max)
Select @cols = STUFF((Select ', [' + CAST(InvoiceNumber As Varchar(10) ) + ']' From Ex For XML Path('')), 1, 2, '')
Set @sql = 'Select '+@cols+' From Ex
Pivot
(MAX(BillName) For InvoiceNumber IN ('+@cols+') ) As pvt'
Execute (@sql)

I have done Parameterized queries where parameters are passed into the where clause. But, what I wanted to ask is if we can change this whole dynamic pivot into a Parameterized Query where parameters can be passed into the "Select" part of the query and then Executed using "sp_Executesql".

I have read quite a bit about Parameterized Queries on the internet but have only seen blog posts or explanations where Parameters are passed into the Where Clause of the Parameterized Query. Is it possible to Parameterize the "Select" or any other parts of the query??...

Any insight you might have on Parameterized queries would be really helpful. It would be even better if you could reccommend some good reads on this topic.

Looking forward to replies.


Vinu Vijayan

For better and faster solutions please check..."How to post data/code on a forum to get the best help" - Jeff Moden
Post #1345874
Posted Friday, August 17, 2012 1:37 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Tuesday, October 7, 2014 10:53 PM
Points: 3,421, Visits: 5,359
Look at BOL: http://msdn.microsoft.com/en-us/library/ms188001(v=sql.105).aspx

Does the first example on that page do what you want?

If not, perhaps I'm misunderstanding.



My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
Post #1346371
Posted Friday, August 17, 2012 3:50 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: 2 days ago @ 12:58 AM
Points: 1,127, Visits: 1,598
dwain.c (8/17/2012)
Look at BOL: http://msdn.microsoft.com/en-us/library/ms188001(v=sql.105).aspx

Does the first example on that page do what you want?

If not, perhaps I'm misunderstanding.


Yes, that's the article I read to start with this. I was implementing the Example B from the link.
But I think I've tried to implement in 'n' no. of ways but nothing has worked yet...I've been trying since morning and haven't been able to do it.
Looks like its just one of those days. I must be missing something.
Dwain could you please give it a shot if ur free??....in the name of sql


Vinu Vijayan

For better and faster solutions please check..."How to post data/code on a forum to get the best help" - Jeff Moden
Post #1346434
Posted Friday, August 17, 2012 4:01 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Tuesday, October 7, 2014 10:53 PM
Points: 3,421, Visits: 5,359
vinu512 (8/17/2012)
dwain.c (8/17/2012)
Look at BOL: http://msdn.microsoft.com/en-us/library/ms188001(v=sql.105).aspx

Does the first example on that page do what you want?

If not, perhaps I'm misunderstanding.


Yes, that's the article I read to start with this. I was implementing the Example B from the link.
But I think I've tried to implement in 'n' no. of ways but nothing has worked yet...I've been trying since morning and haven't been able to do it.
Looks like its just one of those days. I must be missing something.
Dwain could you please give it a shot if ur free??....in the name of sql


I'm always willing.

I'm just not sure I understand what you're trying to do well enough to offer further suggestions.

Perhaps if you showed some example expected output and gave more details on what parameters you want to pass in and why you need to, it might help.

I've done something like the example I referenced before and I admit it was kind of confusing at first.



My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
Post #1346444
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse