views:

1276

answers:

4

Using Informix, I've created a tempory table which I am trying to populate from a select statement. After this, I want to do an update, to populate more fields in the tempory table.

So I'm doing something like;

create temp table _results (group_ser int, item_ser int, restype char(4));

insert into _results (group_ser, item_ser)
select 
      group_ser, item_ser, null
from
      sometable

But you can't select null.

For example;

select first 1 current from systables

works but

select first 1 null from systables

fails!

(Don't get me started on why I can't just do a SQL Server like "select current" with no table specified!)

+2  A: 

This page says the reason you can't do that is because "NULL" doesn't have a type. So, the workaround is to create a sproc that simply returns NULL in the type you want.

That sounds like a pretty bad solution to me though. Maybe you could create a variable in your script, set it to null, then select that variable instead? Something like this:

DEFINE dummy INT;
LET dummy = NULL;

SELECT group_ser, item_ser, dummy
FROM sometable
Eric Petroelje
+1 +Answer. Haven't tried it, but your answer make perfect sense.
Dead account
Note that the FAQ in question is dated 1998. Some things have changed since then.
Jonathan Leffler
+3  A: 

You don't have to write a stored procedure; you simply have to tell IDS what type the NULL is. Assuming you are not using IDS 7.31 (which does not support any cast notation), you can write:

SELECT NULL::INTEGER FROM dual;

SELECT CAST(NULL AS INTEGER) FROM dual;

And, if you don't have dual as a table (you probably don't), you can do one of a few things:

CREATE SYNONYM dual FOR sysmaster:"informix".sysdual;

The 'sysdual' table was added relatively recently (IDS 11.10, IIRC), so if you are using an older version, it won't exist. The following works with any version of IDS - it's what I use.

-- @(#)$Id: dual.sql,v 2.1 2004/11/01 18:16:32 jleffler Exp $
-- Create table DUAL - structurally equivalent to Oracle's similarly named table.
-- It contains one row of data.

CREATE TABLE dual
(
    dummy CHAR(1) DEFAULT 'x' NOT NULL CHECK (dummy = 'x') PRIMARY KEY
) EXTENT SIZE 8 NEXT SIZE 8;
INSERT INTO dual VALUES('x');

REVOKE ALL ON dual FROM PUBLIC;
GRANT SELECT ON dual TO PUBLIC;

Idiomatically, if you are going to SELECT from Systables to get a single row, you should include 'WHERE tabid = 1'; this is the entry for Systables itself, and if it is missing, the fact that your SELECT statement does return any data is the least of your troubles. (I've never seen that as an error, though.)

Jonathan Leffler
+1  A: 

Is there any reason to go for an actual table? I have been using

select blah from table(set{1})
calvinkrishy
A: 
SELECT group_ser, item_ser, replace(null,null) as my_null_column
FROM sometable

or you can use nvl(null,null) to return a null for your select statement.

unrulylogic