Link to home
Start Free TrialLog in
Avatar of edvinson
edvinsonFlag for United States of America

asked on

Function Needed

I need a function that will delete records from X number of tables with a common uniqud id.

For instance, I have 4 tables -  photos, items, stats, equipment

each of them has a field called equipment_id (which is the primary key).

Now, I want to have one function where I pass the id and it deletes all in a table with that id. Those table names are just a sample, in the function I need to be able to pass the names.

Please remember, the Number of tables I will pass to the function will vary.

Thanks!!
SOLUTION
Avatar of Harisha M G
Harisha M G
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I think mgh_mgharish's solution is very good, but if you insist on only passing the primary key to your function, it becomes more complicated, but doable.
I've written a similar function for a client (It is copyrighted code, so I can't give it to you unfortunately).
What I did was
A show tables query and use that result to loop through al the tables with a describe query. Use that result to check if primary key exists and perform the appropriate action on that table.

Hope this will help
Avatar of edvinson

ASKER

Very close to what I need, mgh_mgharish .

And thanks fot the tips Terref.

Here's how I would *like* to call the function:

deletefromtables("myuniqueidFieldNameHere",myUniqueValueHere,"photos","items","stats","equipment");
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mensuck
mensuck

The echo at the bottom is just so you could see the result, just remove it in the real function!\

ms!
sweet. thanks!

I already used mgh_mgharish's solution in our project ( by figuring out the answer myself ) but s/he never responded to my last post.

I will keep your solution also!

Mind if I split the points?