English 中文(简体)
Informix脚本中的本地变量
原标题:Local variables in an Informix script
  • 时间:2011-05-23 13:40:20
  •  标签:
  • informix

I have to do a big update script - not an SPL (stored procedure). It s to be written for an Informix db.

它涉及将行插入到多个表中,每个表都依赖于插入到上一个表中的序列。

我知道我可以通过以下操作访问序列:

SELECT DISTINCT dbinfo( sqlca.sqlerrd1 ) FROM systables

但在插入下一个表之前,我似乎无法定义一个本地变量来存储它。

我想这样做:

insert into table1 (serial, data1, data2) values (0,  newdata1 ,  newdata2 );
define serial1 as int;
let serial1 = SELECT DISTINCT dbinfo( sqlca.sqlerrd1 ) FROM systables;
insert into table2 (serial, data1, data2) values (0, serial1,  newdata3 );

当然,Informix在定义线上遇到了瓶颈。

有没有一种方法可以做到这一点,而不必将其创建为存储过程,运行一次,然后删除该过程?

最佳回答

如果所涉及的表中的列数与您的示例一样少,那么您可以将SPL永久化,并使用它来插入数据,即:

<code>EXECUTE PROCEDURE insert_related_tables(newdata1、newdata2、newdata3)

显然,这并不能很好地扩展,但对于您的示例来说是可以的。

另一个扩展了Jonathan的例子并解决了使用MAX()可能产生的任何并发问题的想法是在Table3中包含DBINFO(sessionid)

DELETE FROM Table3 WHERE sessionid = DBINFO( sessionid );
INSERT INTO Table1 (...);
INSERT INTO Table3 (sessionid, value)
  VALUES (DBINFO( sessionid ), DBINFO( sqlca.sqlerrd1 ));
INSERT INTO Table2 
  VALUES (0, (SELECT value FROM Table3
              WHERE sessionid = DBINFO( sessionid ),  newdata3 );
...

您也可以将Table3制作为TEMP表:

INSERT INTO Table1 (...);
SELECT DISTINCT DBINFO( sqlca.sqlerrd1 ) AS serial_value
  FROM some_dummy_table_like_systables
INTO TEMP Table3 WITH NO LOG;
INSERT INTO Table2 (...);
问题回答

Informix不为所需类型的本地变量提供存储过程之外的机制。但是,在您提供的有限示例中,这是可行的:

CREATE TABLE Table1
(
    serial SERIAL(123) NOT NULL,
    data1  VARCHAR(32) NOT NULL,
    data2  VARCHAR(32) NOT NULL
);
CREATE TABLE Table2
(
    serial SERIAL      NOT NULL,
    data1  INTEGER     NOT NULL,
    data2  VARCHAR(32) NOT NULL
);

INSERT INTO Table1(Serial, Data1, Data2)
    VALUES(0,  newdata1 ,  newdata2 );
INSERT INTO Table2(Serial, Data1, Data2)
    VALUES(0, DBINFO( sqlca.sqlerrd1 ),  newdata3 );

SELECT * FROM Table1;

123   newdata1     newdata2

SELECT * FROM Table2;

1     123          newdata3

然而,这只是因为您需要在表2中插入一行。如果你需要插入更多,这种技术不会很好地工作。我想,您可以使用:

CREATE TEMP TABLE Table3
(
    value   INTEGER NOT NULL
);

INSERT INTO Table1(Serial, Data1, Data2)
    VALUES(0,  newdata1 ,  newdata2 );
INSERT INTO Table3(Value)
    VALUES(DBINFO( sqlca.sqlerrd1 ));
INSERT INTO Table2(Serial, Data1, Data2)
    VALUES(0, (SELECT MAX(value) FROM Table3),  newdata3 );
INSERT INTO Table2(Serial, Data1, Data2)
    VALUES(0, (SELECT MAX(value) FROM Table3),  newdata4 );

等等…Table3的临时表避免了并发和MAX()的问题。





相关问题
Bulk import from Informix into Oracle

We need to pull some tables from an Informix SE database, truncate tables on Oracle 10g, and then populate them with the Informix data. Does a bulk import work? Will data types clash? I d like to ...

Informix with NHibernate

I am trying to get Informix working with NHibernate on windows 7. I have a connection string that works fine with informix now, it is this, Database=db;Server=server:port;uid=username;password=...

update data from one table to another (in a database)

DB gurus, I am hoping someone can set set me on the right direction. I have two tables. Table A and Table B. When the system comes up, all entries from Table A are massaged and copied over to Table ...

SQL query to get the "latest" value for each location

What I thought would be a simple thing to solve has now bugged for quite some time. Now I need help from you guys. In Informix I have a table "temperatures" like this: locId dtg ...

Tools for Informix [closed]

Are there any tools available for Informix, similar to Query Analyzer for SQL Server?

Informix: Fifo valuation with SQL/Stored procedures

I am using IDS 10 and I have a simple transaction table with the inventory changes with product ID, transaction time, volume, quantity and price. Is it possible to determine the FIFO valuation solely ...

Informix ODBC Connection Help

I have a development project that requires us to be able to support informix data sources via ODBC. I ve downloaded the prebuilt Informix Virtual Appliance from the IBM website and am able see the ...

热门标签