How to separate select Queries from a text file - sql

I am having a text file which have some queries and text merged, i want to separate those
Exa: myfile.txt
my file abc.........
select * from exa
my file ABC Again.........
I only want to extract queries from file, and queries might be multi-lined.

This is a little bit difficult task. You can dump the data of the text file in a SQL Server table and use LIKE operators to get the data having SQLs (INSERT, SELECT, UPDATE AND DELETE).

Related

How to export multiple tables in oracle into multiple .csv files in Oracle SQL developer

I am trying to export multiple tables to individual .csv files (approximately 70 tables need to be exported) in oracle SQL Developer. As of right now, I am running through this process
Run Query
SELECT * FROM TABLE;
From the result window, click "Export Query Results", choosing the encoding and delimiter and saving it as a .csv
This is a lengthy process, and takes around a minute per table (lots of information!), I can't help but think there has to be an easier, more efficient way of doing this. I just can't find any information.
Tools - Database Export
Pick your file format (csv) and directory.
I have Excel shown in the picture, but there's a dozen formats to choose from, including delimited and CSV. If you want European CSV (;), pick delimited and change the delimiter to ;
Then pick your 70 tables.
I think the best solution for mass table export is not to use the embedded SQL Developper export tool, but use the SPOOL SQL option.
you can check here how to generate SPOOL files:
How do I spool to a CSV formatted file using SQLPLUS?

Write results of SQL query to multiple files based on field value

My team uses a query that generates a text file over 500MB in size.
The query is executed from a Korn Shell script on an AIX server connecting to DB2.
The results are ordered and grouped by a specific field.
My question: Is it possible, using SQL, to write all rows with this specific field value to its own text file?
For example: All rows with field VENDORID = 1 would go to 1.txt, VENDORID = 2 to 2.txt, etc.
The field in question currently has 1000+ different values, so I would expect the same amount of text files.
Here is an alternative approach that gets each file directly from the database.
You can use the DB2 export command to generate each file. Something like this should be able to create one file :
db2 export to 1.txt of DEL select * from table where vendorid = 1
I would use a shell script or something like Perl to automate the execution of such a command for each value.
Depending on how fancy you want to get, you could just hardcode the extent of vendorid, or you could first get the list of distinct vendorids from the table and use that.
This method might scale a bit better than extracting one huge text file first.

Outputting SQL SERVER Query Results to CSV file

I'm using a basic query to gather data from a few joined tables, and I need to be able to export the data to a CSV (or text) file in order to be imported into Excel. The query format is:
SELECT
Item1 as 'blah'
FROM
table1 JOIN table2
WHERE Condition
GROUP BY ...
HAVING ....
I have the proper output setup correctly through the query, so I'm only looking for a way to output it to a file. If it would be easier to use a stored procedure, then it would be no problem to throw that around the query. I'm just looking for something that can write the output to a file, WITHOUT using a third-party tool, as this needs to be moderately portable.
If you need more detail from the query, I can supply that (but it really is basic).
Use a BCP utility [MSDN]
That's what most commonly used.
If you have access to SSIS (through the Business Intelligence Development Studio), create a data flow task with an OLE DB Source going to a Flat File Destination. Or, you can go straight to Excel, if you don't want to worry about converting the delimited file.

Updating multiple rows with information stored in text file

I have a comma delimited text file containing discrepancies across two different databases, and need to update one of the databases with information from the aforementioned text file. The text file is in the following format:
ID valueFromDb1 valueFromDb2
1 1234 4321
2 2345 5432
... ... ...
I need to go update a table by checking for the ID value, and where valueFromDb1 exists replace it with valueFromDb2. There are around 11,000 rows that need to be updated. Is there a way I can access the information in this text file directly through an sql query? My other thought was to write a java program to do this for me, but I'm not convinced that is the easiest solution.
The article below demonstrates one way to read a text file in MS SQL Server by using xp_cmdshell. In order for it to work the file has to be on one of the drives of the server. Once you have the file loaded into a table variable (which is what the code in the article will do) you should be able to do the joins and updates pretty easily. Let us know if you need any other help.
http://www.kodyaz.com/articles/read-text-file-using-xp_cmdshell.aspx

How to write SQL Query that matches data from a .csv file to a table in MySQL?

Is it possible for me to write an SQL query from within PhpMyAdmin that will search for matching records from a .csv file and match them to a table in MySQL?
Basically I want to do a WHERE IN query, but I want the WHERE IN to check records in a .csv file on my local machine, not a column in the database.
Can I do this?
I'd load the .csv content into a new table, do the comparison/merge and drop the table again.
Loading .csv files into mysql tables is easy:
LOAD DATA INFILE 'path/to/industries.csv'
INTO TABLE `industries`
FIELDS TERMINATED BY ';'
IGNORE 1 LINES (`nogaCode`, `title`);
There are a lot more things you can tell the LOAD command, like what char wraps the entries, etc.
I would do the following:
Create a temporary or MEMORY table on the server
Copy the CSV file to the server
Use the LOAD DATA INFILE command
Run your comparison
There is no way to have the CSV file on the client and the table on the server and be able to compare the contents of both using only SQL.
Short answer: no, you can't.
Long answer: you'll need to build a query locally, maybe with a script (Python/PHP) or just uploading the CSV in a table and doing a JOIN query (or just the WHERE x IN(SELECT y FROM mytmmpTABLE...))
For anyone new asking, there is this new tool that i used : Write SQL on CSV file