Click here to Skip to main content
15,890,123 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Need to select fields from @sql buildup to use the UNPIVOT command


SQL
SET @sql = ' SELECT A.BRN_NAME ,A.PROD_SERV , A.COMP_TYPE,
				  P.PROD_SERV_TAT, 
				  COUNT(*) AS OVER_CNT FROM TRIAL
  LEFT JOIN BRANCH C ON A.BRN_NAME = C.BRN_NAME
  LEFT JOIN PROD_SERV P ON A.PROD_SERV = P.PROD_SERV_NAME
  LEFT JOIN PAYMAST B ON A.LNAME = B.LNAME
  AND A.FNAME = B.FNAME '






SQL
SET @sql = 'SELECT BRN_NAME, PROD_SERV, COMP_TYPE, 
              PROD_SERV_TAT,OVER_CNT,OPINIONS,OPINION_CNT 
             from ('+  @sql+') AA

   UNPIVOT
     (
      OPINIONS_CNT FOR [OPINIONS] IN ([ACCEPT_TAT], [BEYOND_TAT] , [EXACT_TAT]) 
     ) AB  '



---------------------------------------------------------------------------------

This is what I tried

SQL
SET @sql = 'SELECT BRN_NAME, PROD_SERV, COMP_TYPE, 
              PROD_SERV_TAT,OVER_CNT,OPINIONS,OPINION_CNT 
             from ('+  @sql+') AA


Any help to overcome this challenge

Thanks

What I have tried:

Checked the net and other works all to no avail
Posted
Updated 5-May-18 21:09pm
v3

1 solution

Because of the flexible nature of SQL language, it would be hard to parse the column names just from a string. You would need to take into account things like
- Table aliases
- Column aliases
- Function usage
- Scalar queries
- and so on...

Few things that might work for you:

Store the list of columns or column names in a separate string and use that in both places.

identify the column name with some special character pattern. Use that pattern to find columns, for example
SQL
SELECT A.##BRN_NAME##, 
       A.##PROD_SERV##, 
       A.##COMP_TYPE##,
       P.##PROD_SERV_TAT##,
       COUNT(*) AS ##OVER_CNT##
FROM TRIAL

Now you can find the names between ##. Just remember to remove the hashes before using the statement.

ADDITION:
---------
When extracting the column names and using it elsewhere, depending on the SQL statements to be expected there are a lot of things you need to take into consideration. As said SQL is flexible in writing.

But to get you started with, consider the following example
SQL
CREATE TABLE #test2 (
  col1 int,
  col2 int
);

INSERT INTO #test2 VALUES 
(1,2),
(3,4);

Now if we create a function which would extract the column list from a simple case, it could look something like
SQL
CREATE FUNCTION ColList(@originalSql nvarchar(max)) RETURNS varchar(max) AS 
BEGIN
   DECLARE @list nvarchar(max),
           @index1 int,
		   @index2 int;

   SET @index1 = 0;
   SET @list = '';
   SET @index1 =CHARINDEX('##', @originalSql, 0) + 2;
   WHILE @index1 > 0 BEGIN
      SET @index2 =CHARINDEX('##', @originalSql, @index1 + 2);
      IF (@list != '') BEGIN
	     SET @list = @list + ', ';
	  END;
      SET @list = @list + SUBSTRING(@originalSql, @index1, @index2 - @index1);
      SET @index1 = CHARINDEX('##', @originalSql, @index2 + 2) ;
	  IF (@index1 > 0 ) BEGIN
	     SET @index1 = @index1 + 2
	  END;
   END;

   RETURN @list
END;

Let's have a test
SQL
DECLARE
  @sql nvarchar(max),
  @col nvarchar(max)
BEGIN
   SET @sql = 'SELECT t.##Col1##, t.##Col2## FROM #Test2 t';

   SET @col = dbo.ColList(@sql);
   SET @sql = 'SELECT ' + @col + ' FROM (' + REPLACE(@sql, '##','') + ') a';
   PRINT  @sql;
   EXEC (@sql);
END;

The query that is executed is as follows
SQL
SELECT Col1, Col2 FROM (SELECT t.Col1, t.Col2 FROM #Test2 t) a

But as said, that's just the starters. For example you may need to be able to define an alias for the columns used in the column list, and so on...
 
Share this answer
 
v2
Comments
Member 12770648 6-May-18 2:51am    
Thanks for the explanation

using the the example you've provided how will
I select this.

SET @sql = 'SELECT BRN_NAME, PROD_SERV, COMP_TYPE,
PROD_SERV_TAT,OVER_CNT,OPINIONS,OPINION_CNT
from ('+ @sql+') AA


Thanks again
Wendelius 6-May-18 3:39am    
See the updated answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900