Solved

How to insert comma separated values in table

Posted on 2013-12-31
2
4,158 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

     When we have to pass multiple rows of data to SQL Server, the developers either have to send one row at a time or come up with other workarounds to meet requirements like using XML to pass data, which is complex and tedious to use. There is a …
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now