[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Double query to make a view?

Posted on 2012-03-30
3
Medium Priority
?
215 Views
Last Modified: 2012-03-30
I made a query that pivots a table using names from another table.  I asked the IT folks to make it into a view because there's a need to be able to query the pivoted result.  The query, which works fine, is below.  The response was that it couldn't be made into a view because it's actually 2 queries.  Is this true?  Is there a way it could be redone so that it could be used to make a view?

DECLARE @NPDColumns   VARCHAR (8000)

SELECT @NPDColumns =
          COALESCE (@NPDColumns + ',[' + cast (Description AS VARCHAR) + ']',
                    '[' + cast (Description AS VARCHAR) + ']')
  FROM [RKBBackEnd].[dbo].[lkpNPDBatch]
GROUP BY Description

DECLARE @query   VARCHAR (8000)
SET @query = '

DECLARE @NPD TABLE
             (  BatchCode     NVARCHAR (20),
                Description   NVARCHAR (20),
                Actual        FLOAT
             )

INSERT INTO @NPD (BatchCode, Description, Actual)
   SELECT a.BatchCode, b.Description, a.Actual
     FROM    [RKBBackEnd].[dbo].[tblNPDBatch] AS a
          INNER JOIN
             [RKBBackEnd].[dbo].[lkpNPDBatch] AS b
          ON a.BatchAttributeID = b.BatchAttributeID
    --WHERE BatchCode LIKE '+ CHAR(39) + '201203%' + CHAR(39)+ '

SELECT c.BatchAcceptDateTime, d.*
  FROM    (SELECT BatchCode, BatchAcceptDateTime
             FROM [RKBBackEnd].[dbo].[tblBatchInfo]) AS c
       JOIN
          (SELECT *
             FROM @NPD PIVOT (SUM (Actual)
                       FOR Description
                       IN ('
                  + @NPDColumns
                  + ')) AS P) AS d
       ON c.BatchCode = d.BatchCode
ORDER BY BatchCode'
EXECUTE (@query)

Open in new window

0
Comment
Question by:ChiMudGuy
3 Comments
 
LVL 27

Accepted Solution

by:
tliotta earned 1000 total points
ID: 37789834
I'm not aware of any reasonable way to arrive at a VIEW through that. It doesn't seem as if a VIEW is appropriate.

I'd suggest going back to IT and asking for objects that give you the results that you need instead of asking for a specific kind of object. You might get a procedure back or a similar object. But it likely won't be a VIEW.

Tom
0
 
LVL 22

Assisted Solution

by:Thomasian
Thomasian earned 1000 total points
ID: 37789954
>> Is there a way it could be redone so that it could be used to make a view?

No, it is not possible to have dynamic column names in a view.
0
 

Author Closing Comment

by:ChiMudGuy
ID: 37790006
Ok, thanks.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Microsoft Access has a limit of 255 columns in a single table; SQL Server allows tables with over 255 columns, but reading that data is not necessarily simple.  The final solution for this task involved creating a custom text parser and then reading…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Suggested Courses

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question