I have the following query, which works:
DECLARE @Combined VARCHAR(MAX)
SET @Combined = 'mac cheese';
DECLARE @KeyTable TABLE (Keyword VARCHAR(MAX))
INSERT INTO @KeyTable
SELECT @Combined
IF OBJECT_ID('tempdb..#Temp') IS NOT NULL
DROP TABLE #Temp
SELECT LTRIM(RTRIM(m.n.value('.[1]','varchar(8000)'))) AS Keyword
INTO #Temp
FROM
(
SELECT CAST('<XMLRoot><RowData>' + REPLACE(Keyword,',','</RowData><RowData>') + '</RowData></XMLRoot>' AS XML) AS x
FROM @KeyTable
)t
CROSS APPLY x.nodes('/XMLRoot/RowData')m(n);
SELECT * FROM #Temp
When I try to add an ampersand to the @Combined
it gives a special character error:
DECLARE @Combined VARCHAR(MAX)
SET @Combined = 'mac & cheese';
DECLARE @KeyTable TABLE (Keyword VARCHAR(MAX))
INSERT INTO @KeyTable
SELECT @Combined
IF OBJECT_ID('tempdb..#Temp') IS NOT NULL
DROP TABLE #Temp
SELECT LTRIM(RTRIM(m.n.value('.[1]','varchar(8000)'))) AS Keyword
INTO #Temp
FROM
(
SELECT CAST('<XMLRoot><RowData>' + REPLACE(Keyword,',','</RowData><RowData>') + '</RowData></XMLRoot>' AS XML) AS x
FROM @KeyTable
)t
CROSS APPLY x.nodes('/XMLRoot/RowData')m(n);
SELECT * FROM #Temp
So I added some code to replace the ampersand with &
and I get a semicolon error:
DECLARE @Combined VARCHAR(MAX)
SET @Combined = 'mac & cheese'
SET @Combined = REPLACE(@Combined, '&', '&')
DECLARE @KeyTable TABLE (Keyword VARCHAR(MAX))
INSERT INTO @KeyTable
SELECT @Combined;
IF OBJECT_ID('tempdb..#Temp') IS NOT NULL
DROP TABLE #Temp
SELECT LTRIM(RTRIM(m.n.value('.[1]','varchar(8000)'))) AS Keyword
INTO #Temp
FROM
(
SELECT CAST('<XMLRoot><RowData>' + REPLACE(Keyword,',','</RowData><RowData>') + '</RowData></XMLRoot>' AS XML) AS x
FROM @KeyTable
)t
CROSS APPLY x.nodes('/XMLRoot/RowData')m(n);
SELECT * FROM #Temp
I've attempted moving the semicolon to various locations and even adding more but it just won't work. Any help anyone can provide would be appreciated!