Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
171 views
in Technique[技术] by (71.8m points)

Insert data from Json table to the view that has been already created with exec in SQL

I have a problem, I have to insert data from a Json that stored in a table that has jus one field then insert them directly to the view but my code has a error and I can't fix that. My code is this(but it has error):

CREATE PROC [dbo].[viewJson]
AS
BEGIN

DECLARE @OpenJson NVARCHAR(MAX) = (SELECT * FROM FoodSara_tbl FOR JSON AUTO) -- Make Json from Original Table

SELECT * 
FROM OPENJSON(@OpenJson)

WITH (FID INT,
Fname NVARCHAR(50),
Ftype NVARCHAR(20),
Fcount INT,
Fdate VARCHAR(400),
Fregion NVARCHAR(50),
Fdescription NVARCHAR(100)) AS SM


EXEC('CREATE VIEW Json1 AS SELECT * FROM SM')

END

my first table that has Json data:

|  JsonOutPut                                                                   |
|-------------------------------------------------------------------------------|
|[{"FID":1,"Fname":"cake","Fcount":5,"FDate":"2020-02-13","Fregion":"UK"},...]  |

my view should be like this:

| FID | Fname | Fcount| FDate    | Fregion |
|-----|-------|-------|----------|---------|
|  1  | cake  |   5   |2020-02-13|    UK   |

I don't want create new table!

I have an eeror: Incorrect syntax near 'Json1'.

question from:https://stackoverflow.com/questions/65643703/insert-data-from-json-table-to-the-view-that-has-been-already-created-with-exec

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...