SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Converting Dynamic Pivot to Parameterized Query


Converting Dynamic Pivot to Parameterized Query

Author
Message
vinu512
vinu512
Hall of Fame
Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)

Group: General Forum Members
Points: 3677 Visits: 1626
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 ;-)
dwain.c
dwain.c
SSCoach
SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)

Group: General Forum Members
Points: 17859 Visits: 6431
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!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
vinu512
vinu512
Hall of Fame
Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)

Group: General Forum Members
Points: 3677 Visits: 1626
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 ;-)
dwain.c
dwain.c
SSCoach
SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)

Group: General Forum Members
Points: 17859 Visits: 6431
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!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search