Converting Dynamic Pivot to Parameterized Query

  • 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[/url] 😉

  • 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![/I]

    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?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

  • 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[/url] 😉

  • 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![/I]

    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?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply