执行动态SQL查询时出错

时间:2018-03-14 06:15:23

标签: sql sql-server sql-server-2008 dynamic-queries

当我执行此动态查询时,我收到错误:

  

Msg 102,Level 15,State 1,Line 59   '1'附近的语法不正确。

ALTER PROCEDURE [dbo].[sp_getScheduleCactByClntId]  
@clientId nvarchar(50),
@fromDt nvarchar(50),
@toDt nvarchar(50),
@catid nvarchar(50),
@subcatId nvarchar(50),
@locId nvarchar(50),
@groupId nvarchar(50),
@cstcntrId nvarchar(50)
AS

BEGIN
    SET NOCOUNT ON;
    DECLARE @SQL nvarchar(max)
    SET @SQL = 'select catid,particular,(grossblock1+grossblock2)grossblock,addition,disp,
            (grossblock1+grossblock2+addition+disp+reval)balance,
            accdepopbl,(depamt+(-1*depsd))depamt,(accdepopbl+(-1*depsd)+(-1*depsdfy))depsd,
            (accdepopbl+(depamt+(-1*depsd)) - ((accdepopbl+(-1*depsd)+(-1*depsdfy))))balancedep,
            ((grossblock1+grossblock2+addition+disp+reval)-((accdepopbl+(depamt+(-1*depsd)) - ((accdepopbl+(-1*depsd)+(-1*depsdfy))))))netbalcl,
            (grossblock1+grossblock2 - dpon)  netbalop
            from (
            select cat.catid, isnull(cat.name,'''')particular,
            isnull(sum(case when convert(varchar(10),ad.ast_use_dt,126) <  '''+@fromDt+''' then astdd.adjustval_src else 0  end),0)grossblock1,
            isnull(sum(case when convert(varchar(10),ad.ast_use_dt,126) =  '''+@fromDt+''' and (isnull(astdd.opdepcmp,0) > 0 or ad.chkold = 1 )   then astdd.adjustval_src else 0  end),0)grossblock2,
            isnull(sum(addtn.adtnval),0)addition,
            -1 * sum(isnull(sd.sdval,0))disp,
            (sum(isnull(astdd.accudepval,0)) -  sum(isnull(debl.depamt,0)))accdepopbl,sum(isnull(depc.depamt,0))depamt,
            -1 * isnull(sum(depsd.depamt),0)depsd,
            -1 * isnull(sum(depsdopn.depamt),0)depsdopn,
            -1* isnull(sum(depsdfy.depamt),0)depsdfy,
            0.00 ''reval'',
            sum(isnull(dpon.depamt,0))dpon
            from
            tbl_AssetDetails ad 
            inner join tbl_Category cat on ad.ast_cat = cat.catid
            inner join tbl_AssetDepDetails astdd on ad.arid = astdd.arid
            left join (select sum(depamt)depamt,arid  from tbl_AssetDepRecord dprc 
                       where convert(varchar(10),dprc.todt,126) <=  '''+@fromDt+''' and dprc.clientId = '+@clientId+'
                       group by arid )dpon on dpon.arid = ad.arid
            left join ( select sum(depamt)depamt,arid from tbl_AssetDepRecord dprc 
                        where convert(varchar(10),dprc.todt,126) > = '''+@fromDt+''' and convert(varchar(10),dprc.todt,126) < = '''+@toDt+''' 
                        group by arid) depc on depc.arid = ad.arid
            left join ( select sum(addidp.adjustval_src)adtnval,addi.arid from tbl_AssetDetails addi 
                        inner join tbl_AssetDepDetails addidp 
                        on addi.arid = addidp.arid
                        where convert(varchar(10),addi.ast_use_dt,126) > = '''+@fromDt+''' and convert(varchar(10),addi.ast_use_dt,126) <= '''+@toDt+'''
                        and addidp.cact_deprt >= 0 and addi.clientId = '+@clientId+' and isnull(addi.chkold,0) != 1 and isnull(addidp.opdepcmp,0) <= 0
                        group by addi.arid )addtn
                        on addtn.arid = ad.arid
            left join (select sum(adp.adjustval_src)sdval,sdp.arid 
                       from tbl_SaleDispose sdp
                       inner join tbl_AssetDepDetails adp
                       on sdp.arid = adp.arid
                       where convert(varchar(10),sdp.sdate,126) > = '''+@fromDt+''' and convert(varchar(10),sdp.sdate,126) < = '''+@toDt+'''
                       and sdp.clientId = '+@clientId+'
                       group by sdp.arid )sd on sd.arid = ad.arid
            left join ( select sum(depamt)depamt,arid from tbl_AssetDepRecord deb 
                        where convert(varchar(10),deb.todt,126) > = '''+@fromDt+''' and deb.clientId = '+@clientId+'
                        group by arid) debl on debl.arid = ad.arid
            left join ( select sum(depamt)depamt,arid from tbl_AssetDepRecord dprc
                        inner join tbl_Depreciation dp on dprc.did= dp.did and dp.descp=''Sale/Dipose''
                        where convert(varchar(10),dprc.todt,126) > = '''+@fromDt+''' and convert(varchar(10),dprc.todt,126) < = '''+@toDt+''' 
                        and dprc.clientId = '+@clientId+'
                        and dprc.arid in (select sdp.arid from tbl_SaleDispose sdp where convert(varchar(10),sdp.sdate,126) > = '''+@fromDt+''' and 
                        convert(varchar(10),sdp.sdate,126) < = '''+@toDt+''' and sdp.clientId = '+@clientId+' )
                        group by arid) depsd on depsd.arid = ad.arid
            left join ( select sum(depamt)depamt,arid from tbl_AssetDepRecord dprc
                        inner join tbl_Depreciation dp on dprc.did= dp.did 
                        where convert(varchar(10),dprc.todt,126) < = '''+@toDt+''' 
                        and dprc.clientId = '+@clientId+'
                        and dprc.arid in (select sdp.arid from tbl_SaleDispose sdp where convert(varchar(10),sdp.sdate,126) > = '''+@fromDt+''' and 
                        convert(varchar(10),sdp.sdate,126) < = '''+@toDt+''' and sdp.clientId = '+@clientId+' )
                        group by arid) depsdopn on depsdopn.arid = ad.arid
            left join ( select sum(depamt)depamt,arid from tbl_AssetDepRecord dprc
                        inner join tbl_Depreciation dp on dprc.did= dp.did 
                        where convert(varchar(10),dprc.todt,126) > = '''+@fromDt+''' and convert(varchar(10),dprc.todt,126) < = '''+@toDt+''' 
                        and dprc.clientId = '+@clientId+'
                        and dprc.arid in (select sdp.arid from tbl_SaleDispose sdp where convert(varchar(10),sdp.sdate,126) > = '''+@fromDt+''' and 
                        convert(varchar(10),sdp.sdate,126) < = '''+@toDt+''' and sdp.clientId = '+@clientId+' )
                        group by arid) depsdfy on depsdfy.arid = ad.arid
            where convert(varchar(10),ad.ast_use_dt,126) < = '''+@toDt+''' and ad.clientId = '+@clientId+' and astdd.cact_deprt >= 0 and cat.method!=0 AND '

    IF(@catid!=0)
    SET @SQL = @SQL +' ad.ast_cat='+@catid+' AND '
    IF(@subcatId!=0)
    SET @SQL = @SQL +' ad.ast_subcat='+@subcatId+' AND '
    IF(@locId!=0)
    SET @SQL = @SQL + ' ad.ast_loc='+@locId+' AND '
    IF(@groupId!=0)
    SET @SQL = @SQL +' ad.ast_grp='+@groupId+' AND '
    IF(@cstcntrId!=0)
    SET @SQL = @SQL +' ad.ast_costcen='+@cstcntrId+' AND '

    SET @SQL = @SQL +' 1 = 1  group by cat.catid,cat.name )x '

    PRINT @SQL
    EXEC sp_executesql @SQL
END

我执行了@SQL变量的输出(我通过PRINT命令获得)它完美运行。有什么我想念的东西。这之前执行得很完美,我在查询中做了一些更改,现在一切都搞砸了!我很快就会把它投入生产。

@SQL的部分输出是:

select catid,particular,(grossblock1+grossblock2)grossblock,addition,disp,
        (grossblock1+grossblock2+addition+disp+reval)balance,
        accdepopbl,(depamt+(-1*depsd))depamt,(accdepopbl+(-1*depsd)+(-1*depsdfy))depsd,
        (accdepopbl+(depamt+(-1*depsd)) - ((accdepopbl+(-1*depsd)+(-1*depsdfy))))balancedep,
        ((grossblock1+grossblock2+addition+disp+reval)-((accdepopbl+(depamt+(-1*depsd)) - ((accdepopbl+(-1*depsd)+(-1*depsdfy))))))netbalcl,
        (grossblock1+grossblock2 - dpon)  netbalop
        from (
        select cat.catid, isnull(cat.name,'')particular,
        isnull(sum(case when convert(varchar(10),ad.ast_use_dt,126) <  '2017-04-01' then astdd.adjustval_src else 0  end),0)grossblock1,
        isnull(sum(case when convert(varchar(10),ad.ast_use_dt,126) =  '2017-04-01' and (isnull(astdd.opdepcmp,0) > 0 or ad.chkold = 1 )   then astdd.adjustval_src else 0  end),0)grossblock2,
        isnull(sum(addtn.adtnval),0)addition,
        -1 * sum(isnull(sd.sdval,0))disp,
        (sum(isnull(astdd.accudepval,0)) -  sum(isnull(debl.depamt,0)))accdepopbl,sum(isnull(depc.depamt,0))depamt,
        -1 * isnull(sum(depsd.depamt),0)depsd,
        -1 * isnull(sum(depsdopn.depamt),0)depsdopn,
        -1* isnull(sum(depsdfy.depamt),0)depsdfy,
        0.00 'reval',
        sum(isnull(dpon.depamt,0))dpon
        from
        tbl_AssetDetails ad 
        inner join tbl_Category cat on ad.ast_cat = cat.catid
        inner join tbl_AssetDepDetails astdd on ad.arid = astdd.arid
        left join (select sum(depamt)depamt,arid  from tbl_AssetDepRecord dprc 
                   where convert(varchar(10),dprc.todt,126) <=  '2017-04-01' and dprc.clientId = 3
                   group by arid )dpon on dpon.arid = ad.arid
        left join ( select sum(depamt)depamt,arid from tbl_AssetDepRecord dprc 
                    where convert(varchar(10),dprc.todt,126) > = '2017-04-01' and convert(varchar(10),dprc.todt,126) < = '2018-03-31' 
                    group by arid) depc on depc.arid = ad.arid
        left join ( select sum(addidp.adjustval_src)adtnval,addi.arid from tbl_AssetDetails addi 
                    inner join tbl_AssetDepDetails addidp 
                    on addi.arid = addidp.arid
                    where convert(varchar(10),addi.ast_use_dt,126) > = '2017-04-01' and convert(varchar(10),addi.ast_use_dt,126) <= '2018-03-31'
                    and addidp.cact_deprt >= 0 and addi.clientId = 3 and isnull(addi.chkold,0) != 1 and isnull(addidp.opdepcmp,0) <= 0
                    group by addi.arid )addtn
                    on addtn.arid = ad.arid
        left join (select sum(adp.adjustval_src)sdval,sdp.arid 
                   from tbl_SaleDispose sdp
                   inner join tbl_AssetDepDetails adp
                   on sdp.arid = adp.arid
                   where convert(varchar(10),sdp.sdate,126) > = '2017-04-01' and convert(varchar(10),sdp.sdate,126) < = '2018-03-31'
                   and sdp.clientId = 3
                   group by sdp.arid )sd on sd.arid = ad.arid
        left join ( select sum(depamt)depamt,arid from tbl_AssetDepRecord deb 
                    where convert(varchar(10),deb.todt,126) > = '2017-04-01' and deb.clientId = 3
                    group by arid) debl on debl.arid = ad.arid
        left join ( select sum(depamt)depamt,arid from tbl_AssetDepRecord dprc
                    inner join tbl_Depreciation dp on dprc.did= dp.did and dp.descp='Sale/Dipose'
                    where convert(varchar(10),dprc.todt,126) > = '2017-04-01' and convert(varchar(10),dprc.todt,126) < = '2018-03-31' 
                    and dprc.clientId = 3
                    and dprc.arid in (select sdp.arid from tbl_SaleDispose sdp where convert(varchar(10),sdp.sdate,126) > = '2017-04-01' and 
                    convert(varchar(10),sdp.sdate,126) < = '2018-03-31' and sdp.clientId = 3 )
                    group by arid) depsd on depsd.arid = ad.arid
        left join ( select sum(depamt)depamt,arid from tbl_AssetDepRecord dprc
                    inner join tbl_Depreciation dp on dprc.did= dp.did 
                    where convert(varchar(10),dprc.todt,126) < = '2018-03-31' 
                    and dprc.clientId = 3
                    and dprc.arid in (select sdp.arid from tbl_SaleDispose sdp where convert(varchar(10),sdp.sdate,126) > = '2017-04-01' and 
                    convert(varchar(10),sdp.sdate,126) < = '2018-03-31' and sdp.clientId = 3 )
                    group by arid) depsdop

1 个答案:

答案 0 :(得分:1)

即使变量@sql被声明为nvarchar(max),但是连接字符串select catid ...不是nvarchar(max),并且在被分配给@sql之前它被截断了。

set @sql = convert(nvarchar(max), '')
          + 'select catid ...'

这将确保在分配给@sql变量之前,连接字符串的结果是nvarchar(max)

另请注意,PRINT功能能打印出多少个字符取决于SSMS中的设置。无论设置如何,都有最大值,因此对于大型动态查询,您将无法打印完整查询