数据透视期间无效的列名称为“ 100%考试费用”

时间:2018-10-25 09:17:57

标签: sql sql-server

我正在尝试对活动描述中的金额进行调整。

我的数据如下,

  Sr No     Invoice Date    Invoice No    Payer Name    IGMNo    Container No    Size    Type    Act. gate in Date    Container Agent    Container  Agent  Name    Importer Name    CHA Code    CHA Name    Activity Description    Amount    Service Tax    Total                                                                  
 ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 
  1    8-1-2018 12:12:29 AM    MII180800001    SAME DEUTZ FAHR INDIA PRIVATE LIMITED    2200750    ECMU9674562    40    GB    7-26-2018 4:50:35 AM    CLC007    CMA CGM    SAME DEUTZ FAHR INDIA PRIVATE LIMITED    CHS020    SEAKING CARGO SERVICES (I)  PVT LTD    Handling &  PNR Movement Charges-FCL    10400    1872    12272 
  2    8-1-2018 12:12:29 AM    MII180800001    SAME DEUTZ FAHR INDIA PRIVATE LIMITED    2200750    ECMU9674562    40    GB    7-26-2018 4:50:35 AM    CLC007    CMA CGM    SAME DEUTZ FAHR INDIA PRIVATE LIMITED    CHS020    SEAKING CARGO SERVICES (I)  PVT LTD    100% Examination Charges              2000    360    2360      

请注意,这以前适用于我的旧数据。目前,我没有较旧的数据来对它们进行故障排除。

它会引发错误,例如““无效的列名'100%考试费用'。”。可能的失败原因:查询问题,“ ResultSet”属性设置不正确,参数设置不正确“

我的查询如下,

   DECLARE @Columns VARCHAR(MAX) = '',@Sql nvarchar (max)

 SET @Columns=(SELECT STUFF((SELECT DISTINCT ','+ QUOTENAME([Activity Description] )
            FROM  [dbo].[FCLParent] FOR XML PATH ('')),1,1,''))


 SET @sql = 'INSERT INTO [dbo].[FCL_Pivot] ([Payer Name], [Container No], [Size], [Type], [CHAName], 
[Act Gatein Date], [Container  Agent  Name], [Importer Name], ' + @Columns + ')

 SELECT [Payer Name], [Container No], [Size], [Type], [CHAName], 
 [Act Gatein Date], [Container  Agent  Name], [Importer Name], ' + @Columns + '
  FROM (
  SELECT [Payer Name], [Container No], [Size], [Type], [CHAName], [Act Gatein Date],
  [Container  Agent  Name], [Importer Name], [Activity Description], [Amount]
  FROM dbo.FCLParent
  ) src
  PIVOT (MAX([Amount]) FOR [Activity Description] IN (' + @Columns + '))     pvt'
  EXEC (@sql)

打印语句的结果如下,

INSERT INTO [dbo].[FCL_Pivot] ([Payer Name], [Container No], [Size],    [Type], [CHAName], 
[Act Gatein Date], [Container  Agent  Name], [Importer Name], [REEFER  ELECTRIC CHARGES],[CONTAINER GROUND RENT CHARGE],[INCREASE IN MOVEMENT CHARGES],[WEIGHMENT CHARGES],[STORAGE CHARGE],[DESTUFFING CHARGES],[PRIORITY CHARGES],[RE-WEIGHTMENT CHARGES],[SHIFTING CHARGES],[HAZARDOUS CHARGES],[LIFT ON LIFT OFF CHARGES],[VALUE ADDED CHARGES],[SEAL CHARGES],[INSURANCE CHARGES],[CONTAINER TRACKING CHARGES],[EMPTY MOVEMENT CHARGES],[CONTAINER MOVEMENT AND INCREASE CHARGES],[KALMAR HANDLING CHARGES],[HANDLING CHARGES LDD],[MONITORING CHARGES],[FUEL CHARGES],[INSPECTION CHARGES],[SSR CHARGES],[DOCUMENTATION CHARGES],[50% EXAMNIATION CHARGES],[HANDLING CHARGES],[P.O.ESCORT CHARGES],[HANDLING AND  PNR MOVEMENT CHARGES],[OOG DOCUMENTATION CHARGES],[100% EXAMINATION CHARGES],[ESCORT CRANE USAGE CHARGES])

SELECT [Payer Name], [Container No], [Size], [Type], [CHAName], 
[Act Gatein Date], [Container  Agent  Name], [Importer Name], [REEFER   ELECTRIC CHARGES],[CONTAINER GROUND RENT CHARGE],[INCREASE IN MOVEMENT CHARGES],[WEIGHMENT CHARGES],[STORAGE CHARGE],[DESTUFFING CHARGES],[PRIORITY CHARGES],[RE-WEIGHTMENT CHARGES],[SHIFTING CHARGES],[HAZARDOUS CHARGES],[LIFT ON LIFT OFF CHARGES],[VALUE ADDED CHARGES],[SEAL CHARGES],[INSURANCE CHARGES],[CONTAINER TRACKING CHARGES],[EMPTY MOVEMENT CHARGES],[CONTAINER MOVEMENT AND INCREASE CHARGES],[KALMAR HANDLING CHARGES],[HANDLING CHARGES LDD],[MONITORING CHARGES],[FUEL CHARGES],[INSPECTION CHARGES],[SSR CHARGES],[DOCUMENTATION CHARGES],[50% EXAMNIATION CHARGES],[HANDLING CHARGES],[P.O.ESCORT CHARGES],[HANDLING AND  PNR MOVEMENT CHARGES],[OOG DOCUMENTATION CHARGES],[100% EXAMINATION CHARGES],[ESCORT CRANE USAGE CHARGES]
 FROM (
 SELECT [Payer Name], [Container No], [Size], [Type], [CHAName], [Act Gatein Date],
 [Container  Agent  Name], [Importer Name], [Activity Description],  [Amount]
 FROM dbo.FCLParent
 ) src
 PIVOT (MAX([Amount]) FOR [Activity Description] IN ([REEFER ELECTRIC CHARGES],[CONTAINER GROUND RENT CHARGE],[INCREASE IN MOVEMENT CHARGES],[WEIGHMENT CHARGES],[STORAGE CHARGE],[DESTUFFING CHARGES],[PRIORITY CHARGES],[RE-WEIGHTMENT CHARGES],[SHIFTING CHARGES],[HAZARDOUS CHARGES],[LIFT ON LIFT OFF CHARGES],[VALUE ADDED CHARGES],[SEAL CHARGES],[INSURANCE CHARGES],[CONTAINER TRACKING CHARGES],[EMPTY MOVEMENT CHARGES],[CONTAINER MOVEMENT AND INCREASE CHARGES],[KALMAR HANDLING CHARGES],[HANDLING CHARGES LDD],[MONITORING CHARGES],[FUEL CHARGES],[INSPECTION CHARGES],[SSR CHARGES],[DOCUMENTATION CHARGES],[50% EXAMNIATION CHARGES],[HANDLING CHARGES],[P.O.ESCORT CHARGES],[HANDLING AND  PNR MOVEMENT CHARGES],[OOG DOCUMENTATION CHARGES],[100% EXAMINATION CHARGES],[ESCORT CRANE USAGE CHARGES])) pvt

我在一些线程中发现@columns不能在select语句中使用(因为该@columns中存在无效的列)因此会发生这种情况。但是,如果我从select语句中将其删除,则值将为null,而所有值都将为null,如果我将其放入select语句,则会出现此错误。

有人可以指出到底是什么问题。

0 个答案:

没有答案