0

チェックボックスリストから選択した値を取得しようとしていますが、SQLでエラーが発生し続けます。

エラー

Msg 105, Level 15, State 1, Line 22
Unclosed quotation mark after the character string ')'.
Msg 102, Level 15, State 1, Line 22
Incorrect syntax near ')'.

SP

@DEPTCODE INT,
@COA VARCHAR(50),
@MAP VARCHAR(10)
AS
BEGIN
SET NOCOUNT ON;
DECLARE @SQL VARCHAR(MAX);

BEGIN
SET @SQL = N'SELECT 
              A.GL_EXCEPT_ID AS ''GL ID''
            , A.COA_SYSTEM AS ''Account Type''
            , A.ENTN AS ''Entity''
            , A.MED_CTR AS ''Medical Center''
            , (''['' + A.LOCN + ''] '' + B.LOCN_DESC) AS ''Location Code''
            , (''['' + A.CTRN + ''] '' + C.CTRN_DESC) AS ''Cost Center''
            , (''['' + RTRIM(A.FUND) + ''] '' + A.FUND_DESC) AS ''Fund''
            , A.ROLLUP_TYPE_ID AS ''Rollup ID''
            , A.ROLLUP_TYPE_DESC AS ''Rollup Type''
            , A.MAPPING AS ''Mapping''
FROM V_ROLLUP_GL_DESC_REV1 A
    LEFT JOIN T_ROLLUP_GL_LOCN B
        ON A.LOCN = B.LOCN
            AND A.MED_CTR_ID = B.MED_CTR_ID
            AND A.COA_SYSTEM = B.COA_SYSTEM
    LEFT JOIN T_ROLLUP_GL_CTRN C
        ON A.CTRN = C.CTRN
            AND A.COA_SYSTEM = C.COA_SYSTEM
WHERE (A.DEPT_ID = '+CONVERT(VARCHAR,@DEPTCODE)+')
        AND A.COA_SYSTEM IN ('+@COA+')
        AND A.MAPPING IN ('+@MAP+''')'
PRINT(@SQL)
EXEC(@SQL)
END
END

クエリ

USE [Rollup2]
GO

DECLARE @return_value int

EXEC    @return_value = [dbo].[USP_GET_GL_BY_DEPT]
    @DEPTCODE = 1000,
    @COA = N'''COA'',''COA_SYSTEM''',
    @MAP = N'''DEFAULT'',''EXCEPTION'''

SELECT  'Return Value' = @return_value
4

1 に答える 1

2

あなたの問題の一部は:

@MAP VARCHAR(10)

しかし、あなたははるかに長い値を渡しています:

@MAP = N'''DEFAULT'',''EXCEPTION'''

長さを次のように増やします。

@MAP VARCHAR(50)

2番目の問題は、この行です。一重引用符が多すぎます。

AND A.MAPPING IN ('+@MAP+''')'

次のように変更します。

AND A.MAPPING IN ('+@MAP+')'
于 2012-08-13T23:27:26.777 に答える