I’m trying to code a general ‘update’ function for all my tables. However, the name of the primary keys is different for each of them. So is it possible, instead of using the name of the primary key column, to use something like which would automatically select the primary key column ?
UPDATE .... WHERE PRIMARY KEY = 'id_number'
Thanks!
Edit: Sorry, it seems I’m not clear, but I must admit I’m really not an expert. So the idea is that I would like it to work on tables that all have only one INT type primary key column. And I would like if possible to avoid making two queries (one for getting the name of the primary key column + the query itself). At first, I wanted to avoid having to make sure I have the right name of the column for each table (mo_id, rc_id etc…).
I’m going to say that you cannot make a general update function for all your tables. Actually it would need to be a stored procedure, the way I’m thinking of it, but that’s another matter. As @Martin Smith points out, the PK can be a composite. I’ve had PKs that have had four fields tied to make it unique. How can you account for that? Also, the PK can be a number of different types. Are you going to code for all of them in this general purpose sproc? Your snippet assumers the PK is a char type. This is not looking plausible. I did find some code to let you ID the PK of any table, but you have a lot more work to do after that, and your full requirements aren’t clear.
From the vaults (this works for SQL Server):
from SQL Server: Get table primary key using sql query