JOIN Excel spreadsheet to MsSQL Query

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • code green
    Recognized Expert Top Contributor
    • Mar 2007
    • 1726

    #1

    JOIN Excel spreadsheet to MsSQL Query

    I need to combine data from SQL Server tables and a CSV file.
    This is the skeleton query
    Code:
    SELECT 
    customer 'Account',
    --Credit_Limit sage	
    full_name Name, 
    address,city,phone,
    LOWER(dbo.TrimAll(email_address)) 'Email Addr'
    FROM cust_acc WITH(NOLOCK)
    JOIN cust_data WITH(NOLOCK) 
    	ON cust_acc.customer = cust_data.customer
    JOIN accounts WITH(NOLOCK) ON acc_code = acc_id
    WHERE acc_code IN ('XXXX')
    The missing--Credit_Limit sage data has been extracted into a CSV file
    Code:
    account_ref  credit_limit
    Where account_ref matches customer 'Account'. There are other useful fields also.

    Can this be done from Query Analyser or am I better in Excel Microsoft Query?
    I know how to query an Excel spreadsheet from a stored procedure
    Code:
    OPENROWSET('Microsoft.Jet.OLEDB.4.0','Excel 8.0;
    Database=\\nn.n.n.nn\path\to\spreadsheet\speadsheet.xls',
    But how can I JOIN to a spreadsheet in a query?
  • code green
    Recognized Expert Top Contributor
    • Mar 2007
    • 1726

    #2
    Well I have solved it.
    1. The file cannot be CSV, it needs to be in .xls format
    2. Place the file on the same server as the database. I am not saying this is essential but I tried with DB on server and file on local machine, no luck. File on server OK.
    3. This is the JOIN syntax and connection string
    Code:
    LEFT JOIN
    OPENROWSET('Microsoft.Jet.OLEDB.4.0','Excel 8.0;
    Database=\\servername\drivename\path\tofile\file.xls',
    'SELECT * FROM [sheetname$]') sage
    ON account_ref = cust_acc.customer
    4. IP address did not work, used actual server name.
    5. Again with drive, drive letter no good, used actual given drive name.

    Comment

    Working...