如何导致UPDATE查询抛出错误?

时间:2016-04-08 05:08:06

标签: database postgresql

在我的数据库中,我有下表

       Table "public.session"
  Column   |     Type      | Modifiers
-----------+---------------+-----------
 sessionid | character(10) | not null (primary key)
 device    | character(5)  |
 userid    | character(5)  | (foreign key, can be null)

我想要做的是偶尔更新此表中的一行,以将userid从NULL更改为值。但是,如果userid ISNT null我想抛出一个错误,所以我可以在我的后端服务器中捕获它。目前,我的查询如下:

update session set userid = $1 where sessionID = $2 and userid = NULL;

问题是当userid为NULL时,此查询不会失败。我怎么能让它失败?

1 个答案:

答案 0 :(得分:0)

根据@Mark Watson的评论,我只是检查了更新的行数。这不是我想要的,但它肯定比编写触发器更容易。

相关问题