动态SQL语法错误SQLScript SAP HANA

时间:2018-11-01 16:09:11

标签: sql syntax-error dynamic-sql hana hana-sql-script

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

该过程采用三个参数:

  • DimPartialName
  • ColumnName
  • UploadID

这是步骤:

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行col 1(在pos 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'; 

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

1 个答案:

答案 0 :(得分: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" = ZZ周围缺少单引号。

将语句更改为

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;

应解决此问题。