How to add mapped to in table trade codes where mapped from exist ?

ahmed salah 3,216 Reputation points
2021-02-01T21:54:04.517+00:00

I work on SQL server 2012 I face issue I can't insert mapped to into table trade codes where mapped from exist
on table trade code

so

firstly I will get MapCodeTypeFrom and MapCodeValueFrom from table settings

second i will search on table tradecodes for MapCodeTypeFrom and MapCodeValueFrom

if MapCodeTypeFrom and MapCodeValueFrom exist on table trade Codes

then add MapCodeTypeTo and MapCodeValueTo for every part

that meaning every part have MapCodeTypeFrom and MapCodeValueFrom on table TradeCodes
must have MapCodeTypeTo and MapCodeValueTo

with another meaning every part must have two rows
first row for MapCodeTypeFrom and MapCodeValueFrom
second row for MapCodeTypeTo and MapCodeValueTo

create table #settings
(
MapId int,
MapCodeTypeFrom nvarchar(50),
MapCodeValueFrom int,
MapCodeTypeTo nvarchar(50),
MapCodeValueTo int
)
insert into #settings(MapId,MapCodeTypeFrom,MapCodeValueFrom,MapCodeTypeTo,MapCodeValueTo)
values
(1222,'ECCN',9910,'HTS',9920),
(1222,'Hom',9950,'VOM',9960)

--DROP TABLE #TradeCodes
create table #TradeCodes
(
PartId  int,
CodeType nvarchar(50),
Code   int,
)
insert into #TradeCodes(PartId,CodeType,Code)
values
(1334,'ECCN',9910),
(1971,'ECCN',9910),
(2050,'ECCN',9910),
(3000,'VOM',9950),
(3600,'VOM',9950),
(3700,'VOM',9950) 

final rows must added on table trade codes that represent mapped to is :

PartId CodeType Code
1334 HTS 9920
1971 HTS 9920
2050 HTS 9920
3000 VOM 9960
3600 VOM 9960
3700 VOM 9960

mapped to will added in case of mapped from code type and value Exist
on table trade codes

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
14,135 questions
Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,665 questions
0 comments No comments
{count} votes

Accepted answer
  1. Guoxiong 8,206 Reputation points
    2021-02-01T22:44:41.42+00:00

    Should the CodeType is "Hom" instead of "VOM" for PartId 3000, 3600 and 3700?

    INSERT INTO #TradeCodes
    SELECT t.PartId, s.MapCodeTypeTo, s.MapCodeValueTo
    FROM #settings AS s
    INNER JOIN #TradeCodes AS t ON t.CodeType = s.MapCodeTypeFrom AND t.Code = s.MapCodeValueFrom;
    
    SELECT * FROM #TradeCodes;
    
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. ahmed salah 3,216 Reputation points
    2021-02-02T01:09:06.257+00:00

    thank you for reply
    this will give me wrong result
    i need to insert the following result :

    PartId CodeType Code
    1334 HTS 9920
    1971 HTS 9920
    2050 HTS 9920
    3000 VOM 9960
    3600 VOM 9960
    3700 VOM 9960
    

    so i need to add mapped code type to and mapped code value to

    in case of mapped code type from and mapped code type to exist

    on table trade codes above i already have mapped code type from and mapped code value from

    so i need to add mapped code type to and mapped code value to

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.