简体   繁体   English

如何修复 NEXTVAL 在插入查询中返回 null 但在 PostgreSQL 中单独执行时返回正确值的问题?

[英]How to fix NEXTVAL returning null in insert query but returns correct value when executed alone in PostgreSQL?

When I execute the following insert query:当我执行以下插入查询时:

insert into FEED_STAGING_GOOD_TRADE (JOBEXECUTIONID, STAGINGID, ROWNUMBER, system, traderef, mtmvaluation, mtmvaluationccy, prcmtmquality, prcdate) 
values (64, NEXTVAL('FEED_STAGING_TRADE_SEQ') ,2,'RMS','TRD3',1.11111111E8,'USD',100.0,'2011-12-09 +00'::timestamp)

I get the following output:我得到以下输出:

ERROR: null value in column "tradestagingid" violates not-null constraint
  Detail: Failing row contains (46, 64, null, 2, null, null, null, RMS, null, null, TRD3, null, null, null, null, null, null, null, null, null, null, null, null, 111111111, USD, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, 100, 2011-12-09 00:00:00, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null).

The third column which is supposed to be filled by the NEXTVAL function result is being sent null, which is violating a certain non-nullity constraint.应该由NEXTVAL函数结果填充的第三列被发送为空,这违反了某个非空约束。

However when I execute NEXTVAL independently like this:但是,当我像这样独立执行NEXTVAL时:

select NEXTVAL('FEED_STAGING_TRADE_SEQ');

The function returns a correct value as shown below in the screenshot:该函数返回正确的值,如下面的屏幕截图所示:

下一个

Your insert provides a value for the column stagingid , but not for the column tradestagingid (to which the error message refers)您的插入为列stagingid提供了一个值,但没有为列tradestagingid (错误消息所指的)提供一个值

You INSERT statement doesn't even list a column named tradestagingid so the default value for that column will be used.您的 INSERT 语句甚至没有列出名为tradestagingid的列,因此将使用该列的默认值。

As you seem to expect the tradestagingid to be populated with the sequence value, you need to change the column list of your INSERT and use tradestagingid instead of stagingid .由于您似乎希望用序列值填充tradestagingid ,因此您需要更改 INSERT 的列列表并使用tradestagingid而不是stagingid Or better: define it as an identity column, so it gets populated automatically.或者更好:将其定义为标识列,以便自动填充。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 NodeJS中的SQL promise返回空括号,但是在SQL工作台中执行相同的查询时,它返回正确的数据 - SQL promise in NodeJS returning empty brackets, but when same query is executed in SQL workbench, it returns the correct data 与单独执行时相比,内部联接相同的查询返回的结果更多 - Inner joined same query returns more result than when it's executed alone PostgreSQL在查询返回空的情况下连续返回NULL - PostgreSQL returning NULL as a row in case query returns empty 与其他case语句一起使用时,CASE表达式返回NULL,在单独时返回预期结果 - CASE Expression returning NULL when with other case statements, returns expected result when alone 当查询返回NULL时,SQL COALESCE和IS NULL不返回空格 - SQL COALESCE and IS NULL are not returning a space when when the query returns NULL SQL执行时返回NULL - SQL Returning NULL when executed 有没有办法避免在PostgreSQL中插入失败时调用nextval()? - is there a way to avoid calling nextval() if the insert fails in PostgreSQL? PostgreSQL:按顺序插入嵌套的select和NEXTVAL - Postgresql: insert with nested select and NEXTVAL in sequence select sql 返回正确的结果,但插入基于相同的 select 语句返回 Z6C3E226B4D48241 - select sql returning the correct result but insert based on the same select statement returns NULL 如何解决:无法将值NULL插入列,插入失败 - How to fix: Cannot insert the value NULL into column, insert failed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM