Snake Eyes
Snake Eyes

Reputation: 16764

Generate insert statements after splitting in a stored procedure

I have a stored procedure which accept a parameter of type VARCHAR(MAX). That parameter has string separated by commas like

@test = 'test123,test456,test789';

Based on that parameter, I want to generate multiple insert statements.

I'll use the split function defined in this question: Split string by comma in SQL Server 2008.

Can you show me an example how to generate inserts after splitting a string by commas ?

The logic steps should be like:

@test = 'test123,test456,test789';
split @test
use while or cursor ? (I don't know)
INSERT INTO X values ('test123')
INSERT INTO X values ('test456')
...

Upvotes: 1

Views: 1530

Answers (4)

Oleksandr Fedorenko
Oleksandr Fedorenko

Reputation: 16904

You can dynamically create a SQL statement on the fly and then run that command.Actually loop it isn't necessary to you. This script provide multiple inserting in one INSERT statement

DECLARE @test nvarchar(100) = 'test123,test456,test789';  
DECLARE @dml nvarchar(max) = N''
SET @dml = 'INSERT [dbo].[test2] VALUES' + '(''' + REPLACE(@test, ',', '''),(''') + ''')'
EXEC sp_executesql @dml

Also you can use option with dynamic management function sys.dm_fts_parser

SELECT FULLTEXTSERVICEPROPERTY ('IsFulltextInstalled')

0 = Full-text is not installed. 1 = Full-text is installed. NULL = Invalid input, or error.

If 0 = Full-text is not installed then this post is necessary to you How to install fulltext on sql server 2008?

DECLARE @test nvarchar(100) = 'test123,test456,test789';
INSERT [dbo].[test2]
SELECT display_term
FROM sys.dm_fts_parser('"' + @test + '"', 1033, NULL, 0)

Upvotes: 0

Pandian
Pandian

Reputation: 9126

Try like below it will help you...

Here you don't need any user defined function.... You just use the below code and get your result...

DECLARE @test NVARCHAR(MAX)
SET @test = 'test123,test456,test789'
SET @test = ',' + @test + ','
DECLARE @Part NVARCHAR(MAX)
DECLARE @INDEX    INT
SET @INDEX = CHARINDEX(',',@test)
DECLARE @EIND INT set @EIND = 0

WHILE(@INDEX != LEN(@test))
BEGIN
    SET  @EIND = ISNULL(((CHARINDEX(',', @test, @INDEX + 1)) - @INDEX - 1), 0)

    INSERT INTO X SELECT (SUBSTRING(@test, (@INDEX  + 1),  @EIND)) -- Insert Comes Here

    SELECT @INDEX = ISNULL(CHARINDEX(',', @test, @INDEX + 1), 0)
END

Upvotes: 1

Iswanto San
Iswanto San

Reputation: 18569

This query will split the string, then loop the result to construct the insert comment.

declare @test varchar(30) = 'test123,test456,test789';

-- retrieve number of comma(s) in @test
declare @i int = len(@test) - len(replace(@test,',','')) + 1
declare @cmd nvarchar(255)

-- loop
while @i >= 0
begin
    select @cmd = 'INSERT INTO X VALUES (''' +  parsename(replace(@test, ',', '.'), @i) + ''');';
    -- execute sql
    exec sp_executesql @cmd
    set @i = @i - 1
end

Upvotes: -1

Prashant16
Prashant16

Reputation: 1526

DECLARE @id VARCHAR(MAX)

SET @id = 'test123,test456,test789,'

WHILE CHARINDEX(',', @id) > 0 
BEGIN

DECLARE @tmpstr VARCHAR(50)
 SET @tmpstr = SUBSTRING(@id, 1, ( CHARINDEX(',', @id) - 1 ))

INSERT  INTO X

VALUES  ( 
          @tmpstr
        )
SET @id = SUBSTRING(@id, CHARINDEX(',', @id) + 1, LEN(@id))
END

Upvotes: 2

Related Questions