简体   繁体   English

如何在 Informix 的 MERGE 语句中使用源参数?

[英]How do I use parameters for the source in a MERGE statement in Informix?

I am trying to execute a merge statement against an Informix database as follows:我正在尝试对 Informix 数据库执行合并语句,如下所示:

MERGE INTO aa_rec AS dest
USING (SELECT '123456' AS id, '111-222-3333' as phone, '' as phone_ext, 'CELL' as aa FROM sysmaster:'informix'.sysdual) AS src
ON dest.id = src.id AND dest.aa = src.aa
WHEN NOT MATCHED THEN 
    INSERT (dest.id, dest.aa, dest.beg_date, dest.phone, dest.phone_ext, dest.ofc_add_by)
    VALUES (src.id, src.aa, TODAY, src.phone, src.phone_ext, 'TEST')
WHEN MATCHED THEN UPDATE SET 
        dest.phone = src.phone, 
        dest.phone_ext = src.phone_ext, 
        dest.beg_date = '10/29/2019', 
        dest.ofc_add_by = 'TEST'

This statement works as is, with hard-coded values, but I would like to pass parameters for the values in the source table:此语句按原样使用硬编码值,但我想为源表中的值传递参数:

USING (SELECT ? AS id, ? as phone, ? as phone_ext, 'CELL' as aa FROM sysmaster:'informix'.sysdual) AS src

When I execute the statement with parameters and valid values, I receive this error:当我使用参数和有效值执行语句时,我收到此错误:

E42000: (-201) A syntax error has occurred. E42000: (-201) 出现语法错误。

Are parameters supported in the source part of the MERGE statement? MERGE 语句的源代码部分是否支持参数? If they are, where is the error in my syntax?如果是,我的语法错误在哪里?

For context, I'm calling this from ASP.NET using the OleDb provider for Informix.对于上下文,我使用 Informix 的 OleDb 提供程序从 ASP.NET 调用它。

You have:你有:

SELECT ? AS id, ? as phone, ? as phone_ext, 'CELL' as aa FROM sysmaster:'informix'.sysdual

You can't use placeholders ( ? symbols) for 'structural' elements of a SELECT statement.您不能对 SELECT 语句的“结构”元素使用占位符( ?符号)。 You can't provide column names in the placeholders.您不能在占位符中提供列名。 And passing numbers etc as values via placeholders in the select-list doesn't work either.并且通过选择列表中的占位符将数字等作为值传递也不起作用。

I'd probably create a temp table of the appropriate shape, and insert a row into that, and then use the temp table in the select statement:我可能会创建一个适当形状的临时表,并在其中插入一行,然后在 select 语句中使用临时表:

SELECT '123456' AS id, '111-222-3333' AS phone, '' AS phone_ext, 'CELL' AS aa
  FROM sysmaster:'informix'.sysdual
  INTO TEMP phone_data;

MERGE INTO aa_rec AS dest
USING (SELECT * FROM phone_data) AS src
   ON dest.id = src.id AND dest.aa = src.aa
 WHEN NOT MATCHED THEN 
      INSERT (dest.id, dest.aa, dest.beg_date, dest.phone, dest.phone_ext, dest.ofc_add_by)
          VALUES (src.id, src.aa, TODAY, src.phone, src.phone_ext, 'TEST')
 WHEN MATCHED THEN UPDATE SET 
          dest.phone = src.phone, 
          dest.phone_ext = src.phone_ext, 
          dest.beg_date = '10/29/2019', 
          dest.ofc_add_by = 'TEST'
;

DROP TABLE phone_data;

It might be better/safer to create the temp table explicitly rather than to use the INTO TEMP clause.显式创建临时表可能比使用 INTO TEMP 子句更好/更安全。 The types are not necessarily what you'd expect (CHAR(6), CHAR(12), VARCHAR(1), CHAR(4)) — though that may not matter.类型不一定是您所期望的(CHAR(6)、CHAR(12)、VARCHAR(1)、CHAR(4))——尽管这可能无关紧要。

Clearly, once the temp table exists, you can insert whatever data is appropriate into the temp table using any mechanism that's available:显然,一旦临时表存在,您就可以使用任何可用的机制将任何适合的数据插入到临时表中:

INSERT INTO phone_data(id, phone, phone_ext, aa) VALUES(?, ?, ?, ?)

Remember that temp tables are private to a session — you can have lots of people all using the same temporary table name at the same time without interfering with each other.请记住,临时表是会话专用的——您可以让很多人同时使用相同的临时表名称,而不会相互干扰。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM