如何在我的 SQL 动态数据透视查询中为这行代码编写正确的语法?
How to write the proper syntax for this line of code in my SQL dynamic Pivot Query?
我在 SQL Server 2014 上有一个动态数据透视查询 运行。这是我的原始查询,它运行良好:
DECLARE @sql NVARCHAR(max),
@cols VARCHAR(max)
SET @cols = (SELECT DISTINCT a.FOC_TRANSACTIONCODE + ','
FROM P5FOLIOCHARGE a
LEFT JOIN P5FOLIOHEADER b
ON a.FOC_ACCOUNT = b.FOH_ACCOUNT
WHERE b.FOH_PMSCONFIRMATIONNUMBER = '39576189-1'
AND FOC_NETAMOUNT NOT LIKE '-%'
FOR xml path(''))
SELECT @cols = LEFT(@cols, Len(@cols) - 1)
SET @sql = 'SELECT *
FROM (SELECT b.FOH_PMSCONFIRMATIONNUMBER,
a.FOC_ACCOUNT,
a.FOC_PROPERTY,
a.FOC_DATE,
a.FOC_TRANSACTIONCODE,
CASE WHEN a.FOC_NETAMOUNT > 0 THEN a.FOC_NETAMOUNT ELSE 0 END AS FOC_NETAMOUNT
FROM P5FOLIOCHARGE a
LEFT JOIN P5FOLIOHEADER b ON a.FOC_ACCOUNT = b.FOH_ACCOUNT
where b.FOH_PMSCONFIRMATIONNUMBER = ''39576189-1''
AND FOC_NETAMOUNT NOT LIKE ''-%'')a
PIVOT (Sum(FOC_NETAMOUNT)
FOR FOC_TRANSACTIONCODE IN (' + @cols + ')) pv '
EXEC Sp_executesql @sql
我现在对它进行了一些修改,但是我 运行 遇到语法错误,我添加了以下代码行:
WHERE datename(m,FOC_DATE) + ' ' + cast(datepart(yyyy,FOC_DATE) as varchar) = 'FEBRUARY 2015'
查询现在如下所示:
DECLARE @sql NVARCHAR(max),
@cols VARCHAR(max)
SET @cols = (SELECT DISTINCT a.FOC_TRANSACTIONCODE + ','
FROM P5FOLIOCHARGE a
LEFT JOIN P5FOLIOHEADER b
ON a.FOC_ACCOUNT = b.FOH_ACCOUNT
WHERE datename(m,FOC_DATE) + ' ' + cast(datepart(yyyy,FOC_DATE) as varchar) = 'FEBRUARY 2015'
AND FOC_PROPERTY = 'ZIL'
AND FOC_NETAMOUNT NOT LIKE '-%'
FOR xml path(''))
SELECT @cols = LEFT(@cols, Len(@cols) - 1)
SET @sql = 'SELECT *
FROM (SELECT b.FOH_PMSCONFIRMATIONNUMBER,
a.FOC_ACCOUNT,
a.FOC_date,
a.FOC_PROPERTY,
a.FOC_TRANSACTIONCODE,
CASE WHEN a.FOC_NETAMOUNT > 0 THEN a.FOC_NETAMOUNT ELSE 0 END AS FOC_NETAMOUNT
FROM P5FOLIOCHARGE a
LEFT JOIN P5FOLIOHEADER b ON a.FOC_ACCOUNT = b.FOH_ACCOUNT
WHERE datename(m,FOC_DATE) + ' ' + cast(datepart(yyyy,FOC_DATE) as varchar) = ''FEBRUARY 2015''
AND FOC_PROPERTY = 'ZIL'
AND FOC_NETAMOUNT NOT LIKE ''-%'')a
PIVOT (Sum(FOC_NETAMOUNT)
FOR FOC_TRANSACTIONCODE IN (' + @cols + ')) pv '
EXEC Sp_executesql @sql
我在查询中第二次出现语法错误(在我上面提到的代码中)(即在 SET @ sql=.... 之后)
我尝试了几种使用 ' 符号的组合,但仍然失败。我想这一定是一个简单的编码语法错误,但它对我不起作用!
我不确定,但请检查一下
WHERE datename(m,FOC_DATE) + ' ' + cast(datepart(yyyy,FOC_DATE) as varchar) = ''FEBRUARY 2015''
应该是WHERE datename(m,FOC_DATE) + '' '' + cast(datepart(yyyy,FOC_DATE) as varchar) = ''FEBRUARY 2015''
AND FOC_PROPERTY = 'ZIL'
应该是AND FOC_PROPERTY = ''ZIL''
我在 SQL Server 2014 上有一个动态数据透视查询 运行。这是我的原始查询,它运行良好:
DECLARE @sql NVARCHAR(max),
@cols VARCHAR(max)
SET @cols = (SELECT DISTINCT a.FOC_TRANSACTIONCODE + ','
FROM P5FOLIOCHARGE a
LEFT JOIN P5FOLIOHEADER b
ON a.FOC_ACCOUNT = b.FOH_ACCOUNT
WHERE b.FOH_PMSCONFIRMATIONNUMBER = '39576189-1'
AND FOC_NETAMOUNT NOT LIKE '-%'
FOR xml path(''))
SELECT @cols = LEFT(@cols, Len(@cols) - 1)
SET @sql = 'SELECT *
FROM (SELECT b.FOH_PMSCONFIRMATIONNUMBER,
a.FOC_ACCOUNT,
a.FOC_PROPERTY,
a.FOC_DATE,
a.FOC_TRANSACTIONCODE,
CASE WHEN a.FOC_NETAMOUNT > 0 THEN a.FOC_NETAMOUNT ELSE 0 END AS FOC_NETAMOUNT
FROM P5FOLIOCHARGE a
LEFT JOIN P5FOLIOHEADER b ON a.FOC_ACCOUNT = b.FOH_ACCOUNT
where b.FOH_PMSCONFIRMATIONNUMBER = ''39576189-1''
AND FOC_NETAMOUNT NOT LIKE ''-%'')a
PIVOT (Sum(FOC_NETAMOUNT)
FOR FOC_TRANSACTIONCODE IN (' + @cols + ')) pv '
EXEC Sp_executesql @sql
我现在对它进行了一些修改,但是我 运行 遇到语法错误,我添加了以下代码行:
WHERE datename(m,FOC_DATE) + ' ' + cast(datepart(yyyy,FOC_DATE) as varchar) = 'FEBRUARY 2015'
查询现在如下所示:
DECLARE @sql NVARCHAR(max),
@cols VARCHAR(max)
SET @cols = (SELECT DISTINCT a.FOC_TRANSACTIONCODE + ','
FROM P5FOLIOCHARGE a
LEFT JOIN P5FOLIOHEADER b
ON a.FOC_ACCOUNT = b.FOH_ACCOUNT
WHERE datename(m,FOC_DATE) + ' ' + cast(datepart(yyyy,FOC_DATE) as varchar) = 'FEBRUARY 2015'
AND FOC_PROPERTY = 'ZIL'
AND FOC_NETAMOUNT NOT LIKE '-%'
FOR xml path(''))
SELECT @cols = LEFT(@cols, Len(@cols) - 1)
SET @sql = 'SELECT *
FROM (SELECT b.FOH_PMSCONFIRMATIONNUMBER,
a.FOC_ACCOUNT,
a.FOC_date,
a.FOC_PROPERTY,
a.FOC_TRANSACTIONCODE,
CASE WHEN a.FOC_NETAMOUNT > 0 THEN a.FOC_NETAMOUNT ELSE 0 END AS FOC_NETAMOUNT
FROM P5FOLIOCHARGE a
LEFT JOIN P5FOLIOHEADER b ON a.FOC_ACCOUNT = b.FOH_ACCOUNT
WHERE datename(m,FOC_DATE) + ' ' + cast(datepart(yyyy,FOC_DATE) as varchar) = ''FEBRUARY 2015''
AND FOC_PROPERTY = 'ZIL'
AND FOC_NETAMOUNT NOT LIKE ''-%'')a
PIVOT (Sum(FOC_NETAMOUNT)
FOR FOC_TRANSACTIONCODE IN (' + @cols + ')) pv '
EXEC Sp_executesql @sql
我在查询中第二次出现语法错误(在我上面提到的代码中)(即在 SET @ sql=.... 之后)
我尝试了几种使用 ' 符号的组合,但仍然失败。我想这一定是一个简单的编码语法错误,但它对我不起作用!
我不确定,但请检查一下
WHERE datename(m,FOC_DATE) + ' ' + cast(datepart(yyyy,FOC_DATE) as varchar) = ''FEBRUARY 2015''
应该是WHERE datename(m,FOC_DATE) + '' '' + cast(datepart(yyyy,FOC_DATE) as varchar) = ''FEBRUARY 2015''
AND FOC_PROPERTY = 'ZIL'
应该是AND FOC_PROPERTY = ''ZIL''