Setting a table variable from a function in SQL Server

Posted on 2008-10-11
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

ALTER FUNCTION [dbo].[fnDStringToTable]


	  @list VARCHAR(4000)

	, @delimiter NCHAR(1) = ',' --Defaults to CSV



@tableList TABLE(

	value VARCHAR(100)




	DECLARE @value    NVARCHAR(100)

	DECLARE @position INT

	SET @list = LTRIM(RTRIM(@list))+ ','

	SET @position = CHARINDEX(@delimiter, @list, 1)

	IF REPLACE(@list, @delimiter, '') <> ''


		WHILE @position > 0


			SET @value = LTRIM(RTRIM(LEFT(@list, @position - 1)))

			IF @value <> ''


				INSERT INTO @tableList (value) 

				VALUES (@value)


			SET @list = RIGHT(@list, LEN(@list) - @position)

			SET @position = CHARINDEX(@delimiter, @list, 1)





Open in new window

Question by:nkewney
LVL 14

Accepted Solution

Binuth earned 400 total points
Comment Utility
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

LVL 59

Assisted Solution

by:Kevin Cross
Kevin Cross earned 100 total points
Comment Utility
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. :)

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

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

18 Experts available now in Live!

Get 1:1 Help Now