ibm db2:在匿名块中将字符串插入数据库

时间:2017-04-07 08:42:06

标签: sql plsql db2

我正在尝试将字符串数据从匿名PL / SQL块插入到db2表中。

什么有效;

SET SQLCOMPAT PLSQL;

BEGIN
...
SET ID_CITY = 0; SET city = chr(39) || 'Berlin' || chr(39); SET country_id = 83; SET REVNAME = chr(39) || 'create-script' || chr(39); SET REVCREATOR = chr(39) || 'create-script' || chr(39);
SET statement = 'INSERT INTO fk_city (ID_CITY, city, country_id, is_europe, REVNAME, REVDATE, REVFIRST, last_visit, REVCREATOR ) VALUES ( ' || ID_CITY || ', ' || city ||', ' || country_id || ', true, ' || REVNAME || ', CURRENT DATE, CURRENT DATE, CURRENT DATE, ' || REVCREATOR || ' )';
EXECUTE IMMEDIATE statement;
...
END

我的问题是:是否有另一种方法可以使用oneliner插入表中?例如:

EXECUTE IMMEDIATE "INSERT INTO fk_city (ID_CITY, city, country_id, is_europe, REVNAME, REVDATE, REVFIRST, last_visit, REVCREATOR ) VALUES ( 0, 'Berlin', 83, true, 'create-script', CURRENT DATE, CURRENT DATE, CURRENT DATE, 'create-script' )";

上面的语句抛出一个错误SQL0107N(名字太长),这似乎与字符串分隔符(")我曾经接受过语句

1 个答案:

答案 0 :(得分:1)

在执行立即替换的第二个语句中,在开头和结尾使用引号引用双引号,并使用两个单引号替换语句中的引号。

EXECUTE IMMEDIATE 'INSERT INTO fk_city (ID_CITY, city, country_id, is_europe, REVNAME, REVDATE, REVFIRST, last_visit, REVCREATOR ) VALUES ( 0, ''Berlin'', 83, ''true'', ''create-script'', CURRENT DATE, CURRENT DATE, CURRENT DATE, ''create-script'' )';

我不确定你在CURRENT DATE的意思,也许它应该是像sysdate这样的日期时间函数。