I was looking for a way to obtain a tuple where its primary key is equal to the value that I am passing to it as a parameter, that is:
SELECT * FROM clientes WHERE primary_key = 1;
Or it could be:
SELECT * FROM boletas WHERE primary_key = 'boleta1';
The thing is that I don't know in advance what the primary key is, nor its type, but I know that the parameter to evaluate is going to be correct .
In addition, I would need an SQL statement since the ones I found are from MySQL (for example SHOW KEYS FROM clientes WHERE Key_name = 'PRIMARY'
) or SQL Server, what I need is something native to SQL, that any DBMS can understand, since the system could have to deal with anyone.
Is it possible to do this?
I don't think there is an "ANSI" way to get to said information, each engine implements different ways of accessing said data, at one time SQL Server, for example, offered and recommended the use of some Sp's
sp_help_*
, over time it became They were standardizing that data in tables or views of the engine, so retrieving that data can be done with a simple SQL query, that is, you have variants between engines and also between versions of the same engine. As @CharlieBits already told you, you would need an abstraction layer, that is, an ORM to be able to standardize your code. Anyway, as an example, if it helps, I'll give you an example in SQL 2012:Adding the appropriate WHERE you can know from a table which is the primary key (if it has one), the column or columns that make up said index and the data type of each one of them.
I don't know if I understand your question correctly, but basically I know 2 SQL dialects which are:
PL SQL
(typical of Oracle) andTRANSACT SQL
which is the variant used by SQL Server or MySQL among others. In turn, each implementation has variants in terms that are not part of the common corpus of the language. Anyway, the query you want to make should be able to be interpreted by any of the variants. The only problem is in the data type because as far as I know all the variants will try to differentiate between one of the typesstring
and one of the typesnumber
.So, if the parameter you pass must be a number, it must not be quoted in the SQL statement. If it's a chain, yes. That is, in the case that the
ID
is the number 1, but the type is string:For the case where the
ID
is 1 and the type is numeric: