I am creating an upate trigger. I have a situation where I need to test a condition on a table column, without actually knowing what the exact column name is. The trigger is generic and can applied to any table, with varying columns.
Pseudo-code:
// define a cursor that loops through all columns in "MyTable"
Define cursor C1 for (select COLS from SYSCAT.TABLES where TABS="MyTable")
FOR
// take the next column from the cursor
#temp_var = C1.COLS
// DELETED and INSERTED are tables that also contain the same columns as "MyTable" table.
if(DELETED.#temp_var <> INSERTED.#temp_var)
THEN
...
The above statement if(DELETED.#temp_var <> ... does of course not work, but maybe you can see what I am trying to do? So I would want it to be during runtime e.g. if(DELETED.MyColumn <>... where "MyColumn"is a column in"MyTable"and also inINSERTEDandDELETED columns. Note that because this method should be generic, I do not know beforehand what columns the table has (depends on the specific table in use).
Any ideas on how to build the if-statement dynamically like that?
In DB2 SQL you cannot refer to columns dynamically. So, you won't be able to do that using only SQL. You could possibly call an external procedure written in another language from within the trigger. Or, you could rethink your overall design for what you are trying to do. I don't see any other options.
Related
My SQL is limited and I have inherited the schema so please go easy!
The upper tables are populated, the lower section is to be completed.
The customer wants a simple reference, C_SCAFFOLD_DOMAIN_COMBO, for every C_SEQUENCE_RESULT of the same TYPE having the same C_PROTEIN_REGIONs (only ever two) of the same REGION_NAME.
My intention is to read each C_SEQUENCE_RESULT getting the (2) C_PROTEIN_REGION.SEQUENCEs with their REGION_NAME using a cursor.
I can then check if they are referenced by a C_SCAFFOLD_DOMAIN_COMBO already.
If so, then simply add the correct C_SCAFFOLD_DOMAIN_NAME FK to C_SEQUENCE_RESULT.
If not, create a new C_SCAFFOLD_DOMAIN_NAME entry with the appropriate C_PROTEIN_REGION refs and add the FK to C_SEQUENCE_RESULT.
My SQL query returns two results for each C_SEQUENCE_RESULT e.g.
REGION_NAME------SEQUENCE
Loop 1------ABCDEFG
Loop 2------HIJKLMN
Running as a simple query and can INSERT INTO a temp table and get the values. As a cursor I don’t seem to be able.
How can I read the data returned above using a cursor?
Thanks
I have a database. I created it with HeidiSQL. Its look like this.
I enter the value-1 and value-2.
Is there a way to enter a formula to Result column like " =Value-1 * Value-2 " ? I want my database to calculate the Result when I enter my values to other cells.
A trigger is one way to achieve automated column content.
A second one is a view, which you can create additionally to the table. That view could contain SQL which generates the result:
SELECT value1, value2, value1*value2 AS result
A third (more modern) alternative is adding a virtual column in your existing table. You can do that with HeidiSQL's table editor, like shown in the screenshot. Just add a new column with INT data type, and set its Virtuality to "VIRTUAL", and Expression to "value-1 * value-2". That's it.
I'm not familiar with HeidiSQL, but it appears to be a front end? What RDBMS are you using, for example SQL Server allows a computed column.
SQL
ALTER TABLE YourTable
ADD Result AS ([Value-1] * [Value-2])
Right click your database name in the folder structure, go to --> create new then -->Trigger
Then you can create a trigger that when entering data, will be activated on the entire column like this:
But you will need to know how to write the actual query and function. This requires basic knowledge that is generally generic and consistent of most all SQL languages.
I'm searching for a long time for some way to iterate through PL/SQL row, but I did not get any appropriate results.
For example, if I have a PL/SQL row that looks like this row(first_name,last_name) and I want to print the first name and the last name using PL/SQL without knowledge about the row i.e. I don't know how the row looks like so I need some kind of code that looks like this
FOR column IN My_Row --some way to iterate through the row.
LOOP
PRINT column... --do my stuff, in this example
END LOOP;
If you know the name of the table the row is from then you can get the names of the columns from ALL_TAB_COLUMNS. You can build a dynamic SQL around that. See this Ask Tom article regarding something like this: Referencing %rowtype variables without using column names.
Based upon your comment you'll be going through all your tables so you can use ALL_TABLES to get all their names. Though remember that ALL_TABLES is all tables your login has access to while DBA_TABLES is every table in the database and USER_TABLES is those that your user owns.
I am writing a query where the number of columns is not fixed and dependent on the number of values provided by another table. Hence I am not able to write a static query that specifies the number of columns being selected. I need to apply something similar to a loop structure where the number of columns are created dynamically depending on the values provided. Is it possible to use loop structure in select clause ? In case this is not possible then I may have to use a procedure to build the query dynamically.
Thanks in advance
Create your query dynamically and execute it using 'EXECUTE IMMEDIATE'. Refer http://download.oracle.com/docs/cd/B12037_01/appdev.101/b10807/13_elems017.htm for more details
Hi I have a table which was designed by a lazy developer who did not created it in 3rd normal form. He saved the arrays in the table instead of using MM relation . And the application is running so I can not change the database schema.
I need to query the table like this:
SELECT * FROM myTable
WHERE usergroup = 20
where usergroup field contains data like this : 17,19,20 or it could be also only 20 or only 19.
I could search with like:
SELECT * FROM myTable
WHERE usergroup LIKE 20
but in this case it would match also for field which contain 200 e.g.
Anybody any idea?
thanx
Fix the bad database design.
A short-term fix is to add a related table for the correct structure. Add a trigger to parse the info in the old field to the related table on insert and update. Then write a script to [parse out existing data. Now you can porperly query but you haven't broken any of the old code. THen you can search for the old code and fix. Once you have done that then just change how code is inserted or udated inthe orginal table to add the new table and drop the old column.
Write a table-valued user-defined function (UDF in SQL Server, I am sure it will have a different name in other RDBMS) to parse the values of the column containing the list which is stored as a string. For each item in the comma-delimited list, your function should return a row in the table result. When you are using a query like this, query against the results returned from the UDF.
Write a function to convert a comma delimited list to a table. Should be pretty simple. Then you can use IN().