tags:

views:

859

answers:

3

I have a table in informix (Version 11.50.UC4) called NextRecordID with just one column called id and it will have one row. What I want to do is copy this value into another table. But don't want my query to fail if this table does not exist. Something like

if table NextRecordID exists
    then insert into sometable values ('NextRecordID', (select id from NextRecordID))
    else insert into sometable values ('NextRecordID', 1)
A: 

You should be able to do this by checking the systables table.

Adam Hughes
Thanks Adam Hughes. That was easy to find using google :-) I was trying to find out if there was a way to use that information and create an if else kind of construct in plain sql statement. I guess this was not very clear in my question.
sriharsha
A: 

Thank you for including server version information - it makes answering your question easier.

You've not indicated which language(s) you are using.

Normally, though, you design a program to expect a certain schema (certain tables to be present), and then fail - preferably under control - if those tables are not present. Also, it is not clear whether you would get into problems because of repeated execution of the second INSERT statement. Nor is it clear when the NextRecordID table is updated - presumably, once the value has been used, it must be updated.

You should look at SERIAL (BIGSERIAL) and see whether that is appropriate for you.

You should also look at whether a SEQUENCE would be appropriate to use here - it certainly looks rather like it might be applicable.

As Adam Hughes points out, if you want to check whether the NextRecordID table is present in the database, you would look in the systables table. Be aware, though, that your search will need to be against an all lower-case name (nextrecordid).

Also, MODE ANSI databases complicate life - you have to worry about the table's owner (because there could be multiple tables called nextrecordid in a MODE ANSI database). Most likely, you don't have to worry about that - any more than you are likely to have to worry about delimited identifiers for table "someone"."NextRecordID" (which is a different table from someone.NextRecordID).

Jonathan Leffler
Having worked with informix a bit I know how important server version is :-) Language, I was thinking of a simple sql statement to do that job so I did not say about any specific language. Thanks for the detailed reply and also for the insight into some corners I had not thought of. In my case the problem is simple as the database is not open and it used only by our application so there is only one nextrecordid. I can use serial and sequence but I like the control and simplicity of using tables also some legacy there. I am thinking of using systables and case statement to resolve this.
sriharsha
The reason the language is relevant is that plain SQL does not support an IF statement per se. It is available in SPL (stored procedure language), or in I4GL or in the various client APIs (ODBC, JDBC, ESQL/C, etc.).
Jonathan Leffler
A: 

I ended up using the below SQL query. Its not ANSI SQL but works the informix server I am using.

insert into sometable values ('NextRecordID', 
    select case (select 1 from systables where tabname='nextrecordid')
    when 1 then (select nextid from nextrecordid) 
    else (select 1 from systables where tabname='systables') end 
    from systables where tabname='systables');

What is happening here is within insert query I get the value to be inserted by using select query. Now that select query is interesting. It uses case statement of Informix. I have written a select query to check if the table nextrecordid exists in systables and return 1 if it exists. If this query returns 1, I query the table nextrecordid for the value or else I wrote a query to return the default value 1. This work for me.

sriharsha