Solved

Double query to make a view?

Posted on 2012-03-30
3
205 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 250 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 250 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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

706 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now