0
は、私はこのようなクエリがある:可変スカラ宣言エラー
declare @cols AS NVARCHAR(MAX);
declare @query AS NVARCHAR(MAX);
declare @tahun AS VARCHAR(4);
declare @pks AS VARCHAR(2);
declare @bulan AS VARCHAR(2);
set @tahun = '2016';
set @pks = '40';
set @bulan = '11';
SET @cols = STUFF((SELECT distinct ',' + QUOTENAME(c.asal)
FROM (
SELECT
cpo.tanggal,
dg.asal,
(((((cpo - ongkos_ms)* CAST(NULLIF(dg.minyak_efektif,0) as float)/(NULLIF(dg.tbs_diolah, 0))) +((pko - ongkos_is)* CAST(NULLIF(dg.inti_efektif,0) as float)/(NULLIF(dg.tbs_diolah, 0))))-(447.6 * ((CAST(NULLIF(dg.minyak_efektif,0) as float)/(NULLIF(dg.tbs_diolah, 0)))+(CAST(NULLIF(dg.inti_efektif,0) as float)/(NULLIF(dg.tbs_diolah, 0))))))-(((((cpo - ongkos_ms)* CAST(NULLIF(dg.minyak_efektif,0) as float)/(NULLIF(dg.tbs_diolah, 0))) +((pko - ongkos_is)* CAST(NULLIF(dg.inti_efektif,0) as float)/(NULLIF(dg.tbs_diolah, 0))))-(447.6 * ((CAST(NULLIF(dg.minyak_efektif,0) as float)/(NULLIF(dg.tbs_diolah, 0)))+(CAST(NULLIF(dg.inti_efektif,0) as float)/(NULLIF(dg.tbs_diolah, 0)))))) * 0.02))
as harga_beli_tbs_bersih
FROM
PROD.dbo.[TBSP3.HargaCPOPKO] cpo
LEFT JOIN PROD.dbo.daily_GUUXX AS dg ON CONVERT (datetime, dg.tglolah, 103) = cpo.tanggal
LEFT JOIN PNL_TR_HARGA_KOMODITI AS ko ON ko.tanggal = cpo.tanggal
WHERE
YEAR (cpo.tanggal) >= @tahun and [email protected] and month(cpo.tanggal)[email protected]
) c
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'');
set @query = 'SELECT tanggal, ' + @cols + ' from
(
SELECT
cpo.tanggal,
dg.asal,
(((((cpo - ongkos_ms)* CAST(NULLIF(dg.minyak_efektif,0) as float)/(NULLIF(dg.tbs_diolah, 0))) +((pko - ongkos_is)* CAST(NULLIF(dg.inti_efektif,0) as float)/(NULLIF(dg.tbs_diolah, 0))))-(447.6 * ((CAST(NULLIF(dg.minyak_efektif,0) as float)/(NULLIF(dg.tbs_diolah, 0)))+(CAST(NULLIF(dg.inti_efektif,0) as float)/(NULLIF(dg.tbs_diolah, 0))))))-(((((cpo - ongkos_ms)* CAST(NULLIF(dg.minyak_efektif,0) as float)/(NULLIF(dg.tbs_diolah, 0))) +((pko - ongkos_is)* CAST(NULLIF(dg.inti_efektif,0) as float)/(NULLIF(dg.tbs_diolah, 0))))-(447.6 * ((CAST(NULLIF(dg.minyak_efektif,0) as float)/(NULLIF(dg.tbs_diolah, 0)))+(CAST(NULLIF(dg.inti_efektif,0) as float)/(NULLIF(dg.tbs_diolah, 0)))))) * 0.02))
as harga_beli_tbs_bersih
FROM
PROD.dbo.[TBSP3.HargaCPOPKO] cpo
LEFT JOIN PROD.dbo.daily_GUUXX AS dg ON CONVERT (datetime, dg.tglolah, 103) = cpo.tanggal
LEFT JOIN PNL_TR_HARGA_KOMODITI AS ko ON ko.tanggal = cpo.tanggal
WHERE
YEAR (cpo.tanggal) >= '[email protected] +' and pks='+ @pks +' and month(cpo.tanggal)='[email protected]+'
) x
pivot
(
max(harga_beli_tbs_bersih)
for asal in (' + @cols + ')
) p ';
exec (@query);
end
私はそれを実行すると、私はエラーを取得する:
[Err] 42000 - [SQL Server] Must declare the scalar variable "@tahun"
私はすでに変数宣言のすべての種類を試してみましたが、私はまだですそのエラーを取得します。私はまだSQLクエリに新しいです。前もって感謝します!