Loop structure in select clause - sql

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

Related

Cursor returns Multiple Rows & Columns

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

Generate selected rows script SQL

In Sql Server ,
I don't want to generate whole table data script.I want the script of my own inserted 100 rows script . My table have huge record and it takes long if i generate script of whole table Also its not good to find my rows again from that script and pick.
Is there any way to put criteria where i can specify my range ?
something like where condition as we use in sql queries ?
Thanks.
You can use something like this.
It's called sp_generate_inserts. There are many examples in the header of the stored procedure. Take a look at example number 4.

How to execute dynamic query in anypoint studio?

currently I am trying to create one application using Any point studio. But stuck here...
I am having two select query to fetch the data from two different tables of a single database and according to the request it will decide from which table the data will be fetched.
I'm able to create the dynamic query which will take the dynamic table name and column name. But, I wanted to take two different query. As the column name in the where clause is different for both.
please help me out...
Thanks
You should be able to use MEL expressions in the database connector by choosing "Dynamic" type select query. So you only have to put the correct clause on a string variable and reference it on the connector.
Anyway, I would not try that approach. I would place a "choose" flow control and two database connectors and decide which of them use base on your condition. I would use "Parametrized" type select query, it is more secure than using "Dynamic".
Best regards,
José

How to use a dynamic variable in SQL statement

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.

query a table not in normal 3rd form

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().