Ishika Tiwari

Ishika Tiwari

  • 315
  • 5.4k
  • 282.9k

Is it possible to bind HTML templates without using a loop in sql

Oct 17 2023 4:47 AM

Is it possible to bind HTML templates with JSON data dynamically without using a loop in sql server?

DECLARE @json NVARCHAR(MAX) = '[{"name": "John Doe", "age": 30}, {"name": "Jane Smith", "age": 25}, {"name": "Bob Johnson", "age": 35}]'

DECLARE @htmlTemplate NVARCHAR(MAX)
SET @htmlTemplate = N'

  <h1>Name: ' + (SELECT JSON_VALUE(@json, '$[0].name')) + '</h1>
  <p>Age: ' + (SELECT JSON_VALUE(@json, '$[0].age')) + '</p>
'

-- Insert the HTML template into the 'templatedemo' table
INSERT INTO templatedemo (template) VALUES (@htmlTemplate)

 


Answers (2)