Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL query cannot be imported in MS Access 2010

I have a dbo table in SQL with a column that I need to split up into multiple columns based on a delimiter (,). The code to fulfill this is at the end of this question. The code works perfectly as a query, however I would like to import the final table (so after the split up) in MS Access 2010. That's were it goes wrong, as I cannot find the table with ODBC or the query file. Also, because of the "Declare" function I cannot put this code in a view function. The code is from (it also shows what I want to do with my code): https://raresql.com/2015/08/22/sql-server-how-to-split-one-column-into-multiple-columns/

Can you help me with this?

To split up 1 column into multiple columns the code below is used:

DECLARE @delimiter VARCHAR(50)
SET @delimiter=', '

;WITH CTE AS
( 
    SELECT  [Tour number], 
            [TISLOT Time slot begin],
            [TISLOT Delivery day],
            [Gate],
            CAST('<M>' + REPLACE([Gate], @delimiter , '</M><M>') + '</M>' AS XML) AS [Gate XML]
    FROM dbo.TISLOT
)

SELECT  [Tour number], 
        [TISLOT Time slot begin],
        [TISLOT Delivery day],
        [Gate],
        [Gate XML].value('/M[1]', 'varchar(50)') As [Gate1],
        [Gate XML].value('/M[2]', 'varchar(50)') As [Gate2],
        [Gate XML].value('/M[3]', 'varchar(50)') As [Gate3],
        [Gate XML].value('/M[4]', 'varchar(50)') As [Gate4],
        [Gate XML].value('/M[5]', 'varchar(50)') As [Gate5],
        [Gate XML].value('/M[6]', 'varchar(50)') As [Gate6],
        [Gate XML].value('/M[7]', 'varchar(50)') As [Gate7],
        [Gate XML].value('/M[8]', 'varchar(50)') As [Gate8],
        [Gate XML].value('/M[9]', 'varchar(50)') As [Gate9],
        [Gate XML].value('/M[10]', 'varchar(50)') As [Gate10]
FROM CTE 
GO

Thank you in advance

like image 438
TruckTruck Avatar asked Oct 18 '22 10:10

TruckTruck


1 Answers

Consider two special query objects (both available on Ribbon) in MS Access:

  1. Pass-Through query that allows you to retain the SQL Server syntax of the connected BackEnd but run from inside MS Access; this will require specifying ODBC/OLEDB settings on creation.
  2. Make-Table action query to produce a local Access table from above pass through query.

Pass-Through Query

(save as a stored, pass-through query object, slightly adjusted the CTE into a derived table but no reason CTE couldn't work)

SELECT  [Tour number], 
        [TISLOT Time slot begin],
        [TISLOT Delivery day],
        [Gate],
        [Gate XML].value('/M[1]', 'varchar(50)') As [Gate1],
        [Gate XML].value('/M[2]', 'varchar(50)') As [Gate2],
        [Gate XML].value('/M[3]', 'varchar(50)') As [Gate3],
        [Gate XML].value('/M[4]', 'varchar(50)') As [Gate4],
        [Gate XML].value('/M[5]', 'varchar(50)') As [Gate5],
        [Gate XML].value('/M[6]', 'varchar(50)') As [Gate6],
        [Gate XML].value('/M[7]', 'varchar(50)') As [Gate7],
        [Gate XML].value('/M[8]', 'varchar(50)') As [Gate8],
        [Gate XML].value('/M[9]', 'varchar(50)') As [Gate9],
        [Gate XML].value('/M[10]', 'varchar(50)') As [Gate10]
FROM 
   ( 
    SELECT  [Tour number], 
            [TISLOT Time slot begin],
            [TISLOT Delivery day],
            [Gate],
            CAST('<M>' + REPLACE([Gate], ',' , '</M><M>') + '</M>' AS XML) AS [Gate XML]
    FROM dbo.TISLOT 
   ) AS dT

Make-Table Query

(action query can be run one time or saved as a stored query object for regular use)

SELECT * 
INTO [NewMSAccessLocalTable]
FROM [SQLServerPassThruQuery]
like image 74
Parfait Avatar answered Nov 03 '22 23:11

Parfait