我正在使用这个简单的代码来转动某些列。
但由于这个错误,我无法使其发挥作用。
DECLARE @IO_dy AS VARCHAR(100) = '>9C604-M'
DECLARE @style_dy AS VARCHAR (100) = 'S1415MBS06'
DECLARE @query AS VARCHAR(8000)
DECLARE @con AS VARCHAR(8000)
SET @con = STUFF((SELECT distinct ',' + QUOTENAME(Size_id)
FROM iPLEXSTY_SIQ
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
SET @query = 'SELECT *
FROM (
SELECT DISTINCT a.Po_no,a.Article_id,a.Season_id,a.Customer_id,a.Destn_id,b.planned_dt, (c.Description + c.Resource_id) AS comb_size,a.Qty,a.Size_id
from iPLEXSTY_SIQ a
INNER JOIN iPLEX_BULK_PO_DET b on b.upload_batch_id = a.Batch_id
INNER JOIN iPLEXCOLORS c on c.Seq_no = a.Seq_no
WHERE IO_no = @IO_dy AND Style_id = @style_dy //ERROR HERE
GROUP BY a.Po_no,a.Article_id,a.Season_id,a.Customer_id,a.Destn_id,b.planned_dt,(c.Description + c.Resource_id),a.Qty,A.Size_id
) as s
PIVOT
(
SUM(Qty)
FOR Size_id IN (' +@con+ ')
)AS pvt'
EXEC(@query)
答案 0 :(得分:1)
另外一个答案 只是对查询中使用的变量名称的单引号
DECLARE @IO_dy AS VARCHAR(100) = '>9C604-M'
DECLARE @style_dy AS VARCHAR (100) = 'S1415MBS06'
DECLARE @query AS VARCHAR(8000)
DECLARE @con AS VARCHAR(8000)
SET @con = STUFF((SELECT distinct ',' + QUOTENAME(Size_id)
FROM iPLEXSTY_SIQ
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
SET @query = 'SELECT *
FROM (
SELECT DISTINCT a.Po_no,a.Article_id,a.Season_id,a.Customer_id,a.Destn_id,b.planned_dt, (c.Description + c.Resource_id) AS comb_size,a.Qty,a.Size_id
from iPLEXSTY_SIQ a
INNER JOIN iPLEX_BULK_PO_DET b on b.upload_batch_id = a.Batch_id
INNER JOIN iPLEXCOLORS c on c.Seq_no = a.Seq_no
WHERE IO_no = '''+@IO_dy+''' AND Style_id = '''+@style_dy+''' //ERROR HERE
GROUP BY a.Po_no,a.Article_id,a.Season_id,a.Customer_id,a.Destn_id,b.planned_dt,(c.Description + c.Resource_id),a.Qty,A.Size_id
) as s
PIVOT
(
SUM(Qty)
FOR Size_id IN (' +@con+ ')
)AS pvt'
EXEC(@query)
并且另一个答案解释了使用default
关键字
答案 1 :(得分:0)
您不能使用“=”声明和初始化变量的值。所以而不是:
DECLARE @IO_dy AS VARCHAR(100) = '>9C604-M'
使用
DECLARE @IO_dy AS VARCHAR(100) DEFAULT '>9C604-M'
^^^^^^^
请参阅syntax