Informix SQL 11.5 Put the title on the columns of an unloaded file - union

Is there a way to make the file from unload more readable?
unload to file1 select * from table1
Other than this bad solution:
unload to file1 select * Title1table union select * from table1

There isn't a way to get the column names into the first row of the unloaded data using standard Informix tools.
Your best bet may be to use my sqlcmd program. You can find it at the IIUG — International Informix Users Group web site in the Software Repository. With it, you'd write:
sqlcmd -d yourdb -H -e 'unload to file1 select * from table1'
and you'd get one line with the column headings (-H) and the other lines in UNLOAD format. You could add -T to get the column type information too, another line after the names (if both are requested). Both the names and the types are in UNLOAD format.
sqlcmd -d stores -HT -e 'unload to "elements.unl" select * from elements'
The first seven lines of output are:
atomic_number|symbol|name|atomic_weight|pt_period|pt_group|stable|
INTEGER|CHAR(3)|CHAR(20)|DECIMAL(8,4)|SMALLINT|CHAR(2)|CHAR(1)|
1|H|Hydrogen|1.0079|1|1|Y|
2|He|Helium|4.0026|1|18|Y|
3|Li|Lithium|6.9410|2|1|Y|
4|Be|Beryllium|9.0122|2|2|Y|
5|B|Boron|10.8110|2|13|Y|

Related

Is it possible to return query results as an Excel/CSV file formatted as a BLOB or base64 encoded string?

Limitations on some software I'm using require me to think outside the box, so can you have a query that automatically returns a "file" with the results in it? I imagine it would be as BLOB or a base 64 encoded string or something similar.
Using Microsoft SQL Server
You can use two methods to achieve this
1. Use SQLcmd
SQLCMD -S SERVERNAME -E -Q "SELECT Col1,Col2,Col3 FROM MyDatabase.dbo.MyTable"
-s "," -o "D:\MyData.csv"
Run this above command in a cmd and achieve the expected result.
2. Use OpenRawset
INSERT INTO OPENROWSET('Microsoft.ACE.OLEDB.12.0','Text;Database=D:\;HDR=YES;FMT=Delimited','SELECT * FROM [FileName.csv]')
SELECT Field1, Field2, Field3 FROM DatabaseName
You need to have the Microsoft.ACE.OLEDB.12.0 provider available. The Jet 4.0 provider will work, too, but it's ancient, so I used this one instead.
limitations:
The .CSV file will have to exist already. If you're using headers (HDR=YES), make sure the first line of the .CSV file is a delimited list of all the fields.
If you can create objects in the target database or another database on the same server, you could use the stored procedures and functions from my answer to this question: SSMS: Automatically save multiple result sets from same SQL script into separate tabs in Excel?
You would just need to exclude the #OutputFileName parameter to force output as a single column, single row varbinary(max).
select * into #systables from sys.tables;
select * into #syscolumns from sys.columns;
select * into #systypes from sys.types;
exec dbo.GetExcelSpreadsheetData
#Worksheets = 'sys.tables/#systables/autofilter|sys.columns/#syscolumns/autofilter|sys.types/#systypes'
drop table #systables;
drop table #syscolumns;
drop table #systypes;
Output (truncated):
ExcelSpreadsheetData
-------------------------------------
0x504B0304140000000800DB78295479400A1

Issue extracting individual image from SQL Server using BCP

I have a table of data, one column contains images. I am trying to extract an individual records image column and export to image file using BCP. My code is as follows:
bcp "SELECT PictureData FROM BigTable WHERE RecordId='ASDF-QWER' queryout "C:\Path\File.jpg" -n -SCONNECTION\STRING -T
The output after running the command appears successful, it says 1 rows copied but when I double click on the file created it says "cannot open this file". If I modify the statement to select different rows and copy them to a text file using something like:
bcp "SELECT Name,Address FROM BigTable WHERE RecordId='ASDF-QWER' queryout "C:\Path\File.txt" -n -SCONNECTION\STRING -T
Then the text file contains the data I expect it to contain

Removing query decorations from psql output in PostgreSQL

I have an SQL file which extracts data from a PostgreSQL database in a particular format. However, I'm getting query "decoration", specifically a "SELECT N" output when creating my temporary tables.
This is an example SQL file.
create temporary table a as
select 1 as the_code, 'Test'::text as the_name;
create temporary table b as
select 2 as the_code, 'Foo'::text as the_name;
select * from a union all
select * from b;
And this is the example output, produced with psql --tuples-only --no-align --file query.sql:
SELECT 1
SELECT 1
1|Test
2|Foo
Note that this question is separate from How to hide result set decoration in Psql output in that it is in relation to different query "decoration". That is, my question is how to remove the "SELECT N", while the existing question is how to remove the column headers and "(n rows)" footer.
EDIT
As a workaround, I could use common table expressions (CTEs) from which the select is run, but it'd be nice if there was a solution to the above so I don't have to rework my exiting SQL.

MySQL dump specific values in DB

I want to make a dump file of a DB, but all I want from the DB is the rows that are associated with a specific value. For example, I want to create a dump file for all the tables with rows related an organization_id of 23e4r. Is there a way to do that?
mysqldump has a --where option, which lets you specify a WHERE clause, exactly as if you were writing a query, eg:
mysqldump -u<user> -p<password> --where="organization_id=23e4r" <database> <table> > dumpfile.sql
If you want to dump the results from multiple tables that match that criteria, its:
for T in table1 table2 table3; do mysqldump -u<user> -p<password> --where="organization_id=23e4r" <database> $T >> dumpfile.sql;done
Assuming you are using a bash shell, or equivalent

mysqldump partial database

I recently decided to switch the company through which i get my hosting, so to move my old db into my new db, i have been trying to run this:
mysqldump --host=ipaddress --user=username --password=password db_name table_name | mysql -u username -ppassword -h new_url new_db_name
and this seemed to be working fine.. but because my database is so freaking massive, i would get time out errors in the middle of my tables. So i was wondering if there was any easy way to do a mysqldump on just part of my table.
I would assume the work flow will look something like this:
create temp_table
move rows from old_table where id>2,500,000 into temp_table
some how dump the temp table into the new db's table (which has the same name as old_table)
but i'm not exactly sure how to do those steps.
Add this --where="id>2500000" at the end of mysqldump command. MySQL 5.1 Reference Manual
In your case the mysqldump command would look like
mysqldump --host=ipaddress \
--user=username \
--password=password \
db_name table_name \
--where="id>2500000
If you dump twice. The second dump will contain table creation info. But next time you want to add the new rows only. So for second dump add --no-create-info option in mysqldump command line.
I've developed a tool for this job. It's called mysqlsuperdump and can be found here:
https://github.com/hgfischer/mysqlsuperdump
With it you can speciffy the full "WHERE" clause for each table, so it's possible to specify different rules for each table.
You can also replace the values of each column by each table in the dump. This is useful, for example, when you want to export a database dump to use in development environment.

Resources