14

I have json data which have a list of productDetails. I need to combine each of the detail with productId.

I am using a stored procedure. This is my code:

DECLARE @products AS TABLE(productName NVARCHAR(255), 
                           productId INT NOT NULL);

INSERT INTO @products  
    EXEC [saveproducts] @product;

From the above query, I will get the list of products as a table. Now I need to join the detail in the json with corresponding product

INSERT INTO @detail 
     SELECT
         [detaiId], [productId]
     FROM 
         OPENJSON(@detailJSON) 
     WITH(detailId UNIQUEIDENTIFIER 'strict $.detaiId',
       productId INT ??????);

How to get the productId by comparing the productName in @detailJSON and from table @products?

2
  • Can you provide JSON sample (structure) and productDetails schema Commented Jan 2, 2017 at 11:30
  • 1
    @Habeeb.. I have solved it actually only a simle inner join is needed. Thanks for the edit and responce Commented Jan 2, 2017 at 12:03

1 Answer 1

14

Only a simple inner join is needed

 INSERT INTO @detail 
 SELECT
     J.[detaiId], P.[productId]
 FROM 
 @products P
 INNER JOIN
     OPENJSON(@detailJSON) 
 WITH(detailId UNIQUEIDENTIFIER 'strict $.detaiId',
      productName  NVARCHAR(255) 'strict $.productName'
   ) J 
 ON J.productName = P.productName 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.