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
Consider two special query objects (both available on Ribbon) in MS Access:
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]
You can put your code into stored procedure. Then call it from Access and put it into table with the help of recordset:
Dim db As New ADODB.Connection
Dim rs As ADODB.Recordset
Dim rstCurr As DAO.Recordset
Dim dbsCurr As Database
db.Open "Provider=SQLNCLI11;Server=SERVER\INSTANCE;Database=MyDataBase;Trusted_Connection=yes;"
db.CommandTimeout = 180
db.CursorLocation = adUseClient
Set rs = db.Execute("EXEC dbo.StoredProc")
Set dbsCurr = Access.CurrentDb
Set rstCurr = dbsCurr.OpenRecordset("AccessTable", dbOpenDynaset)
Do Until rs.EOF
rstCurr.AddNew
rstCurr.Fields(0).value = rs.Fields(0).value
rstCurr.Fields(1).value = rs.Fields(1).value
...
rstCurr.Update
rs.MoveNext
Loop
Set rs = Nothing
db.Close: Set db = Nothing
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.