Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Setting a table variable from a function in SQL Server

Posted on 2008-10-11
2
Medium Priority
?
572 Views
Last Modified: 2012-05-05
Dear Experts,

I have a function in my database (code snippet below) which returns type table)

I'm trying to set this table as a variable in my stored procedure by calling something like the following:

DECLARE @tblMain table
SET @tblMain = [dbo].[fnDStringToTable](@Original_Text, ' ')

This doesn't seem to work and  the following is returned...

Msg 156, Level 15, State 1, Procedure procTranslate, Line 19
Incorrect syntax near the keyword 'SET'.
Msg 137, Level 15, State 1, Procedure procTranslate, Line 19
Must declare the scalar variable "@tblMain".

Could anybody tell me where I'm going wrong?

Thanks in advance

Nick
ALTER FUNCTION [dbo].[fnDStringToTable]
(
	  @list VARCHAR(4000)
	, @delimiter NCHAR(1) = ',' --Defaults to CSV
)
RETURNS 
@tableList TABLE(
	value VARCHAR(100)
	)
AS
BEGIN
	DECLARE @value    NVARCHAR(100)
	DECLARE @position INT
 
	SET @list = LTRIM(RTRIM(@list))+ ','
	SET @position = CHARINDEX(@delimiter, @list, 1)
 
	IF REPLACE(@list, @delimiter, '') <> ''
	BEGIN
		WHILE @position > 0
		BEGIN
			SET @value = LTRIM(RTRIM(LEFT(@list, @position - 1)))
			IF @value <> ''
			BEGIN
				INSERT INTO @tableList (value) 
				VALUES (@value)
			END
			SET @list = RIGHT(@list, LEN(@list) - @position)
			SET @position = CHARINDEX(@delimiter, @list, 1)
 
		END
	END	
	RETURN
END

Open in new window

0
Comment
Question by:nkewney
[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 14

Accepted Solution

by:
Binuth earned 1600 total points
ID: 22693424
try this
declare @tblMain as table(val varchar(100))
insert into @tblMain
select * from dbo.fnDStringToTable('1,2,3',',')
select * from @tblMain

Open in new window

0
 
LVL 60

Assisted Solution

by:Kevin Cross
Kevin Cross earned 400 total points
ID: 22695319
Binuth's answer is correct.

But just a suggestion, but you are already going through all this trouble in your function, you can just simply use the function as a table as Binuth is also illustrating and skip the variable.

select * from dbo.fnDStringToTable('1,2,3',',')

Gets you same results as above. :)
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

721 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