变量标量声明错误

时间:2016-12-03 05:11:52

标签: sql-server function pivot

我有这样的查询:

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 pks=@pks and month(cpo.tanggal)=@bulan
            ) 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) >= '+@tahun +' and pks='+ @pks +' and month(cpo.tanggal)='+@bulan+'
           ) x
            pivot 
            (
                 max(harga_beli_tbs_bersih)
                for asal in (' + @cols + ')
            ) p ';

exec (@query);
end

当我运行它时出现错误:

  

[Err] 42000 - [SQL Server]必须声明标量变量" @ tahun"

我已经尝试过所有类型的变量声明,但我仍然遇到了这个错误。我还是SQL查询的新手。提前谢谢!

1 个答案:

答案 0 :(得分:0)

您正在将字符串与整数进行比较。

你可以尝试:

declare @tahun AS INT;
set @tahun = 2016;

并且

 YEAR (cpo.tanggal) >= '+ CONVERT(varchar(4), @tahun) +' and pks='