Group,
Passing inline values to a udf is straightforward . However, how or is it
possible to pass a column from the select of one table into a udf that
returns a table variable in a join to the original table. The goal is to
explode the number of rows in the result set out based on the result of the
udf. Although the example I am providing here is simplified, we are trying
to parse out multiple values out of a text column and using a select into so
the result will be one row for each of row in the original table compounded
by the number of occurrences in the text field.
(I know bad table design but that's out of my control)
Thanks,
Ray
create table TableOne
(
Col1 int,
Col2 char(5)
)
go
insert TableOne values (1, 'One')
insert TableOne values (2, 'Two')
insert TableOne values (3, 'Three')
go
Create Function dbo.udfTableTwo
(@Id int)
RETURNS @tbl TABLE (TID int, TChar char(1))
AS
BEGIN
Declare @test int
set @test = @Id
While @test >= 0
Begin
INSERT @tbl VALUES (@Id, Char(65+@test))
set @test = @test - 1
End
RETURN
END
--works
select a.*, b.* from TableOne a join dbo.udfTableTwo (2) b
on a.col1=b.TID
--Fails with Line 1: Incorrect syntax near '.'.
select a.*, b.* from TableOne a join dbo.udfTableTwo (a.col1) b
on a.col1=b.TID
drop table TableOne
go
drop function dbo.udfTableTwo
go
Passing inline values to a udf is straightforward . However, how or is it
possible to pass a column from the select of one table into a udf that
returns a table variable in a join to the original table. The goal is to
explode the number of rows in the result set out based on the result of the
udf. Although the example I am providing here is simplified, we are trying
to parse out multiple values out of a text column and using a select into so
the result will be one row for each of row in the original table compounded
by the number of occurrences in the text field.
(I know bad table design but that's out of my control)
Thanks,
Ray
create table TableOne
(
Col1 int,
Col2 char(5)
)
go
insert TableOne values (1, 'One')
insert TableOne values (2, 'Two')
insert TableOne values (3, 'Three')
go
Create Function dbo.udfTableTwo
(@Id int)
RETURNS @tbl TABLE (TID int, TChar char(1))
AS
BEGIN
Declare @test int
set @test = @Id
While @test >= 0
Begin
INSERT @tbl VALUES (@Id, Char(65+@test))
set @test = @test - 1
End
RETURN
END
--works
select a.*, b.* from TableOne a join dbo.udfTableTwo (2) b
on a.col1=b.TID
--Fails with Line 1: Incorrect syntax near '.'.
select a.*, b.* from TableOne a join dbo.udfTableTwo (a.col1) b
on a.col1=b.TID
drop table TableOne
go
drop function dbo.udfTableTwo
go
Comment