【发布时间】:2016-10-27 20:01:47
【问题描述】:
我只想在表存在时执行查询。
如果表不存在,什么也不做。
因此,我想检查表是否存在,然后检查表中是否有一些数据,如果有,请选择它们。
我想要这个/client.query( "SELECT * FROM mytable", function(err,res) {
所以,我尝试了类似的方法:
client.query("do"+
" $$"+
"begin"+
" if (select count(*) from information_schema.columns" +
" where table_schema = 'public' " +
" and table_name = 'mytable' )"+
" then "+
"DO NOTHING;"+
"else "+
"SELECT * FROM mytable;" +
"end if;"+
"end;"+
"$$"+
";", function(err, res) {
我不确定DO NOTHING 的使用,现在我收到error: syntax error at or near "NOTHING"
如果我使用 NOT:
client.query("do"+
" $$"+
"begin"+
" if NOT (select count(*) = 0 from information_schema.columns" +
" where table_schema = 'public' " +
" and table_name = 'mytable' )"+
" then "+
"SELECT * FROM mytable;" +
"end if;"+
"end;"+
"$$"+
";", function(err, res) {
当表格为空时它可以工作,但是当我填满表格时应该做select * from table它会抛出error: query has no destination for result data
【问题讨论】:
-
如果 mytable 中的数据存在,你想要什么?..
-
@VaoTsun:例如,当我单击(进入我的程序)字段
CheckMe时,它会显示 mytable 中的所有字段。当它已满时会发生这种情况。当表为空时,我我收到一个错误(因为它是空的)。所以,我想避免这种情况。 -
所以你想检查表是否存在然后检查表中是否有一些数据,如果有一些你想选择它?...全部在一个过程中 - 对吗?..跨度>
-
@VaoTsun:是的,就是这样!
-
然后更新您的问题,以便其他人了解您的需求
标签: node.js postgresql