1

我在 sql 语句上苦苦挣扎,找不到正确的语法。

我正在使用 teamdeveloper 6.1 进行编程,我正在使用该功能SqlPrepareAndExecute(...)

我正在尝试的是插入一组值。在 mysql 上会是这样的:

INSERT INTO supportContacts
    (type, details)
VALUES
    ('Email', 'admin@sqlfiddle.com'),
    ('Twitter', '@sqlfiddle');

使用 SqlTalk 我可以做到这一点......

INSERT INTO SYSADM.FOOBAR VALUES(:1,:2,:3,:4,:5,:6,:7,:8,:9,:10,:11,:12,:13)
\
$datatypes CHARACTER,NUMERIC,NUMERIC,NUMERIC,NUMERIC,NUMERIC,NUMERIC,NUMERIC,NUMERIC,NUMERIC,NUMERIC,NUMERIC,NUMERIC
"01",0,0,0,0,0,0,0,0,0,0,0,0
"02",0,0,0,0,0,0,0,0,0,0,0,0
"03",0,0,0,0,0,0,0,0,0,0,0,0
"04",0,0,0,0,0,0,0,0,0,0,0,0
"05",0,0,0,0,0,0,0,0,0,0,0,0
"06",0,0,0,0,0,0,0,0,0,0,0,0
"07",0,0,0,0,0,0,0,0,0,0,0,0
"08",0,0,0,0,0,0,0,0,0,0,0,0
/

当我填充变量并在 TD 6.1 的 SqlExecute 函数中使用它时,Bot 都不起作用

我总是得到类似Statement not ended properlyor的错误Invalid constant

我还尝试将这些值设置为:

('01',0,0,0,0,0,0,0,0,0,0,0,0),
('02',0,0,0,0,0,0,0,0,0,0,0,0),
('03',0,0,0,0,0,0,0,0,0,0,0,0), ...

错误...

我究竟做错了什么 ?

编辑(到目前为止我尝试过的事情):


试试 1

这都在这里


试试 2

"INSERT INTO SYSADM.FOOBAR VALUES(:1,:2,:3,:4,:5,:6,:7,:8,:9,:10,:11,:12,:13)
\
$datatypes CHARACTER,NUMERIC,NUMERIC,NUMERIC,NUMERIC,NUMERIC,NUMERIC,NUMERIC,NUMERIC,NUMERIC,NUMERIC,NUMERIC,NUMERIC
'01',0,0,0,0,0,0,0,0,0,0,0,0
'02',0,0,0,0,0,0,0,0,0,0,0,0"

只说什么SQL command not properly ended


尝试 3

我想可能是因为\. 实际上它是一个 escapechar 所以我逃脱了它

"INSERT INTO SYSADM.FOOBAR VALUES(:1,:2,:3,:4,:5,:6,:7,:8,:9,:10,:11,:12,:13)
\\
$datatypes....

现在它说无效字符


尝试 4

"INSERT INTO SYSADM.FOOBAR VALUES(:1,:2,:3,:4,:5,:6,:7,:8,:9,:10,:11,:12,:13)
\
$da...
\'01\',0,0,0,0,0,0,0,0,0,0,0,0
\'02\',0,0,0,0,0,0,0,0,0,0,0,0"

也失败了!:)

4

3 回答 3

0

这可能只是由于示例查询,但我不确定您是否知道数据绑定在 TD 中的工作原理:

INSERT INTO SYSADM.FOOBAR VALUES(:var1,:var2,:var3)

其中 var1、var2、var3 是调用 SqlExecute/SqlPrepareAndExecute 的函数中可见的变量

于 2014-12-30T21:30:11.530 回答
0

一些代码来说明一般语法。
注意事项:

  1. 这是通过 CTD 2.1 完成的。
  2. 我省略了连接数据库的所有代码,只是假设 h_SqlMain 作为有效的连接句柄。
  3. 数据来自一个数组。在现实生活中,您会例如读取 CSV 文件。
  4. 变量myTypemyDetail在 SqlPrepare-Statment 中用作绑定变量,因为 CTD 2.1 不支持直接使用数组变量作为绑定。

“局部变量”-部分:

String: myType
String: myDetail
String: types[*]
String: details[*]
Number: i
Sql Handle: h_SqlMain

“行动”部分:

Set types[0] = 'Email'
Set details[0] = 'admin@sqlfiddle.com'
Set types[1] = 'Twitter'
Set details[1] = '@sqlfiddle'
Set types[2] = 'Foo'
Set details[2] = 'Bar'
Call SqlPrepare( h_SqlMain, 
   "INSERT INTO supportContacts (type, details) 
    VALUES(:myType, :myDetail)")
Set i=0
While i <= 2 
    Set myType = types[i]
    Set myDetail = details[i]
    Call SqlExecute( h_SqlMain )
    Set i=i+1
于 2015-01-02T10:58:59.623 回答
0

您的源数据在数据库表中吗?然后您可以使用 INSERT SELECT 语句。

像这样:

INSERT INTO SYSADM.FOOBAR (COL1, COL2, COL3) SELECT COL1, COL2, COL3 FROM SOURCE_DATA

编辑:

或者您可以使用:

Call SqlPrepareAndExecute( hSql, '
INSERT INTO FOOBAR VALUES(:1,:2,:3,:4,:5,:6,:7,:8,:9,:10,:11,:12,:13)
select \'01\',0,0,0,0,0,0,0,0,0,0,0,0 from dual union all
select \'02\',0,0,0,0,0,0,0,0,0,0,0,0 from dual union all
select \'03\',0,0,0,0,0,0,0,0,0,0,0,0 from dual union all
select \'04\',0,0,0,0,0,0,0,0,0,0,0,0 from dual union all
select \'05\',0,0,0,0,0,0,0,0,0,0,0,0 from dual union all
select \'06\',0,0,0,0,0,0,0,0,0,0,0,0 from dual union all
select \'07\',0,0,0,0,0,0,0,0,0,0,0,0 from dual union all
select \'08\',0,0,0,0,0,0,0,0,0,0,0,0 from dual ' )
于 2014-12-31T10:10:58.843 回答