Creating a stored procedure which ingests data after reading a JSON string

好久不见. 提交于 2021-02-11 12:13:55

问题


I would like to create a stored procedure, in SQL Server, that can accept JSON data and upload it into a specified table into its relevant columns. For instance, take this JSON string:

[{"ID":5,"LastName":"Donalds","FirstName":"Dave","Age":23,"City":"London"}]

This above string can be produced with the following query:

SELECT 
5 as 'ID',
'Donalds' as 'LastName',
'Dave' as 'FirstName',
23 as 'Age',
'London' as 'City'
FOR JSON PATH

I've managed to write a script which can upload into my table called 'Persons'. My table has the following CREATE script:

CREATE TABLE [dbo].[Persons](
    [ID] [int] NOT NULL,
    [LastName] [varchar](255) NOT NULL,
    [FirstName] [varchar](255) NULL,
    [Age] [int] NULL,
    [City] [varchar](255) NULL
) ON [PRIMARY]
GO

I've managed to write a script that can upload the above JSON into my table but I would like to further automate it within a stored procedure. The script that uploads the above JSON is:

DECLARE @jsonVariable varchar(max);

Set @jsonVariable = N'[{"ID":5,"LastName":"Donalds","FirstName":"Dave","Age":23,"City":"London"}]';

INSERT INTO Persons Select * 
FROM OPENJSON (@jsonVariable, N'$')
  WITH (
    ID INT N'$.ID',
    LastName VARCHAR(50) N'$.LastName',
    FirstName VARCHAR(50) N'$.FirstName',
    Age INT N'$.Age',
    City VARCHAR(50) N'$.City'
  ) 

So my objective is to create a stored procedure which has the exec function along the lines of:

 EXEC InsertPersonsJSON 
'[{"ID":5,"LastName":"Donalds","FirstName":"Dave","Age":23,"City":"London"}]'

However, I'm unsure how to separate the variables within the steps of creating a procedure for JSON data.

My desired result is the last row in my table:

ID  LastName    FirstName   Age   City

5   Donalds     Dave        23    London

Thank you for your help and please let me know if my question requires further clarification! :)


回答1:


If I understand you correctly and you want to ... create a stored procedure, in SQL Server, that can accept JSON data and upload it into a specified table into its relevant columns, you are really close. You need to create this stored procedure and include your SQL statement:

Stored procedure:

CREATE PROCEDURE InsertPersonsJSON (
    @JsonData NVARCHAR(MAX)
)
AS
BEGIN
    DECLARE @err int

    INSERT INTO Persons (ID, LastName, FirstName, Age, City)
    SELECT ID, LastName, FirstName, Age, City
    FROM OPENJSON (@JsonData, N'$') WITH (
        ID INT N'$.ID',
        LastName VARCHAR(50) N'$.LastName',
        FirstName VARCHAR(50) N'$.FirstName',
        Age INT N'$.Age',
        City VARCHAR(50) N'$.City'
    )   

    SELECT @err = @@ERROR
    RETURN (@err)
END

Execution:

DECLARE @RC int
DECLARE @JsonData nvarchar(max)

SET @JsonData = N'[{"ID":5,"LastName":"Donalds","FirstName":"Dave","Age":23,"City":"London"}]'
EXECUTE @RC = InsertPersonsJSON @JsonData

IF @RC = 0 PRINT 'OK'
ELSE PRINT 'Error'


来源:https://stackoverflow.com/questions/62326689/creating-a-stored-procedure-which-ingests-data-after-reading-a-json-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!