Check If record Exists in table or not in SQL Server

hamzashah5
 
on Feb 25, 2021 03:13 AM
644 Views

I'm working on a project where I've made a Store procedure to load Bulk of customers data in CustomersInfo table.

There is customer's City Id in a table.

So i want when there is new city of customer then new city will added to City table.

I'm not getting the logic to do that.

Can anyone please help me?

Here's my Store Procedure

alter procedure sp_AddCustomerBulk
    @CompanyId int,
    @jsonstring nvarchar(max)
as begin
    select * from OPENJSON(@jsonstring)
    WITH (MemberId int,CompanyId int,ShopId int, MemberName nvarchar(500), CityName nvarchar(50), CityAreaName nvarchar(50), Email nvarchar(50), PhoneResident nvarchar(25), PhoneOffice nvarchar(25), PhoneMobile nvarchar(25), Comments nvarchar(500) , MemberDOB datetime , MemberAnniversary datetime , CNIC nvarchar(20) , Gender nvarchar(10), CreditLimit numeric(18,4) , AllowCredit bit , MemberAddress nvarchar(500) , MemberTypeName nvarchar(50) , OpeningBalance money) 
end

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Feb 25, 2021 07:10 AM

Hi hamzashah5,

Check below query.

CREATE PROCEDURE sp_AddCity
    @CityId INT
AS BEGIN
    IF NOT EXISTS (SELECT CityId FROM Cities WHERE CityId = @CityId)
    BEGIN
        -- INSERT query.
        INSERT INTO City (CityId) VALUES (@CityId) 
    END
END