I need your assistance. I am running the below sql query from Oracle SQL Developer so the results can be save to a csv file. I run this query using the Run Script and I can see the records that are being save to the csv file.
How can I modify the query so it can show me the number of records being save to the file? For example something like printing 1000 of 200000.
spool c:\temp\myoutputfile.csv
select * from mytable;
spool off;
You can add rownum column to your query result like below
select rownum, mytable.* from mytable;
Note, SQL doesn't have other options to print some text separately while selection is under progress. Of course you have option to print some hard coded text as part of a resulted column. Otherwise you may have to write PL SQL procedure to meet your requirement.
Related
So, I am working with redshift and SQL for the first time. I have run into this problem due to my limted knowledge of SQL.
I have this first query which return me tables with the columnA. (TableX, TableY... etc)
SELECT tablename
FROM PG_TABLE_DEF
WHERE ("column" = 'columnA');
Also I have this second query which returns me all the rows from the table containig certain value of columnA.
SELECT *
FROM TableX
WHERE columnA='123934'
What I want to achieve is take the result from the first query which is list of tables, and for each table run the second query i.e. get the rows with value of columnA=123934 for each table returned from first query.
What you want to achieve is done using dynamic SQL. Dynamic queries let you run a query from a string.
I am not an Redshit user but to generate the SQL string you need to run you could use the following query:
SELECT 'SELECT * FROM '||tablename ||' WHERE ColumnA= ''123934''; '
FROM PG_TABLE_DEF
WHERE ("column" = 'columnA');
You can try running it manually.
I have millions of Id's in text file (temp.txt). I have to write a select statement which recursively execute by picking the ids from text file and return the output.
select * from table where id=123;
temp.txt
1234
1224
1232
.
.
Some options:
Created a "linked server" using a text DB driver and join to your real data
Load the text data to a temp table somehow and join that to your "real" data
Use a script to generate a query with a massive "IN" clause (or multiple queries that are UNIONed together if the IN clause is too big)
Loading to a temp file is probably the most efficient overall, but may or may not be possible depending on your DB permissions.
Let's say you have 5 IDs in your text file.. the IDs could be something like 1984, 2346, 2345, 6534, 1234.
To write a query to select all of these, try this:
SELECT * FROM your_table WHERE column IN (1984, 2346, 2345, 6534, 1234);
For every ID in your text file, you need to put the ID with a comma after it inside the parenthesis.
This query selects all columns for each record in a table where any of the IDs you search for in the parenthesis match a column value based on the column you suggested in the query.
i had a problem .... i have and old excels files and i want to save them into SQL database ... MY Q is : if i want to do for example in .xls from (C16:C28) want to take it and put it into table1 in field NAMES .. how i can write it in query ?
note am using Microsoft SQL Server Management .. and try to get all my xls and import it into data base via sql query take from those files data and put it into my database ..
INSERT INTO [table1] (Names) VALUES ('&C16&') to ('&C28&')
is this true ? idk how to write it .. need help
When you use openrecordset you can specify a range [Sheet1$C16:C28] on the table in the query parameter
INSERT INTO [table1] (Names)
SELECT *
FROM OPENROWSET
('Microsoft.Jet.OLEDB.4.0',
'Excel 8.0;Database=C:\Foo.xls;HDR=NO',
'select * from [Sheet1$C16:C28]') AS t
Using SQL Server Management Studio is there a way I can select one or more rows in the grid of select results and have SQL Server Mangement Studio generate one or more insert statements (one for each row selected) which would insert that data into a table with the same schema?
Edit: I know how to create one manually, but I was hoping there would be something that would create it automatically for me. If you are familiar with Toad there is a way to have Toad generate inserts based on data in the results pane and I was hoping SSMS had an equivalant function.
Try to save the query result into a disposable table.
For example:
SELECT * INTO disposable_customer_table FROM customer_table WHERE id IN (in range of something)
Then do a db -> Tasks -> Generate Scripts.
Select specific database objects.
Choose disposable_customer_table from the list of table names.
Choose Save to file.
Make sure to do an Advance setup and select "Data only" from the 'Types of data to script'.
Tweak the result file and rename the disposable_customer_table back to the original table name.
Clean it up and drop the disposable_customer_table.
select 'insert into tableB values (', tableA.x ,',',tableA.y,',',tableA.z,')' from tableA
I think you have two options here:
Create your inserts manually. For instance:
select Name, Surname,
'insert into Person (Name,surname) values ('''+Name+''','''+Surname+')'
from Person
This gets you the results and, in the last column, the insert script for the row. You can then select and paste it in an Editor window.
Right click on the db -> Tasks -> Generate Scripts. Press then Advance and select "Data Only" (Default is Schema Only).
Perform your query and right click on the blank area where the column headers meet the row number in the Results view.
You can then select Script Grid Results:
I am using the Solaris OS. From Solaris I'm logging into SQL*Plus.
My database is Oracle 9i.
I am spooling the output of my query into a file. How can I get it in CSV format so that I can copy it into Excel?
My query is like the follwing.
select name,id,location from employee;
I have found a way out of it.
We can use concatenation here,
select name,id,location from employee;
gives us 2 different columns, but not in CSV format.
I did
select name||','||id||','||location from employee;
We get the output in a CSV format. It has just concatenated the output with commas (,).