简体   繁体   中英

MS SQL Stored Procedure run time error

How to rectify error in sp while concatenating the MSSQL queries?

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

Are you attempting to assign an empty value to the [Status] output column, in case @LNKOPT = 1?

In that case, you need to use double apostrophes, to escape the apostrophe character:

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

which will result in @Ws_Sql containing: '' AS [Status] (otherwise it would result in ' AS [Status] which will give an error when the @Ws_Sql string is executed).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM