Generate selected rows script SQL - 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.

Related

Pentaho replace table name in a loop dynamically

I currently have a transformation setup with 2 table inputs and one Merge Rows (Diff), the SQL select statement in both table inputs are constant, they are not changing except for the table name. So I have:
select * from THIS_WILL_CHANGE
I have around 100 tables and I don't want to manually enter the table names every iteration, especially because this is automation...
What is the best way to achieve this? Is there any way to read like a CSV file with all the table names and loop that way? Any help is appreciated..
This is something I've had to do before too!
You can do this with a variable and a job which executes once for each row of the previous step.
Create a parent job to host these steps
Create a transformation which gets the table names from 'somewhere' eg. CSV file, or database query - a select on all_tables for tables with the same column names might be a nice way to do this for all time...
In this same transformation, use copy rows to result step to push the data back to the job
Create a new 'sub job', which executes once for each row, and has a hop from the 'get data' step in the main job
In the sub job, create two transformations, one to set the variable from the results field, and one to do your select
In your select query, check the box 'substitute variables' and place your variable with the same name as your set variables step into your SQL as ${yourVariableHere}
I've put this in an image below, which hopefully helps you.
The dynamic sql row step is a good option for this, provided the tables all have the same layout/metadata.

Retrieve results from a batch of SQL queries in Pentaho or Postgres?

I'm still relatively new to SQL and Pentaho.
I've pulled a table with two different IDs and need to run a query for each specific instance.
For example,
SELECT *
FROM Table
WHERE RecordA = 'value in column A'
AND RecordB = 'value in column B'
I need the results back, either appended to new columns in the original table or part of their own text file output.
I was initially looking at using a formula for this inside of Pentaho, but couldn't quite figure it out. Since I have the query written I threw it into Excel and got the concatenated results (so a string of 350 or so queries that I need to run). I'm just not sure how to accomplish this - I tried the Execute SQL Script inside of Pentaho but it doesn't seem to do output?
Any direction would be useful. I've searched a little but have come up short so far, possibly because I am still pretty new to this platform.
You can accomplish this behavior in a lot of ways, with a "Database Lookup" step for example, but I usually do that in a quite easy way and here is a example for your tests, I hope it helps.
The idea here is to have two Table input steps, the first one will fetch the IDs we want to look at. For example you may use a SQL query similar to note on the left. The result will be a 1 column stream of rows.
Next we have a Table Input that reads the rows received and executes it's query for each row. I'll add a screenshot with the options that I selected.
What it does is replace a placeholder '?' with the data that is received. If you need two columns use two '?' but remember that it will replace the first one with the first column and the second one with the second column
And you are good to go. Test it a couple of times and good luck.
And the config for the second table input.

Searching in PL/SQL /Oracle Forms

This is with respect to search of a text in a table
Table_Name:
Details
Columns:
Fname,Mname,Lname,NName
This table contains nearly one lakh records
We are using Oracle forms for some querying option
The user input one name the form searches the table for the name and based on the name either(Fname/Mname/Lname/NName) in which column its is present further actions are proceeeded.
The search is taking a long time since we have huge amount of data present in the table.
I tried with Functional indexes for the table but t did not work its also taking more time
Later i tried with something like this
concatenated all the names into one name and put it into a cursor.
Using the cursor output i tried with Instring but it is hanging
I also tried with searching for building a dynamic cursor but it did not work.
My database is Oracle
Can u help me to out to find an effective solution or please help me if i have missed something.
Thanks
First of all, 1 lakh (100,000) records is not in itself a large table.
The problem I can see is the query appears to be doing an OR against the Fname/Mname/Lname/NName columns.
This means the query will be doing at least one full-table scan to obtain the results.
You may wish to use debug to obtain the query it is firing against the database and attempt to tune this at the SQL prompt using auto trace.
You may need to clarify if the search is also doing something like a LIKE against these columns rather than an EQUALS. As a LIKE will impact the query further and affect indexes.
Certainly the use of INSTR will disable indexes on your searched column.
It is not clear if what your block is based on ie. table, view, query, procedure
You may want to try using the hint on the block properties of the form FIRST_ROWS.

Loop structure in select clause

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

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