带CASE STATEMENT的动态sql

时间:2015-06-24 10:13:04

标签: sql sql-server sql-server-2008 sql-server-2008-r2

   SET @SQL =
        'SELECT
        CaseStatus =
            CASE Level1Status
                WHEN 1100 THEN ''Case Submitted to QC''
                WHEN 1200 THEN ''Pending QC''
                WHEN 1400 THEN ''Passed QC''

            END,

我目前在上面的动态sql / case语句中遇到问题,因为我似乎无法将字符串放入动态sql中,是否有人碰巧有解决方法来修复此问题

非常感谢

修改

AS 开始     DECLARE @SQL nvarchar(4000)

SET @SQL =
    'SELECT
    CaseStatus =
        CASE Level1Status
            WHEN 1100 THEN ''Case Submitted to QC''
            WHEN 1200 THEN ''Pending QC''
            WHEN 1400 THEN ''Passed QC''

        END,

        CaseStartDateTime,
        CaseEndDateTime,
        StatusName,
        Cell_NameDescription,
        QCAnalystName,
        AnalystName,
        Upload_Datetime,
        Requesting_Entity,
        Legal_Entity_TypeDescription,
        HighPriorityDescription,
        DD_Level_RequiredDescription,
        CountryDscr,
        Maintable.KYCCaseId AS KYCCaseId
    FROM
        UACTc75760ab10784b51b585f082d4b25223 AS MI,
        UACT175e55161660402692a53a4cdeb89bd6 AS MainTable,
        UACT5996d6e5151245cab24e4e76e3e53540 AS Statuses,
        UACTde5f05df6c5f4872a1e57b3cf8368301 AS AddressDetails
    WHERE
        (
        MI.CaseStartDateTime BETWEEN ' + quotename(convert(varchar(10), @CaseStartDateTime, 120), '''') + ' AND '  + quotename(convert(varchar(10), @CaseEndDateTime, 120), '''') +
        ' OR
        MI.CaseEndDateTime BETWEEN ' + quotename(convert(varchar(10), @CaseStartDateTime, 120), '''') +  ' AND ' + quotename(convert(varchar(10), @CaseEndDateTime, 120), '''') +
        ' )
    AND
        MI.KYCCase_Id = MainTable.KYCCaseId'
    IF @StatusName IS NOT NULL AND ltrim(rtrim(@StatusName)) != N''
        SET @SQL = @SQL + ' 
        AND
            Statuses.SourceStatus = MainTable.Level1Status
        AND
            Statuses.StatusName = ' + quotename(@StatusName, '''')
    IF @CountryDscr IS NOT NULL AND ltrim(rtrim(@CountryDscr)) != N''
    SET @SQL = @SQL + '
        AND
            AddressDetails.CountryDscr = ' + quotename(@CountryDscr, '''')      
    IF @CellDscr IS NOT NULL AND ltrim(rtrim(@CellDscr)) != N''
        SET @SQL = @SQL + ' 
        AND
            MainTable.Cell_NameDescription = ' + quotename(@CellDscr, '''') 
    IF @QCAnalystName IS NOT NULL AND ltrim(rtrim(@QCAnalystName)) != N''
        SET @SQL = @SQL + '
        AND
            MainTable.QCAnalystName = ' + quotename(@QCAnalystName, '''')   
    IF @AnalystName IS NOT NULL AND ltrim(rtrim(@AnalystName)) != N''
        SET @SQL = @SQL + '
        AND
            MainTable.AnalystName = ' + quotename(@AnalystName, '''')
    IF @RequestingEntity IS NOT NULL AND ltrim(rtrim(@RequestingEntity)) != N''
        SET @SQL = @SQL + '
        AND
            MainTable.Requesting_Entity = ' + quotename(@RequestingEntity, '''')
    IF @EntityType IS NOT NULL AND ltrim(rtrim(@EntityType)) != N''
        SET @SQL = @SQL + '
        AND
            MainTable.Legal_Entity_TypeDescription = ' + quotename(@EntityType, '''')
    IF @HighPriority IS NOT NULL AND ltrim(rtrim(@HighPriority)) != N''
        SET @SQL = @SQL + '
        AND
            MainTable.HighPriorityDescription = ' + quotename(@HighPriority, '''')
    IF @DDLevelRequired IS NOT NULL AND ltrim(rtrim(@DDLevelRequired)) != N''
        SET @SQL = @SQL + '
        AND
            MainTable.DD_Level_RequiredDescription = ' + quotename(@DDLevelRequired, '''')  

EXEC sp_executesql @SQL

这是整个代码,我得到的错误是

第15行,第15行,第1行,第32行 关键字“Case”附近的语法不正确。

2 个答案:

答案 0 :(得分:0)

这很好。

Create Table #Level1Status(Id int identity ,Level1Status int)

Insert INto #Level1Status(Level1Status) values(1100),(1200),(1400)


            Declare @SQL Nvarchar(Max)
            SET @SQL =
                    'SELECT 
                    CaseStatus =
                        CASE Level1Status
                            WHEN 1100 THEN ''Case Submitted to QC''
                            WHEN 1200 THEN ''Pending QC''
                            WHEN 1400 THEN ''Passed QC''

                        END from #Level1Status';
            Exec SP_ExecuteSQL @SQL

答案 1 :(得分:0)

由于你将@SQL字符串与本地变量" @ xxx"连接起来,如果任何一个可能为空,它想要给出任何输出,所以在连接时使用ISNULL()或COALESCE()您使用的本地可变性。