不工作时的情况

时间:2013-10-05 04:57:35

标签: sql sql-server tsql

我是初学者在T-Sql中,我使用case创建串联字符串,但是它的抛出错误。 请指出语法错误的地方..

Declare @intCnt int;
Declare @strQuery Varchar(max);
Set @intCnt=1;
set @strQuery='';
While @intCnt<25
Begin
Case @strQuery
    When @strQuery='' then 
        Set @strQuery='Select Doctot_ID,Product_ID,'+ Cast(@intCnt as varchar(2))  +' As Month_ID,''NRx'' as Metric,PROD_NRx_'+ Cast(@intCnt as Varchar(2)) +' as Sales From Cube_Train_Prescription Where  Cast(PROD_NRx_'+ Cast(@intCnt as Varchar(2)) +' as int) <>0'
    else
        Set @strQuery=@strQuery + Char(13) + Char(10) + ' Union '+ Char(13) + Char(10) + ' Select Doctot_ID,Product_ID,'+ Cast(@intCnt as varchar(2)) +' As Month_ID,''NRx'' as Metric,PROD_NRx_'+ Cast(@intCnt as Varchar(2)) +' as Sales From Cube_Train_Prescription Where  Cast(PROD_NRx_'+ Cast(@intCnt as Varchar(2)) +' as int) <>0'
    End
End
Set @intCnt=@intCnt+1
End
Print @strQuery

5 个答案:

答案 0 :(得分:1)

T-SQL的CASE语句只能用于返回单个原子值 - 它不能用于执行代码。它不等同于C#中的switch

您需要将代码修改为:

DECLARE @intCnt INT;
DECLARE @strQuery VARCHAR(MAX);

SET @intCnt = 1;
SET @strQuery = '';

WHILE @intCnt < 25
BEGIN
   SET @strQuery =
         CASE 
            WHEN @strQuery='' 
              THEN 'Select Doctot_ID,Product_ID,'+ Cast(@intCnt as varchar(2))  +' As Month_ID,''NRx'' as Metric,PROD_NRx_'+ Cast(@intCnt as Varchar(2)) +' as Sales From Cube_Train_Prescription Where  Cast(PROD_NRx_'+ Cast(@intCnt as Varchar(2)) +' as int) <>0'
            ELSE 
                @strQuery + Char(13) + Char(10) + ' Union '+ Char(13) + Char(10) + ' Select Doctot_ID,Product_ID,'+ Cast(@intCnt as varchar(2)) +' As Month_ID,''NRx'' as Metric,PROD_NRx_'+ Cast(@intCnt as Varchar(2)) +' as Sales From Cube_Train_Prescription Where  Cast(PROD_NRx_'+ Cast(@intCnt as Varchar(2)) +' as int) <>0'
         END

    SET @intCnt = @intCnt + 1
END

PRINT @strQuery

有关详细信息,请参阅免费提供的online documentation on CASE

答案 1 :(得分:0)

SELECT语句中的用例。

SELECT CASE WHEN <PREDICATE> THEN <RESULT> ELSE <RESULT> END

答案 2 :(得分:0)

用户if else条件语句进行查询。 (Case When)用于在某些条件基础上获取/放置值

答案 3 :(得分:0)

我相信你在这里不需要CASE声明。您只是使用case语句来检查它是否是循环的第一次迭代。你可以把它从循环中拉出来。

DECLARE @intCnt INT;
DECLARE @strQuery VARCHAR(MAX);


SET @strQuery = 'Select Doctot_ID,Product_ID,1 As Month_ID,''NRx'' as Metric,PROD_NRx_1 as Sales From Cube_Train_Prescription Where  Cast(PROD_NRx_1 as int) <>0'
SET @intCnt = 2;

WHILE @intCnt < 25
BEGIN
    SET @strQuery = @strQuery + Char(13) + Char(10) + ' Union '+ Char(13) + Char(10) + ' Select Doctot_ID,Product_ID,'+ Cast(@intCnt as varchar(2)) +' As Month_ID,''NRx'' as Metric,PROD_NRx_'+ Cast(@intCnt as Varchar(2)) +' as Sales From Cube_Train_Prescription Where  Cast(PROD_NRx_'+ Cast(@intCnt as Varchar(2)) +' as int) <>0'

    SET @intCnt = @intCnt + 1
END

PRINT @strQuery

答案 4 :(得分:-1)

修改 When @strQuery=''when ''

Case @strQuery
When '' then 
    Set @strQuery='Select Doctot_ID,Product_ID,'+ Cast(@intCnt as varchar(2))  +' As Month_ID,''NRx'' as Metric,PROD_NRx_'+ Cast(@intCnt as Varchar(2)) +' as Sales From Cube_Train_Prescription Where  Cast(PROD_NRx_'+ Cast(@intCnt as Varchar(2)) +' as int) <>0'
else
    Set @strQuery=@strQuery + Char(13) + Char(10) + ' Union '+ Char(13) + Char(10) + ' Select Doctot_ID,Product_ID,'+ Cast(@intCnt as varchar(2)) +' As Month_ID,''NRx'' as Metric,PROD_NRx_'+ Cast(@intCnt as Varchar(2)) +' as Sales From Cube_Train_Prescription Where  Cast(PROD_NRx_'+ Cast(@intCnt as Varchar(2)) +' as int) <>0'
End