tags:

views:

481

answers:

2

I have a requirement in a SQL environment that under specific circumstances, all references to table (or view) A in a procedure actually use table (or view) B. On the iSeries I would have used the OVRDBF command to override references to table A with table B: OVRDBF FILE(A) TOFILE(B). What would be the equivalent to this in SQL? Is there one?

My goal is to end up with a procedure that is ignorant of the override. I don't want conditional logic inside the procedure that directs processing at table B when certain conditions are met. The vision:

Under typical circumstances: Just invoke the procedure

Under specific alternative circumstances: Perform the OVRDBF equivalent and then Invoke the procedure

+1  A: 

Not sure which SQL environment support which options:

I believe DB2 has a CREATE ALIAS statement. Write the SQL over the alias.

Another possibility: run your queries over views: where you would do the OVRDBF, drop the view and rebuild it over the desired table.

The only thing I don't like about this answer is that both options involve temporarily overriding the references for everyone. Assuming you would want other processes to operate on the 'normal' objects, perhaps involving an additional schema or something like that would be appropriate?
JBell6
A: 

As Ed mentions if you can modify your procedure:

1) Create an alias for file(A)

CREATE ALIAS XYZ FOR A

2) Modify the procedure to reference XYZ instead of A.

3) When running the procedure to use file B execute

DROP ALIAS XYZ;
CREATE ALIAS XYZ FOR B;
CALL PROCEDURE;
DROP ALIAS XYZ;
CREATE ALIAS XYZ FOR A;

If you can't modify the procedure and you're not worried about simultaneous access to table A you could use:

RENAME TABLE A TO C;
CREATE ALIAS A FOR B;
CALL PROCEDURE;
DROP ALIAS A;
RENAME TABLE C TO A;
Paul Morgan
In my comment to Ed I expressed a concern about impacting all users and processes when using the alias. Doesn't everything end up pointing at whatever the alias is currently pointing to? Is there anyway to make the approach more specific to a single user and/or process?
JBell6