If I have a table that has data like:
Test1
Test2
Test3
Test4
Test5
And another table with data like:
Foo1
Foo2
Foo3
How can I join the latter with the former like:
Test1 Foo1
Test2 Foo2
Test3 Foo3
Test4 Foo1
Test5 Foo2
Essentially repeating the same sequence from the second table for all of the first table.
EDIT:
I also need the data in the second table to stay in that order (it’s coming from a table variable), even if they are not naturally in order alphabetically.
So, if the second table looks like:
Foo2
Foo3
Foo1
The result should look like:
Test1 Foo2
Test2 Foo3
Test3 Foo1
Test4 Foo2
Test5 Foo3
EDIT2:
The data for the second table is coming from the following TVF. I’m splitting a string that looks like “Foo1,Foo2,Foo3”. How would I add a sequence to the result of that split?
CREATE FUNCTION [dbo].[Split]
(
@Data varchar(max),
@Delimiter varchar(max)
)
RETURNS @Tokens table
(
Token varchar(max)
)
AS
BEGIN
while (charindex(@Delimiter, @Data) > 0)
begin
insert into @Tokens (Token)
select
ltrim(rtrim(substring(@Data,1,charindex(@Delimiter,@Data)-1)))
select
@Data = substring(
@Data,charindex(@Delimiter,@Data)+len(@Delimiter),len(@Data))
end
insert into @Tokens (Token)
select
ltrim(rtrim(@Data))
return
Update
To get a sequence from your split function you can add a identity field in the returned table.