MS SQL存储过程运行时错误

时间:2014-08-07 06:09:01

标签: sql sql-server stored-procedures

如何在连接MSSQL查询时纠正sp中的错误?

SET  @Ws_Sql = 'SELECT CONVERT(VARCHAR(25),@WS_STATUS) AS [Status],A.RECNUB AS [Rec #], A.REVCOD AS [Revenue CODE], ' 
IF @LNKOPT = 1 
BEGIN
    SET  @Ws_Sql = @WS_SQL +''' AS [Status],'
END
ELSE IF @LNKOPT = 2
BEGIN
    SET  @Ws_Sql = @WS_SQL +'' + 'Modified' + ' AS [Status],'
END
ELSE IF @LNKOPT = 3
BEGIN
    SET  @Ws_Sql = @WS_SQL +'' + 'Deleted' + ' AS [Status],'
END
ELSE IF @LNKOPT = 4
BEGIN
    SET  @Ws_Sql = @WS_SQL +'' + 'Reprint' + ' AS [Status],'
END

1 个答案:

答案 0 :(得分:0)

您是否尝试将空值分配给[状态]输出列,以防@LNKOPT = 1?

在这种情况下,你需要使用双撇号来逃避撇号字符:

IF @LNKOPT = 1 
BEGIN
    SET  @Ws_Sql = @WS_SQL +''''' AS [Status],'
END

将导致@Ws_Sql包含:'' AS [Status](否则会导致' AS [Status],这会在执行@Ws_Sql字符串时出错。