IF EXISTS(SELECT NAME FROM sysobjects s WHERE s.name='CastStringToTable') DROP function CastStringToTable go --select * from dbo.[CastStringToTable]('1,2,3,4,5,6') create function [dbo].[CastStringToTable] (@str as varchar(max)) returns @tbl table(ID int) as begin -- تبدیل رشته به تیبل declare @id as int declare @i as int while charindex(',', @str)>0 begin set @i=charindex(',', @str) set @id = cast(substring(@str, 1, @i - 1) as int) set @str = substring(@str, @i + 1, len(@str)) insert into @tbl select @id end insert into @tbl select cast(@str as int) return end