SQLPlus Won't Display Columns In CSV When No Results Returned - sql

In SQL Plus I am using "spool" and "set markup csv on" to send sql SELECT queries to csv files on my computer.
This all works well enough, except when a query generates zero results, it doesn't display the column headings. It just says 'no results returned,' but I still need it to show the column names in that case.
How can I change this?

Related

How to merge two rows in SSIS Excel SQL Command

I have a table in the middle of an excel sheet. The table is given below, here the header is defined in two rows so I need to capture both of them together.
For an example the header for the second column has to be Management JG8
For this purpose I'm using the SQL command option in SSIS and I executed the following query and the output is shown below.
SELECT * FROM [Monthly SSC Report$W7:AJ31]
The headers are not what I'm expecting, I require assistance in modifying the sql query to get the header by merging those two rows.
Connect your Excel file normally to the Source Data Flow
Your Source data should look like this:
In the Excel Source Editor, Rename the required columns. For Example:
To ignore the first row., add a conditional split that filters rows with Grand Total = NULL.

VB.Net Query CSV File

I have a a csv file I need to process to find just a few line items. I followed the tutorial here for reading in a CSV file into a DataTable. It worked very well when my query was:
SELECT * FROM <NameOfCsvFile>
This is great but it returns the whole CSV file as a DataTable. I only want a portion of the file to check for known errors. So I modified my query to be like this:
SELECT * FROM <NameOfCsvFile> WHERE Column1 IN (Int1,Int2,Int3,Int4,Int5)
When I checked the DataTables output in the debugger it had the right row count (being 5) but the list of rows was equal to NOTHING.
How can I get this to work MS's query interface instead of manually looping through the whole CSV file to return the 5 records I seek out of hundreds?
I figured it out. The query worked perfectly but the debugger doesn't allow you to see what rows are returned as they haven't been decoded yet. Calling Dim rows() as DataRows = dt.Select("") allowed me to see the actual results of the CSV file query.
All is good in the world now

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.

How can I extract the value of a varbinary(max) column?

I have a varbinary(max) column that is storing images in an SQL database.
I am working on a newdb script, where an application creates a new instance of the db and populates a few of the tables. One of those tables I am working on is initializing that image column.
In order to do this, I printed the contents of the column using a select statement and pasted the content into the insert statement of the newdb script. This appeared to work initially, but the image didn't load correctly.
So I compared the DATALENTH() of the original data (5469988) and the new data (21839). It appears the Microsoft SQL Server management Studio - 2014 cut off the data why I copied it from the original db at a certain point. I need to be able to get the entire content of the column. Any ideas?
select cast(convert(varchar(max), VarBinaryMaxColumn, 1) as xml) from Table
Instead of copying/pasting, right-click on the results and do 'Save Results As...', and that should export the full contents. Funny thing is setting the query output to text or file explicitly will still truncate long data values.
If you copy and paste your limited to the query result options. Mostly columns will be cut of after a certain lenght (often 256 chars).
You can select in the top bar "save result as..." which will prompt you an dialog for data export.
You can use the data export wizard too.

Write a Macro to pull all table and column names used in SQL Query in excel?

I want to write a Macro in excel to pull all the Column Names and Table Names used in SQL Query.
For this i want to put the SQL Query in a Text file or Excel file and then I want to extract all columns and tables used in that SQL using Macro. and I want final result (table and column names) in Excel file.
My database is Oracle.
Depending on your company policy, If you are able to run certain programs, you Could use sql developer, run your query, and export the result set to an XLS format. That will give you your excel file.