CREATE OR ALTER PROCEDURE dbo.USP_CreateCity
(
    @CityName NVARCHAR(100),
    @StateProvinceCode NVARCHAR(6)
)
AS
BEGIN
    SET NOCOUNT ON;
    SET XACT_ABORT ON;

    DECLARE @StateProvinceId UNIQUEIDENTIFIER = dbo.UDF_GetStateProvinceIdByCode(@StateProvinceCode);
    IF @StateProvinceId IS NULL
    BEGIN
        RAISERROR('State/province not found for code.', 16, 1);
        RETURN;
    END

    IF EXISTS (
        SELECT 1
        FROM dbo.City
        WHERE CityName = @CityName
            AND StateProvinceID = @StateProvinceId
    )
        RETURN;

    INSERT INTO dbo.City
        (StateProvinceID, CityName)
    VALUES
        (@StateProvinceId, @CityName);
END;
