-
January 19th, 2006, 09:55 AM
#1
Informix - INSERT INTO issue
So here's the problem - I have to insert a record into a table in informix. The statement looks something like this (executed from the client side code):
Code:
INSERT INTO MYTABLE
(column_a, column_b, column_c, version, column_e)
VALUES
(value_a, value_b, value_c, value_version, value_e)
Now this looks simple.
There is a primary key set on the table which consists of columns a, b, c and version & a unique index set on columns a, b, c and e.
Now, whenever I insert records I need to see if there already exists a record with column values for columns a, b, and c as same as what I am inserting (and whatever e - same e value in such situation would not be accepted - this would be ensured by the unique index) and if there are any then increase the value of the version column by 1 and go ahead with the insert.
What do you suggest? I am open to using triggers as well. And to just say it again - its Informix I am using. Thanks for having a look. Regards.
Last edited by exterminator; January 19th, 2006 at 10:17 AM.
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
-
January 19th, 2006, 09:06 PM
#2
Re: Informix - INSERT INTO issue
Code:
IF NOT EXISTS(SELECT * FROM mytable WHERE column_a = @a, column_b = @b, column_c = @c)
BEGIN
INSERT ...
VALUES ...
ELSE
SELECT @version = version_column + 1 FROM mytable WHERE column_a = @a, column_b = @b, column_c = @c
INSERT ...
VALUES(@a, @b, @c, @version)
END
IF @@ERROR <> 0
RETURN -1
ELSE
RETURN 0
PS : not sure in Informix..it's for MSSQL
@ = Query parameter or in this case SPROC's parameter
for trigger, i'm not sure if it can be done using it since trigger can't accept parameter like SPROC, well at least in MSSQL
-
January 20th, 2006, 02:49 PM
#3
Re: Informix - INSERT INTO issue
Thanks, erickwidya, for your reply!
That is a solution but I was to fire the query from the client side and did not want to use a stored procedure or a trigger (although I was open to using them if no single query could do that job) and this is what I thought of:
Code:
INSERT INTO mytable
(column_a, column_b, column_c, version, column_e)
VALUES
(value_a, value_b, value_c,
isnull((SELECT MAX(version)+1 from mytable
WHERE
column_a=value_a AND
column_b=value_b AND
column_c = value_c), 1), value_e)
This seems to be the one for me. But the problem now is that Informix doesn't have the isnull function as does MS SQL server and Sybase (and probably even Oracle). So I thought of writing a UDF for this but I am stuck with silly syntax errors. Is there any other way out of this isnull thing? This is important in the cases when the table would be empty and select MAX returns null. Am I missing something really simple out here? Thanks again for reply. Regards.
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
-
January 20th, 2006, 04:06 PM
#4
Re: Informix - INSERT INTO issue
Got it... there is a function NVL that does exactly what I wanted. The thread is resolved but you are welcome to pour in anything interesting on the topic. regards.
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
-
January 20th, 2006, 09:12 PM
#5
Re: Informix - INSERT INTO issue
This is important in the cases when the table would be empty and select MAX returns null
why don't u DECLARE @cmd that check for EXISTence of the record first..if there any then set the @cmd = usual INSERT but if there isn't set @cmd = INSERT with SELECT MAX(version)+1 and after that execute that @cmd?
That is a solution but I was to fire the query from the client side and did not want to use a stored procedure or a trigger
why? from what i read using SPROC is more faster than using Client Side code also it encapsulate the code(SELECT, ACTION query) that needed into Server side (more less code to maintain i think)
..there is a function NVL that does exactly what I wanted.
so it act like ISNULL in MSSQL?
PS: again it just something i thought in MSSQL, glad u already solved this issue
-
January 21st, 2006, 01:18 AM
#6
Re: Informix - INSERT INTO issue
Originally Posted by erickwidya
why? from what i read using SPROC is more faster than using Client Side code also it encapsulate the code(SELECT, ACTION query) that needed into Server side (more less code to maintain i think)
Very true. I will not say anything on this . Actually, thats the current project architecture or design or coding guideline whatever you may call it, which is wierd. But yeah, I would have had made an exception if it was really impossible on clientside.
Originally Posted by erickwidya
so it act like ISNULL in MSSQL?
Yes. Has an expression as the first argument and a replacement value, the second, that is returned in case the expression evaluates to null else returns the non-null result of the expression. Same as isnull of SQL Server.
Originally Posted by erickwidya
PS: again it just something i thought in MSSQL, glad u already solved this issue
Well, thanks a lot for your participation. Cheers, regards.
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|