1

I have a table PRODUCT that is basically set up so there is an PRODUCTID, PRODUCTNAME... it looks sort of like this

PRODUCTID  PRODUCTNAME
100        PNAME1   
101        PNAME2         
102        PNAME3   

Now I have to insert a record into new table PRODUCTMAPPING for each row in the PRODUCT.

so my new table PRODUCTMAPPING should look like this

PRODUCTMAPPINGID  PRODUCTID
1                 100
1                 101
1                 102
2                 100
2                 101
2                 102
3                 100

and so on ....

I tried doing while but I need it using Join.

Can we acheive this using joins ?

Thanks in advance.

Alex K.
  • 171,639
  • 30
  • 264
  • 288
Santosh
  • 2,355
  • 10
  • 41
  • 64
  • 1
    How do you mapping your product ? What is the relation between these two ? – Mahesh Feb 26 '15 at 11:49
  • possible duplicate of [SQL Server FOR EACH Loop](http://stackoverflow.com/questions/10300414/sql-server-for-each-loop) – Rajesh Feb 26 '15 at 11:51
  • 2
    If you have your PRODUCTMAPPINGID values somewhere, you can just cross join that with PRODUCT – James Z Feb 26 '15 at 11:54

3 Answers3

1

One way;

select
    row_number() over(partition by a.PRODUCTID order by a.PRODUCTID) PRODUCTMAPPINGID,
    a.PRODUCTID
from PRODUCT a, PRODUCT b
Alex K.
  • 171,639
  • 30
  • 264
  • 288
0
  • Using LOOP

The following example specifies that the JOIN operation in the query is performed by a LOOP join.

Select sp.Name,spqh.quota 
FROM Sales.SalesPersonQuotaHistory AS spqh
    INNER LOOP JOIN Sales.SalesPerson AS sp
    ON spqh.SalesPersonID = sp.SalesPersonID
WHERE sp.SalesYTD > 2500000.00;
GO

Refer this MSDN link

Rajesh
  • 1,600
  • 5
  • 33
  • 59
0
INSERT
INTO    dbo.PRODUCTMAPPING
        (
        PRODUCTMAPPINGID
        ,PRODUCTID
        )

SELECT  pmv.PRODUCTMAPPINGID
        ,p.PRODUCTID
FROM    dbo.Product p
CROSS JOIN
        (
        SELECT  pm.ProductMappingID
        FROM    dbo.ProductMappingValues pmv -- WHERE DO THESE COME FROM?
        ) pmv
Pixelated
  • 1,531
  • 3
  • 15
  • 35