Solved

How to insert comma separated values in table

Posted on 2013-12-31
2
4,363 Views
Last Modified: 2013-12-31
Dear Experts,
How can I insert them into the table


Declare @FeeType Varchar(100)
Set @FeeType='1,4,5'


INSERT Into #FeeTypeTemp(FeeType)
Select @FeeType
WITH
(
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)
GO
0
Comment
Question by:Mehram
2 Comments
 
LVL 12

Accepted Solution

by:
Habib Pourfard earned 500 total points
ID: 39749470
First create this function :
CREATE FUNCTION [dbo].[SplitIDs]
(
	@List varchar(MAX)
)
RETURNS 
@ParsedList table
(
	ID int
)
AS
BEGIN
	DECLARE @ID varchar(10), @Pos int

	SET @List = LTRIM(RTRIM(@List))+ ','
	SET @Pos = CHARINDEX(',', @List, 1)

	IF REPLACE(@List, ',', '') <> ''
	BEGIN
		WHILE @Pos > 0
		BEGIN
			SET @ID = LTRIM(RTRIM(LEFT(@List, @Pos - 1)))
			IF @ID <> ''
			BEGIN
				INSERT INTO @ParsedList (ID) 
				VALUES (CAST(@ID AS int))
			END
			SET @List = RIGHT(@List, LEN(@List) - @Pos)
			SET @Pos = CHARINDEX(',', @List, 1)

		END
	END	
	RETURN
END

Open in new window


Then:
DECLARE @FeeType VARCHAR(100)
SET @FeeType = '1,4,5'

CREATE TABLE #FeeTypeTemp(FeeType INT)

INSERT Into #FeeTypeTemp(FeeType)
SELECT * FROM dbo.SplitIDs(@FeeType)

Open in new window

0
 
LVL 12

Expert Comment

by:Harish Varghese
ID: 39749491
You can insert multiple records using single INSERT statement as 'INSERT INTO table (columns) VALUES (values1), (values2), (values3), (etc)'

Try below code:
--create table #FeeTypeTemp(feetype int)

declare @values varchar(8000), @sql varchar(8000)
Select @values = '10,20,30'

select @sql = 'Insert into #FeeTypeTemp (feetype) values ('
                        + REPLACE (@values, ',', '),(') + ')'
exec (@sql)
select * from #FeeTypeTemp

-Harish
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
sql Total query 2 28
Extract string portion 2 24
why can not I insert data into my table ,i don't have any primary key? 5 39
Webservices in T-SQL 3 31
This is basically a blog post I wrote recently. I've found that SARGability is poorly understood, and since many people don't read blogs, I figured I'd post it here as an article. SARGable is an adjective in SQL that means that an item can be fou…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question