473,387 Members | 1,493 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

problem with inserting a generated seq no into not null field

I have the following generic query which I am trying to use to insert
into a 3 field table

INSERT INTO tb_files (subj, area, file_no) select '4', '8',(SELECT CASE
WHEN (4||8) NOT IN (SELECT (subj||area) FROM tb_files) THEN '1' ELSE
max(file_no)+1 END FROM tb_files GROUP BY (subj||area),subj,area HAVING
subj = '4' AND area = '8');

However the first entry will not go in with a

'null value in column "file_no" violates not-null constraint'

message

Anyone any ideas on this - the CASE statement does not seem to be being
evaluated

---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match

Nov 23 '05 #1
4 1313
On Tue, 08 Jun 2004 12:37:48 +0100, mike <mi**@bristolreccc.co.uk>
wrote:
INSERT INTO tb_files (subj, area, file_no) select '4', '8',(SELECT CASE
WHEN (4||8) NOT IN (SELECT (subj||area) FROM tb_files) THEN '1' ELSE
max(file_no)+1 END FROM tb_files GROUP BY (subj||area),subj,area HAVING
subj = '4' AND area = '8');


If there is no row with subj = '4' AND area = '8', then SELECT ...
HAVING returns no rows and therefore max(file_no) is NULL. And NULL+1
is NULL. Try

INSERT INTO tb_files (subj, area, file_no)
SELECT '4', '8', coalesce(max(file_no), 0)+1
FROM tb_files
WHERE subj = '4' AND area = '8';

Servus
Manfred

---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to ma*******@postgresql.org

Nov 23 '05 #2
On Tue, 08 Jun 2004 12:37:48 +0100, mike <mi**@bristolreccc.co.uk>
wrote:
INSERT INTO tb_files (subj, area, file_no) select '4', '8',(SELECT CASE
WHEN (4||8) NOT IN (SELECT (subj||area) FROM tb_files) THEN '1' ELSE
max(file_no)+1 END FROM tb_files GROUP BY (subj||area),subj,area HAVING
subj = '4' AND area = '8');


If there is no row with subj = '4' AND area = '8', then SELECT ...
HAVING returns no rows and therefore max(file_no) is NULL. And NULL+1
is NULL. Try

INSERT INTO tb_files (subj, area, file_no)
SELECT '4', '8', coalesce(max(file_no), 0)+1
FROM tb_files
WHERE subj = '4' AND area = '8';

Servus
Manfred

---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to ma*******@postgresql.org

Nov 23 '05 #3
I have the following generic query which I am trying to use to insert
into a 3 field table

INSERT INTO tb_files (subj, area, file_no) select '4', '8',(SELECT CASE
WHEN (4||8) NOT IN (SELECT (subj||area) FROM tb_files) THEN '1' ELSE


That's not really a good way to detect the existance of a row with those
values. If either subj or area is allowed to be NULL, wierdness occurs
(*). Even if neither is allowed to be null, if the values can ever have
more than one digit wierdness occurs.

(*) - IIRC, even if no row exists with (4,8) as (subj,area), if there's a
row where subj || area is NULL, you will never get true from the NOT IN
clause and the second alternative will always be chosen. NULLs and IN
don't play very well together.
---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings

Nov 23 '05 #4
I have the following generic query which I am trying to use to insert
into a 3 field table

INSERT INTO tb_files (subj, area, file_no) select '4', '8',(SELECT CASE
WHEN (4||8) NOT IN (SELECT (subj||area) FROM tb_files) THEN '1' ELSE


That's not really a good way to detect the existance of a row with those
values. If either subj or area is allowed to be NULL, wierdness occurs
(*). Even if neither is allowed to be null, if the values can ever have
more than one digit wierdness occurs.

(*) - IIRC, even if no row exists with (4,8) as (subj,area), if there's a
row where subj || area is NULL, you will never get true from the NOT IN
clause and the second alternative will always be chosen. NULLs and IN
don't play very well together.
---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings

Nov 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: thoidi | last post by:
Hi Guru, This is the work that inserting a new request into the tables tbl_request and tbl_worktime. The problem is it produced two records to each table. After this asp executed, it generated...
0
by: john | last post by:
Hi,All Gurus: It is kind of complicated, please bear with me and let me know if you have any questions. Thanks a lot in advance. John I have a csharp method, using emit to dynamically generate...
0
by: mike | last post by:
I have the following generic query which I am trying to use to insert into a 3 field table INSERT INTO tb_files (subj, area, file_no) select '4', '8',(SELECT CASE WHEN (4||8) NOT IN (SELECT...
2
by: ajikoe | last post by:
Hi, I tried to follow the example in swig homepage. I found error which I don't understand. I use bcc32, I already include directory where my python.h exist in bcc32.cfg. /* File : example.c...
7
by: Dabbler | last post by:
I'm using an ObjectDataSource with a stored procedure and am getting the following error when trying to update (ExecuteNonQuery): System.Data.SqlClient.SqlException: Procedure or Function...
0
by: russganz | last post by:
It seems to me there are real problems with the datagridview (along with the detailsview and form view) control when using optimistic concurrency with fields that can be null. Trying to use these...
11
by: c676228 | last post by:
Hi everyone, I am just wodering in asp program, if there is anybody writing store procedure for inserting data into database since there are so many parameters need to be passed into store...
3
by: Robert Johnson | last post by:
Hi all. Created a simple table in my db. 3 colums one is a Int set for autoincrement. Itentity True, seed 1, Incremement 1, null False. The other colums are simple VarChar(50) null false on the...
0
by: Edwin.Madari | last post by:
-----Original Message----- statement prepared first and executed many times with exectemany - db API http://www.python.org/dev/peps/pep-0249/ inline statemets can be exeucuted only. hope that...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.