0
votes

Currently running the following application, however I require some assistance as I'm fairly new to SQL. What I'm looking to achieve here is to have my records look at the @Pid value and if this value already exists then to update it, otherwise to insert it. I've currently got the insert method outlined with some help already from the community here, but I could use a pointer on how to update this SQL script.

 private static async void UpdateStreetWebApiProperties()
   {
      
       var client = new HttpClient();
       var request = new HttpRequestMessage
       {
           Method = HttpMethod.Get,
           RequestUri = new Uri("https://inventorymanchester.co.uk/api/property-feed/sales/search"),
           Headers =
{
    { "ContentType", "application/json" },
    { "Authorization", "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJhdWQiOiIxIiwianRpIjoiNzU5YTJlZjZmYjY0MWE3NjRiZGE5ZjdmYzk2ZDNkMjAwN2IwNTk1MTMyZWJlYjhjMmQ0MTUyZDZlODcwZGI4ODRiOTFmMWEzMGE5NDA0ZWEiLCJpYXQiOjE2NTU3MzQzNjAuNDA5OTY1LCJuYmYiOjE2NTU3MzQzNjAuNDA5OTcxLCJleHAiOjE5NzEzNTM1NjAuMTE2MzYzLCJzdWIiOiIxMzM0MjIiLCJzY29wZXMiOlsicHVibGljLWFwaSJdfQ.kxo8GVwKFRUgfyhrRqUrh6bQvJvv5PfJgken90RulA_hTKuX4zWPs6fZ6RkljSK1ECsPYfeNKF5Z_E-xYekRWtDKOcWCXp-wKKNfHWA7rwVU1NzzeghszqcQ0ojLqcqiJ_sxFXCyouvP6LW1-jlcWmYUaHrBu2rajSSyo-K3RN_uFq85Fc5_XkCAvTzO4U59ZcNU4nhRvj3SG2b4l0kZPGlTePkJafciToZ-Q618DYDtO1Q63hUY6X6CunjIIKXiw8h1o9qG9vlmV60JBuVZ4COzc05H-e4UrA3tYkxa6O6xi3baczCZFr97bTIwXMRlifUh8E6WS29T_2uIjSl5OIcaXLuDhrxYOUrfTsFuGU2SuwKKflps0jAUir1s_T_Fi4sPiC5BqvvbrPSBHXlNQpguIN6nYCK4FXW2ixALjs5e2JKN5gMmOCFeqzEmZJAkrHR0iN7n-UbWwsX7dh8ODss3XprckzadC-f_EHiY4ZDSdKPPnJTukkA0V-ezafoO-hBPeiiPOo-IH3kqCyElUMv2sosV4yaFT8DhFlrivCAktwXz4vkKTUYhjuueWU-QxX1y2MwE0zT32SKb21Bwp4Z7nJ0Fa84_CglbWWZZVglJh6Rrgmmg17DgxpSJx3qCiqqum3UrKGTYuLTube47zjumFQeyy1cYUWmqcRG84OQ" },
},
       };

    using (var response = await client.SendAsync(request))
       {
           
           response.EnsureSuccessStatusCode();
           
           
           if (response.IsSuccessStatusCode)
           {
               var properties = await response.Content.ReadAsStringAsync();  
               string strBufJSON = properties;
               string strBufJSON2 = properties;
               
               // above is web service results
          

               JsonModel.JSONModel.Rootobject jData;
         
          
               
               jData = JsonConvert.DeserializeObject<JsonModel.JSONModel.Rootobject>(strBufJSON);
     
          
             foreach (var data in jData.data)
               {
                  
                   string strSQL
                 = @"INSERT into [Tortoise_Street_Properties]
                (Pid, bedrooms, postcode, salestatus) VALUES(@Pid, @bedrooms, @postcode, @salestatus)";


                   using (SqlConnection conn = new SqlConnection("Data Source=tortoise-final.c4u53gizwfmz.eu-west-1.rds.amazonaws.com,1433;Initial Catalog=TortoiseDB;User Id=tortoise_admin;Password=Casper34;"))
                   {
                       using (SqlCommand cmdSQL = new SqlCommand(strSQL, conn))
                       {
                       
                           cmdSQL.Parameters.Add("@Pid", SqlDbType.NVarChar).Value = data.id;
                           cmdSQL.Parameters.Add("@bedrooms", SqlDbType.NVarChar).Value = data.attributes.bedrooms;
                           cmdSQL.Parameters.Add("@postcode", SqlDbType.NVarChar).Value = data.attributes.postcode;
                           cmdSQL.Parameters.Add("@salestatus", SqlDbType.NVarChar).Value = data.attributes.sale_status;


                           conn.Open();
                           cmdSQL.ExecuteNonQuery();
                       }
                   }
               
               
               }
             
           }
           else
           {

               Console.WriteLine("Could not get branch properties");
           }


       }

My SQL table exsits as follows:

USE [TortoiseDB]
GO

/****** Object:  Table [dbo].[Tortoise_Street_Properties]    Script Date: 28-Jul-22 
5:09:00 PM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[Tortoise_Street_Properties](
[id] [int] IDENTITY(1,1) NOT NULL,
[Pid] [varchar](255) NULL,
[inlineaddress] [varchar](255) NULL,
[postcode] [varchar](20) NULL,
[description] [varchar](255) NULL,
[bedrooms] [varchar](255) NULL,
[price] [varchar](255) NULL,
[salestatus] [varchar](255) NULL,
[brochure] [varchar](255) NULL,
[longitude] [int] NULL,
[latitude] [int] NULL,
[virtualtour] [varchar](255) NULL,
PRIMARY KEY CLUSTERED 
(
[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

I appreciate all your help!

See also sqlperformance.com/2020/09/locking/upsert-anti-pattern. And consider using a Table Valued Parameter to do a bulk insert - Charlieface