Question

I have a stored procedure in SQL Server 2008R2 that takes two user defined table types as parameters. Each of these types is a simple table holding an series of Ids:

CREATE TYPE [dbo].[Ids] AS TABLE(
    [Id] [int] NULL
)

I want to combine the two parameters passed in to achieve the following result:

DECLARE 
@Table1 TABLE (Id INT )

DECLARE
@Table2 TABLE (Id INT )


INSERT INTO @Table1 VALUES (1)
INSERT INTO @Table1 VALUES (2)

INSERT INTO @Table2 VALUES (11)
INSERT INTO @Table2 VALUES (22)


SELECT * FROM @Table1
SELECT * FROM @Table2


DECLARE
@Combined TABLE (T1 INT, T2 INT)

-- TODO: Magically combine the two tables

SELECT * FROM @Combined

-- Output would be the following

1, 11
1, 22
2, 11
2, 22
Was it helpful?

Solution

You seem to want a cross join:

insert into @Combined(t1, t2)
    select t1.id, t2.id
    from @Table1 t1 cross join
         @Table2 t2;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top