informix 检查表是否存在然后读取值

发布于 2024-07-27 21:40:50 字数 325 浏览 5 评论 0原文

我在 informix(版本 11.50.UC4)中有一个名为 NextRecordID 的表,其中只有一列名为 id 的列,并且它将有一行。 我想要做的是将这个值复制到另一个表中。 但如果该表不存在,我不希望我的查询失败。 就像是

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

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

笑脸一如从前 2024-08-03 21:40:50

我最终使用了下面的 SQL 查询。 它不是 ANSI SQL,但适用于我正在使用的 informix 服务器。

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');

这里发生的事情是在 insert 查询中,我使用 select 查询获取要插入的值。 现在 select 查询很有趣。 它使用 Informix 的 case 语句。 我编写了一个 select 查询来检查 nextrecordid 表是否存在于 systables 中,如果存在则返回 1。 如果此查询返回 1,我会在表 nextrecordid 中查询该值,否则我会编写一个查询来返回默认值 1。这对我有用。

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.

夏至、离别 2024-08-03 21:40:50

您应该能够通过检查systables 表来完成此操作。

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

把梦留给海 2024-08-03 21:40:50

感谢您提供服务器版本信息 - 它使回答您的问题变得更加容易。

您尚未表明您正在使用哪种语言。

不过,通常情况下,您设计的程序会期望某个模式(存在某些表),然后如果这些表不存在,则失败(最好是在受控制的情况下)。 此外,还不清楚是否会因为重复执行第二个 INSERT 语句而遇到问题。 也不清楚 NextRecordID 表何时更新 - 据推测,一旦使用该值,就必须更新它。

您应该查看 SERIAL (BIGSERIAL) 并看看它是否适合您。

您还应该看看 SEQUENCE 是否适合在这里使用 - 它看起来确实很适用。

正如 Adam Hughes 指出的那样,如果您想检查数据库中是否存在 NextRecordID 表,您可以查看 systables 表。 但请注意,您的搜索需要针对全小写的名称 (nextrecordid)。

此外,MODE ANSI 数据库使生活变得复杂 - 您必须担心表的所有者(因为 MODE ANSI 数据库中可能有多个名为 nextrecordid 的表)。 最有可能的是,您不必担心这一点 - 就像您可能不必担心表“someone”.“NextRecordID”(这是与 person.NextRecordID 不同的表)的分隔标识符一样。

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文