What is talxy?
stored function, multiple queries, best practices
Hello Guest
  
  • Login
• Register…
• Start blog
  • Who, Where, When
• What can I do?
• What to Read?
  • Polls
• Avatars
• Interests
  • Cities and Countries
• Random blog
• Users search
  • Search
• Games
• Tests
• QAIX
  • Сообщества
• Talxy Chat
• Horoscope
• Online
 
Зарегистрируйся!

QAIX > PostgreSQL database development > stored function, multiple queries, best practices 19 April 2006 00:19:16

  Recent blog posts: 
  They have birthday today: 
  Forums:   
  Discuss: 
  Recent forum topics: 
  Recent forum comments:
  Moderators:

stored function, multiple queries, best practices

Ash Grove 19 April 2006 00:19:16
 Hello,

I'd like to learn how other people approach this kind
of situation:

Say you want to do an insert on three tables and all
of your data is coming into the db from your
application. You do the first insert and a primary key
is generated. You grab that primary key and insert it
(along with some other data) into the two other
tables. Finally, you return the primary key to the
application. If something goes wrong, you want to
everything to rollback.

I currently do this by building a delimited string in
the application containing all data to be inserted and
then sending it, via callablestatement, to a stored
function. The function parses the string, does the
inserts and returns the primary key via a registered
out parameter.

Are there glaring errors with this approach? How would
you do this?

Thanks in advance,
Ash

___________________­____________________­___________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.c­om

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

Add comment
Oliver Jowett 14 April 2006 18:18:58 permanent link ]
 Ash Grove wrote:
I currently do this by building a delimited string in> the application containing all data to be inserted and> then sending it, via callablestatement, to a stored> function. The function parses the string, does the> inserts and returns the primary key via a registered> out parameter.

Why assemble a string and parse it? Why not just use multiple parameters
directly?

-O

-------------------­--------(end of broadcast)---------­------------------
TIP 5: don't forget to increase your free space map settings

Add comment
Ash Grove 14 April 2006 18:37:20 permanent link ]
 
Why assemble a string and parse it? Why not just use>multiple parameters >directly?>-O


So, Oliver, you're suggesting something like "call
myfunction('query1'­,'query2','query3')"­?
I currently do this by building a delimited string> in> the application containing all data to be inserted> and> then sending it, via callablestatement, to a> stored> function. The function parses the string, does the> inserts and returns the primary key via a> registered> out parameter.>
Why assemble a string and parse it? Why not just use
multiple parameters
directly?

-O


___________________­____________________­___________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.c­om

-------------------­--------(end of broadcast)---------­------------------
TIP 3: Have you checked our extensive FAQ?

http://www.postgres­ql.org/docs/faq

Add comment
Oliver Jowett 16 April 2006 05:07:34 permanent link ]
 Ash Grove wrote:>
Why assemble a string and parse it? Why not just use>>multiple parameters >>directly?>>-O>
So, Oliver, you're suggesting something like "call> myfunction('query1'­,'query2','query3')"­?

No, I'm suggesting:

call myfunction('data value 1', 42, 1.234, 'data value 4')

I am assuming you know the combinations of updates you will need to run
ahead of time. If you want to run arbitary queries then you are probably
stuck with doing some parsing in the function .. but that is pretty nasty.

-O

-------------------­--------(end of broadcast)---------­------------------
TIP 4: Have you searched our list archives?

http://archives.pos­tgresql.org

Add comment
Ash Grove 18 April 2006 22:53:51 permanent link ]
 

--- Oliver Jowett <oliver@opencloud.c­om> wrote:
Ash Grove wrote:> >
Why assemble a string and parse it? Why not just> use> >>multiple parameters > >>directly?> >>-O> >
So, Oliver, you're suggesting something like> "call> > myfunction('query1'­,'query2','query3')"­?>
No, I'm suggesting:>
call myfunction('data value 1', 42, 1.234, 'data> value 4')>
I am assuming you know the combinations of updates> you will need to run > ahead of time. If you want to run arbitary queries> then you are probably > stuck with doing some parsing in the function .. but> that is pretty nasty.>
-O>

I know the combination of queries, but whether a
parameter has a value is unknown. Also unknow is
whether all relevant tables will get updated. If I
simply send all parameters in a defined order, the
parameter list will be kinda long and many values will
be nulls.

I can only call one function (from the application)
per transaction, right?

I don't mind doing the parsing, I was just wondering
if I was considering all options.

___________________­____________________­___________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.c­om

-------------------­--------(end of broadcast)---------­------------------
TIP 4: Have you searched our list archives?

http://archives.pos­tgresql.org

Add comment
Guy Rouillier 19 April 2006 00:19:16 permanent link ]
 Ash Grove wrote:
I can only call one function (from the application)> per transaction, right?

Incorrect. Transactions are delimited by commits or rollbacks, not by
function calls or SQL statements (more correctly, DML.) If you want to
control your commit points, tell JDBC to "conn.setAutocommit­(false)".

--
Guy Rouillier


-------------------­--------(end of broadcast)---------­------------------
TIP 1: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to majordomo@postgresq­l.org so that your
message can get through to the mailing list cleanly

Add comment
 

Add new comment

As:
Login:  Password:  
 
 
  
 
Пожалуйста, относитесь к собеседникам уважительно, не используйте нецензурные слова, не злоупотребляйте заглавными буквами, не публикуйте рекламу и объявления о купле/продаже, а также материалы нарушающие сетевой этикет или УК РФ.


QAIX > PostgreSQL database development > stored function, multiple queries, best practices 19 April 2006 00:19:16

see also:
inserting works fine but I cant update.
Foreign Key on text field
reversion? Recursion question
пройди тесты:
Do you really know yourself?
see also:
Posting Lookup Values
hallo , j don t know what to write...
mahalo transaction manager

  Copyright © 2001—2008 QAIX
Idea: Miсhael Monashev
Помощь и задать вопросы можно в сообществе support.qaix.com.
Сообщения об ошибках оставляем в сообществе bugs.qaix.com.
Предложения и комментарии пишем в сообществе suggest.qaix.com.
Информация для родителей.
Write us at:
If you would like to report an abuse of our service, such as a spam message, please .