Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

How to convert json to create delete statement from header table and footer?

$
0
0

problem

How to convert json to create delete statement  for sql query?

I work on asp.net core 2.1 and i create class library generate SQL statement to execute on another layer 

i have json string as following

{
   "master" : {
       table: "",
       fields : {},
       keys: {}
       
   },
   "details" : [
       {
           table: "",
           fields : {},
           keys: {}
          
       }
   ]
}


Example:
{
   "master" : {
       table: "master_table",
       fields : {
           "name" : "bar",
           "address" : "fleet street",
           "phone" : "555"
       },
       keys{
           "id" : 1,
           "branch_id" : 1 
       }
       
   },
   "details" : [
       {
           table: "detail1_table",
           keys{
               "id" : 1,
               "branch_id" : 1 ,
               "LineNumber" : 1
           },
           fields : {
               "ItemCode" : item-5050,
               "Quantity" : 10 ,
               "Price" : 50 ,
               "Total" : 500 
           }
       },
       {
           table: "detail1_table",
            keys{
               "id" : 1,
               "branch_id" : 1 ,
               "LineNumber" : 2
           },
           fields : {
               "ItemCode" : item-9050,
               "Quantity" : 5 ,
               "Price" : 20 ,
               "Total" : 100 
           }
       }
   ]
}

here on json i need to make function c sharp as class library convert json to delete query from two tables

table header

and

table footer

Result of code deletefrom master_table where id=1and branch_id=1deletefrom detail1_table where id=1and branch_id=1and Linenumber=1

so that how to convert json string above to sql delete query ?

but really the main goal is to create dynamic sql string query

by depending  keys and fields on two tables header and footer meaning

if i change data above then it will do another query with fields or keys changed

scripts table

CREATETABLE[dbo].[detail1_table]([id][int]NOTNULL,[branch_id][int]NOTNULL,[LineNumber][int]NOTNULL,[ItemCode][nvarchar](50)NULL,[Quantity][int]NULL,[Price][decimal](18,2)NULL,[Total][decimal](18,2)NULL,CONSTRAINT[PK_detail1_table]PRIMARYKEYCLUSTERED([id]ASC,[branch_id]ASC,[LineNumber]ASC)WITH(PAD_INDEX =OFF, STATISTICS_NORECOMPUTE =OFF, IGNORE_DUP_KEY =OFF, ALLOW_ROW_LOCKS =ON, ALLOW_PAGE_LOCKS =ON)ON[PRIMARY])ON[PRIMARY]

GO
/****** Object:  Table [dbo].[master_table]    Script Date: 11-08-2019 07:42:49 ص ******/SET ANSI_NULLS ON
GOSET QUOTED_IDENTIFIER ON
GOCREATETABLE[dbo].[master_table]([id][int]NOTNULL,[branch_id][int]NOTNULL,[name][nvarchar](50)NULL,[address][nvarchar](50)NULL,[phone][nvarchar](50)NULL,CONSTRAINT[PK_master_table]PRIMARYKEYCLUSTERED([id]ASC,[branch_id]ASC)WITH(PAD_INDEX =OFF, STATISTICS_NORECOMPUTE =OFF, IGNORE_DUP_KEY =OFF, ALLOW_ROW_LOCKS =ON, ALLOW_PAGE_LOCKS =ON)ON[PRIMARY])ON[PRIMARY]

GO
INSERT[dbo].[detail1_table]([id],[branch_id],[LineNumber],[ItemCode],[Quantity],[Price],[Total])VALUES(1,1,1, N'item-500',10, CAST(50.00AS Decimal(18,2)), CAST(500.00AS Decimal(18,2)))INSERT[dbo].[detail1_table]([id],[branch_id],[LineNumber],[ItemCode],[Quantity],[Price],[Total])VALUES(1,1,2, N'item-500',5, CAST(50.00AS Decimal(18,2)), CAST(250.00AS Decimal(18,2)))INSERT[dbo].[master_table]([id],[branch_id],[name],[address],[phone])VALUES(1,1, N'michel plater', N'francw', N'1222222')INSERT[dbo].[master_table]([id],[branch_id],[name],[address],[phone])VALUES(2,1, N'sergio ram', N'german', N'1221111')

How to generate delete sql query to two tables ?


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>