Aug 25, 2010 2:32 PM
Switch between databases within a Stored Procedure
-
Like (0)
Hi all,
I would like to know how to switch between Databases within a Stored Procedure for DML Operations?
Thanks in advance.
You can't do it. You have to completely disconnect from the database and then reconnect to the other database.
Thanks for your reply.
I think you need to split up your procedure in to two based on your requirement, say if you procedure has to switch between two databases, then you need to split your procedure into to two, and add the respective procedure part in one sql file and add the other procedure part in another sql and call them sequentially. I have done this to avoid cross database access.
but i guess in that case also you need to disconnect from that database to run individual procedures you are creating on differnt databases. i mean i didnt get "call them sequentially", can you please elaborate it ?
i went through one document of netezza, where it was mentioned that
"Certain types of operations will not work across databases, such as insert, update,
and delete operations. For any procedure which contains those types of operations, make
sure that you are connected to the database where the stored procedure is defined."
and i have tried to do these operations without using stored procedures within the same server with differnt database.
e.g.
if i am in database DB1 then (creating table TEST in DB1 using DB2 table TEST2)
CREATE TABLE TEST
AS SELECT * FROM DB2..TEST2;
How about using SYNONYMs in the 'current' database to point to stuff in other databases, to save having to code the" OTHERSCHEMA..TABLENAME" syntax everytime?
I've used this approach to work around similar issues when connecting via a 3rd party client app.
D.

