Subject RE: [firebird-support] question table numbers in select
Author Svein Erling Tysvær
>> I would like to assign a element-group to an table. For example group
>> valve - table t_valve. Now I can realize this with an statement, but
>> perhaps I can assign the table not by its name, rather with its table
>> ID? Maybe I can select the ID and. "select * from TABLEID where xxx"?
>
>Is there a possible way to select/insert records in a table where only
>the tablename is known - and without a statement? In my case the structure
>of some tables is the same, only the names are different. I can assign a
>tablename to an category and would like to update the corresponding table.
>
>For example:
>
>Members:
>ID Name Category
>1 Compressor1 1
>2 Compressor2 1
>3 Valve1 2
>
>Categories:
>ID Table
>1 TCompressor
>2 TValve
>
>TCompressors:
>ID STATE .
>1 .
>
>Now If the PLC send me a change of ID 1, I would like to look for the right
>table and update this. Select table from . . update TABLENAME set STATE = .
>
>How can I realize this without an statement?

Sorry, Olaf, I still don't quite get your question, but I guess something similar to the following might be what you want:

EXECUTE BLOCK(ID INTEGER, STATE VARCHAR(55)
AS
DECLARE VARIABLE S VARCHAR(255);
BEGIN
SELECT C.TABLE
FROM CATEGORIES C
JOIN MEMBERS M ON C.CATEGORY = M.CATEGORY
WHERE M.ID = :ID
INTO :S;
S = 'UPDATE ' +S+ ' SET STATE = '+STATE+' WHERE ID = '+ID;
EXECUTE STATEMENT S;
END;

Now, I've assumed that TCompressor and TCompressors are the same in your description, and I'm very uncertain whether you want to update all records of TCompressors or only the one with the same ID as in Members or something completely different that you simply haven't described. I generally don't use EXECUTE STATEMENT myself, and chose to not use parameters within S since I simply do not know how to use them (albeit with STATE being a VARCHAR parameter, this does make the statement vulnerable to SQL injection attacks). Moreover, it is likely that there are some errors in my suggested EXECUTE BLOCK statement above and you may want to change EXECUTE BLOCK to a stored procedure or something similar.

HTH,
Set