Script to save to CSV from ODBC

Soldato
Joined
19 Oct 2006
Posts
3,708
I have a need to save a CSV export from an ODBC data source which I am struggling with. I have been using a tool called ODBCView but this only supports 1000 lines when run from a batch file, can I do this with some sort of powershell or vbscript? The query I run is below, I also need to ftp this file somewhere but have been struggling also with Microsoft's ftp command line tool (perhaps firewall related).

SELECT STOCK.STOCK_CODE, STOCK.DESCRIPTION, STOCK.UNIT_OF_SALE, STOCK.NOMINAL_CODE, STOCK.PURCHASE_REF, STOCK.SUPPLIER_PART_NUMBER, STOCK.LOCATION, STOCK.COMMODITY_CODE, STOCK.SALES_PRICE, STOCK.WEB_DESCRIPTION, STOCK.WEB_DETAILS, STOCK.WEB_CATEGORY_1, STOCK.WEB_CATEGORY_2, STOCK.WEB_CATEGORY_3, STOCK.WEB_IMAGE_FILE, STOCK.WEB_PUBLISH, STOCK.WEB_SPECIAL, STOCK.INTRASTAT_COMM_CODE, STOCK.IGNORE_STK_LVL_FLAG, STOCK.DEPT_NUMBER, STOCK.DEPT_NAME, STOCK.TAX_CODE, STOCK.ASSEMBLY_LEVEL, STOCK.LINK_LEVEL, STOCK.STOCK_CAT, STOCK.STOCK_CAT_NAME, STOCK.STOCK_TAKE_DATE, STOCK.LAST_PURCHASE_PRICE, STOCK.LAST_DISC_PURCHASE_PRICE, STOCK.AVERAGE_COST_PRICE, STOCK.QTY_IN_STOCK, STOCK.QTY_ON_ORDER, STOCK.QTY_ALLOCATED, STOCK.QTY_LAST_ORDER, STOCK.QTY_REORDER, STOCK.QTY_REORDER_LEVEL, STOCK.QTY_LAST_STOCK_TAKE, STOCK.QTY_MAKEUP, STOCK.LAST_SALE_DATE, STOCK.LAST_PURCHASE_DATE, STOCK.UNIT_WEIGHT, STOCK.HAS_NO_COMPONENT, STOCK.HAS_BOM, STOCK.INACTIVE_FLAG
FROM STOCK STOCK
WHERE (STOCK.INACTIVE_FLAG=0)
ORDER BY STOCK.STOCK_CODE

DSN=SageLine50v21;UID=username;PWD=password

Any ideas chaps?
 
Associate
Joined
2 Jul 2003
Posts
2,442
Dawdle in a bit of PowerShell here and think this might work:

Code:
#Function which connects to SQL server and DB, queries it and returns result as object
Function sql($sqlText, $database, $server)
{
    $connection = new-object System.Data.SqlClient.SQLConnection("Data Source=$server;Integrated Security=SSPI;Initial Catalog=$database");
    $cmd = new-object System.Data.SqlClient.SqlCommand($sqlText, $connection);

    $connection.Open();
    $reader = $cmd.ExecuteReader()

    $results = @()
    while ($reader.Read())
    {
        $row = @{}
        for ($i = 0; $i -lt $reader.FieldCount; $i++)
        {
            $row[$reader.GetName($i)] = $reader.GetValue($i)
        }
        $results += new-object psobject -property $row            
    }
    $connection.Close();

    $results
}

$sqlResults = @()

$server = "ServerName"
$database = "DB Name"
$sqlText = "SELECT STOCK.STOCK_CODE, STOCK.DESCRIPTION, STOCK.UNIT_OF_SALE, STOCK.NOMINAL_CODE, STOCK.PURCHASE_REF, STOCK.SUPPLIER_PART_NUMBER, STOCK.LOCATION, STOCK.COMMODITY_CODE, STOCK.SALES_PRICE, STOCK.WEB_DESCRIPTION, STOCK.WEB_DETAILS, STOCK.WEB_CATEGORY_1, STOCK.WEB_CATEGORY_2, STOCK.WEB_CATEGORY_3, STOCK.WEB_IMAGE_FILE, STOCK.WEB_PUBLISH, STOCK.WEB_SPECIAL, STOCK.INTRASTAT_COMM_CODE, STOCK.IGNORE_STK_LVL_FLAG, STOCK.DEPT_NUMBER, STOCK.DEPT_NAME, STOCK.TAX_CODE, STOCK.ASSEMBLY_LEVEL, STOCK.LINK_LEVEL, STOCK.STOCK_CAT, STOCK.STOCK_CAT_NAME, STOCK.STOCK_TAKE_DATE, STOCK.LAST_PURCHASE_PRICE, STOCK.LAST_DISC_PURCHASE_PRICE, STOCK.AVERAGE_COST_PRICE, STOCK.QTY_IN_STOCK, STOCK.QTY_ON_ORDER, STOCK.QTY_ALLOCATED, STOCK.QTY_LAST_ORDER, STOCK.QTY_REORDER, STOCK.QTY_REORDER_LEVEL, STOCK.QTY_LAST_STOCK_TAKE, STOCK.QTY_MAKEUP, STOCK.LAST_SALE_DATE, STOCK.LAST_PURCHASE_DATE, STOCK.UNIT_WEIGHT, STOCK.HAS_NO_COMPONENT, STOCK.HAS_BOM, STOCK.INACTIVE_FLAG
            FROM STOCK STOCK
            WHERE (STOCK.INACTIVE_FLAG=0)
            ORDER BY STOCK.STOCK_CODE"

$sqlResults = sql $sqlText $database $server
$sqlResults | Export-Csv "Path\filename.csv" -NoTypeInformation
 

Pho

Pho

Soldato
Joined
18 Oct 2002
Posts
9,325
Location
Derbyshire
I used to use ODBCView at work, it's awful :p. The best part is that it doesn't warn you it's going to delete your query when you switch to another connection :D.

I recommend SQL Workbench instead. It will let you output to CSV and it not only supports a tabbed workspace for several queries but it actually saves them between connections :p.

If you already have an ODBC connection* it's really simple to set-up. Just run it and and set-up a new connection using the ODBC bridge driver. All you need to do is set URL to your ODBC connection name. E.g,. if your ODBC connection was prodserv1 then you would set URL to be:

Code:
jdbc:odbc:prodserv1

(* and if you don't, it's probably easier to set one up first under Windows)


edit: missed the part about batching, oops. I've never tried it, but it will apparently do it. Though I'm sure you'll better off with Washout's powershell as above.
 
Last edited:
Associate
Joined
2 Aug 2004
Posts
564
Location
Adelaide
A bit overkill but if you are on a SQL server you could use integration services to build a small SSIS package, to do that in about 5 minutes. The SSIS package can then be executed from an SQL agent job and scheduled if required.
 
Back
Top Bottom