0

I have the following query

CREATE TABLE yourtable
    ([Country_Code] varchar(3),[Location] varchar (10),[ProcessedMonth] nvarchar(50),[ProcessedYear] nvarchar(50),[BillingMonth] nvarchar(50),[BillingYear] nvarchar(50), [aaa] nvarchar(40),[aaa Narration] nvarchar(40),[aaa Remarks] nvarchar(40), [bbb] nvarchar(40), [bbb Narration] nvarchar(40),[bbb Remarks] nvarchar(40), [ccc] nvarchar(40),[ccc Narration] nvarchar(40),[ccc Remarks] nvarchar(40), [ddd] nvarchar(40),[ddd Narration] nvarchar(40),[ddd Remarks] nvarchar(40))
;

INSERT INTO yourtable
    ([Country_Code],[Location],[ProcessedMonth],[ProcessedYear],[BillingMonth],[BillingYear] , [aaa],[aaa Narration],[aaa Remarks] , [bbb],[bbb Narration],[bbb Remarks] , [ccc],[ccc Narration],[ccc Remarks], [ddd],[ddd Narration],[ddd Remarks] )
VALUES
    ('IND','-1', 'sep', '2013', 'sep', '2012','1','asdf1','qwer1','2','asdf2','','3','','qwer3','Y','asdf4','wqeq4')    
;
Declare @Col nvarchar(max)
        Declare @ServiceName nvarchar(50)
Select @Col = ''


    Declare #tmpColList cursor for
                Select ServiceName from tbl_servicemapping order by serviceid
            Open #tmpColList 
                Fetch from #tmpColList into @ServiceName
            while @@FETCH_STATUS = 0
            Begin
                if @Col = ''
                    Select @Col = '''' + @ServiceName + ''''
                else
                    Select @Col = @Col + ',' + '''' + @ServiceName + ''''
                Fetch from #tmpColList into @ServiceName
            end
            close #tmpColList
            deallocate #tmpColList
            print @Col  --o/p of this statement is 'aaa','bbb','ccc','ddd','eee',


DECLARE @colsUnpivot AS NVARCHAR(MAX),
@colsUnpivot1 AS NVARCHAR(MAX),
@colsUnpivot2 AS NVARCHAR(MAX),

   @query  AS NVARCHAR(MAX)

select @colsUnpivot = stuff((select ','+quotename(C.name)
         from sys.columns as C
         where C.object_id = object_id('yourtable') and
               C.name != 'Country_Code'
           and C.name!='Location'  
                             and C.name!='ProcessedMonth'  
                             and C.name!='ProcessedYear'
                              and C.name!='BillingMonth'  
                             and C.name!='BillingYear'
                             and C.Name not like '%Narration'
                             and C.Name not like '%Remarks'
         for xml path('')), 1, 1, '')

        select @colsUnpivot1 = stuff((select ','+quotename(C.name)
         from sys.columns as C
         where C.object_id = object_id('yourtable') and
               C.name != 'Country_Code'
           and C.name!='Location'  
                             and C.name!='ProcessedMonth'  
                             and C.name!='ProcessedYear'
                              and C.name!='BillingMonth'  
                             and C.name!='BillingYear'  
                             and C.Name not in (@Col)
                             and C.Name not like '%Remarks'

         for xml path('')), 1,1, '') 

         select @colsUnpivot2 = stuff((select ','+quotename(C.name)
         from sys.columns as C
         where C.object_id = object_id('yourtable') and
               C.name != 'Country_Code'
           and C.name!='Location'  
                             and C.name!='ProcessedMonth'  
                             and C.name!='ProcessedYear'
                              and C.name!='BillingMonth'  
                             and C.name!='BillingYear'  
                             and C.Name not in (select @Col)
                             and C.Name not like '%Narration'
         for xml path('')), 1,1, '') 


set @query 
  = 'select country_code,Location,processedmonth,processedyear,billingmonth,billingyear, servicename,Value as NoOfRecords,Val as Narration,Vale as Remarks
     from yourtable
      UNPIVOT
     (
        Value
        for servicename in ('+ @colsunpivot +')) as r
         UNPIVOT        
        ( Val
        for servicenamenarration in ('+ @colsunpivot1 +')) as t
        UNPIVOT        
        ( Vale
        for servicenameremarks in ('+ @colsunpivot2 +')) as f

       WHERE RIGHT(Val, 1) = RIGHT(Value, 1) and RIGHT(Val, 1) = RIGHT(Vale, 1); '


exec(@query)
--drop table yourtable

It says invalid column name 'aaa','bbb' etc.., but when i hard cord the @col values i am not getting any error and also the desired o/p should be:

country_code    Location    processedmonth    processedyear    billingmonth    billingyear    servicename   NoOfRecords Narration   Remarks
IND -1  sep 2013    sep 2012    aaa 1   asdf1   qwer1
IND -1  sep 2013    sep 2012    bbb 2   asdf2   
IND -1  sep 2013    sep 2012    ccc 3       qwer3
IND -1  sep 2013    sep 2012    ddd Y   asdf4   qwer4

Kindly help how to achieve this. Thanks..

4

0 回答 0