I’m creating a stored procedure to add in a table the data from a json. The table has an ID
defined as uniqueidentifier
and a CreatedAt
defined as datetime
.
If I try to run this script without ID
andor CreatedAt
, I get this error
Column name or number of supplied values does not match table definition.
An example of the script is the following:
<code>INSERT INTO [RequestsDetails]
SELECT *
FROM OPENJSON(@json)
WITH (
RequestId uniqueidentifier '$.RequestId',
QuoteId uniqueidentifier '$.QuoteId',
SiteId int '$.SiteId',
SumBuilding money '$.SumBuilding'
)
</code>
<code>INSERT INTO [RequestsDetails]
SELECT *
FROM OPENJSON(@json)
WITH (
RequestId uniqueidentifier '$.RequestId',
QuoteId uniqueidentifier '$.QuoteId',
SiteId int '$.SiteId',
SumBuilding money '$.SumBuilding'
)
</code>
INSERT INTO [RequestsDetails]
SELECT *
FROM OPENJSON(@json)
WITH (
RequestId uniqueidentifier '$.RequestId',
QuoteId uniqueidentifier '$.QuoteId',
SiteId int '$.SiteId',
SumBuilding money '$.SumBuilding'
)
If I add the line using NEWID()
for the ID
and GETDATE()
for the CreatedAt
, I get the error
Incorrect syntax near
NEWID
and this is an example of the script
<code>INSERT INTO [RequestsDetails]
SELECT *
FROM OPENJSON(@json)
WITH (
Id uniqueidentifier NEWID(),
RequestId uniqueidentifier '$.RequestId',
QuoteId uniqueidentifier '$.QuoteId',
SiteId int '$.SiteId',
SumBuilding money '$.SumBuilding',
CreatedAt datetime GETDATE()
)
</code>
<code>INSERT INTO [RequestsDetails]
SELECT *
FROM OPENJSON(@json)
WITH (
Id uniqueidentifier NEWID(),
RequestId uniqueidentifier '$.RequestId',
QuoteId uniqueidentifier '$.QuoteId',
SiteId int '$.SiteId',
SumBuilding money '$.SumBuilding',
CreatedAt datetime GETDATE()
)
</code>
INSERT INTO [RequestsDetails]
SELECT *
FROM OPENJSON(@json)
WITH (
Id uniqueidentifier NEWID(),
RequestId uniqueidentifier '$.RequestId',
QuoteId uniqueidentifier '$.QuoteId',
SiteId int '$.SiteId',
SumBuilding money '$.SumBuilding',
CreatedAt datetime GETDATE()
)
How can I add those values that are not present in the json?
1