1

我在存储过程中有动态 SQL。

该过程采用三个参数:

  • 暗部名称
  • 列名
  • 上传 ID

这是程序:

PROCEDURE "Schema"."DeletefromDIM" (In DimPartialName NVARCHAR(50), In 
IDColumnName NVARCHAR(50), IN UploadID NVARCHAR(36) ) 
LANGUAGE SQLSCRIPT
SQL SECURITY INVOKER 
--DEFAULT SCHEMA <default_schema_name>
AS
BEGIN
EXECUTE IMMEDIATE
'select distinct "'|| IDColumnName ||'" from 
"Schema"."ZT.'|| DimPartialName ||'" dim
LEFT JOIN "Schema"."SourceTable" raw on 
dim."AUDIT.CREATED_ON" = raw."Audit.CREATED_ON"
where "UPLOAD_UUID" = ' || UploadID ||' ';
End;

和错误:

SAP DBTech JDBC:[257]:sql 语法错误:“Schema”。“DeletefromDIM”:第 15 列第 1 行(位置 520):[257](范围 3)sql 语法错误异常:sql 语法错误:“附近的语法不正确” -”:第 3 行第 41 列(在 pos 214)

当我在控制台中运行此 SQL 并替换我的输入时,它工作正常:

select distinct "BRANDID" from "Schema"."ZT.BRAND" dim
LEFT JOIN "Schema"."SourceTable" raw on dim."AUDIT.CREATED_ON" = raw."Audit.CREATED_ON"
where "UPLOAD_UUID" = 'f889e016-1364-4aac-9536-037d932c55b5'; 

该错误实际上没有意义,因为该位置不仅没有“-”,而且整个语句中也没有破折号。

4

1 回答 1

2

您应该通过选择它作为输出来检查生成的 SQL 字符串,如下所示:

BEGIN

        select                'select distinct "'|| IDColumnName ||'" from 
                        "Schema"."ZT.'|| DimPartialName ||'" dim
                        LEFT JOIN "Schema"."SourceTable" raw on 
                        dim."AUDIT.CREATED_ON" = raw."Audit.CREATED_ON"
                        where "UPLOAD_UUID" = ' || UploadID ||' ' as SQLTEXT from dummy;

End;

如果你这样做,你会发现生成的 SQL 语句看起来像这样(对于'X'、'Y'、'Z'的输入):

select distinct "Y" from 
                    "Schema"."ZT.X" dim
                    LEFT JOIN "Schema"."SourceTable" raw on 
                    dim."AUDIT.CREATED_ON" = raw."Audit.CREATED_ON"
                    where "UPLOAD_UUID" = Z 

WHERE请注意,条件如何在."UPLOAD_UUID" = Z周围缺少单引号Z

将语句更改为

BEGIN

    execute immediate 
                  'select distinct "'|| :IDColumnName ||'" ' 
               || 'from '
               || '"Schema"."ZT.'|| :DimPartialName ||'" dim '
               || 'LEFT JOIN "Schema"."SourceTable" raw on '
               || 'dim."AUDIT.CREATED_ON" = raw."Audit.CREATED_ON" '
               || 'where "UPLOAD_UUID" = ''' || :UploadID ||''' ';

END;

应该解决这个问题。

于 2018-11-02T03:23:37.830 回答