Solved

How to insert comma separated values in table

Posted on 2013-12-31
2
4,640 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Disable TLS1.0 on Win 2012 server 7 118
SQL Server Shrink hurting performance? 4 49
SQL Select Query help 1 51
when sa SQL Login becomes locked 6 35
Audit has been really one of the more interesting, most useful, yet difficult to maintain topics in the history of SQL Server. In earlier versions of SQL people had very few options for auditing in SQL Server. It typically meant using SQL Trace …
In this article I will describe the Backup & Restore 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.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

710 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