Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Pivot 3 column stored procedure query record set in SQL Server

Pivot 3 column stored procedure query record set in SQL Server

Scheduled Pinned Locked Moved Database
databasesql-serversysadminquestion
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    T2102
    wrote on last edited by
    #1

    Let's say I have a store procedure returning Value, Desired_Row_Header, and Desired_Column_Header. EXEC [dbo].[scsp_My_Query] Can I pivot the results of the stored procedure directly or do I have to embed the pivot in the stored procedure? I am currently fetching the results off the database and manually creating pivot tables in code. I'd like to move it on the database, especially if I can do the pivot based on the column position without having to lookup the names of each column.

    S 1 Reply Last reply
    0
    • T T2102

      Let's say I have a store procedure returning Value, Desired_Row_Header, and Desired_Column_Header. EXEC [dbo].[scsp_My_Query] Can I pivot the results of the stored procedure directly or do I have to embed the pivot in the stored procedure? I am currently fetching the results off the database and manually creating pivot tables in code. I'd like to move it on the database, especially if I can do the pivot based on the column position without having to lookup the names of each column.

      S Offline
      S Offline
      S Douglas
      wrote on last edited by
      #2

      Have you tried CREATE TABLE #tmp(Value varchar(20), Desired_Row_Header varchar(20), Desired_Column_Header varchar(20)) INSERT INTO #tmp exec scsp_My_Query Now do your pivot on #tmp


      Common sense is admitting there is cause and effect and that you can exert some control over what you understand.

      T 1 Reply Last reply
      0
      • S S Douglas

        Have you tried CREATE TABLE #tmp(Value varchar(20), Desired_Row_Header varchar(20), Desired_Column_Header varchar(20)) INSERT INTO #tmp exec scsp_My_Query Now do your pivot on #tmp


        Common sense is admitting there is cause and effect and that you can exert some control over what you understand.

        T Offline
        T Offline
        T2102
        wrote on last edited by
        #3

        No, I hadn't tried doing that. It sounds like a good idea and I will try it out. I have gotten some pivots to work with arbitrary # of columns, but I've had to use dynamic sql.

        S 1 Reply Last reply
        0
        • T T2102

          No, I hadn't tried doing that. It sounds like a good idea and I will try it out. I have gotten some pivots to work with arbitrary # of columns, but I've had to use dynamic sql.

          S Offline
          S Offline
          S Douglas
          wrote on last edited by
          #4

          T2102 wrote:

          I have gotten some pivots to work with arbitrary # of columns, but I've had to use dynamic sql.

          Yea if the columns before execution time are not known then you will need to resort to dynamic SQL.


          Common sense is admitting there is cause and effect and that you can exert some control over what you understand.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups