1
2
3
4
5
6
7
8
| DECLARE @string varchar(50), @execSql varchar(1000)
SET @string = 'I like kittens'
SET @execSql = 'INSERT INTO table (column) SELECT '''
SET @execSql = @execSql + REPLACE(@string, ' ', ''' UNION ALL SELECT ''')
SET @execSql = @execSql + ''''
EXEC (@execSql) |
DECLARE @string varchar(50), @execSql varchar(1000)
SET @string = 'I like kittens'
SET @execSql = 'INSERT INTO table (column) SELECT '''
SET @execSql = @execSql + REPLACE(@string, ' ', ''' UNION ALL SELECT ''')
SET @execSql = @execSql + ''''
EXEC (@execSql)
The snippet above will prepare a statement for insertion and replace the space in the string to be split with UNION ALL SELECT
.
INSERT INTO table (column) SELECT 'I' UNION ALL SELECT 'like' UNION ALL SELECT 'kittens' |
INSERT INTO table (column) SELECT 'I' UNION ALL SELECT 'like' UNION ALL SELECT 'kittens'
Source