Edit for Ewan My stored proc's: for main/parent object:
ALTER PROCEDURE [dbo].[InsertUpdateParent]
(
@code nvarchar(max),
@name nvarchar(max),
@lat float,
@lng float,
@country int,
@countryCode nvarchar(2)
)
AS
BEGIN
DECLARE @id int;
DECLARE @err int;
DECLARE @newObject bit;
SET NOCOUNT ON
IF (@country = 0)
begin
select @country = id from countries where code = @countrycode;
SELECT @err = @@error;
end
IF EXISTS (SELECT ID FROM ParentObject where code = @code)
begin
SELECT @id = ID FROM ParentObject where code = @code;
SELECT @err = @@error;
SELECT @newObject = 0;
UPDATE ParentObject SET
[Name] = @name,
Latitude = @lat,
Longitude = @lng,
CountryId = @country
WHERE code = @code;
SELECT @err = @@error;
end
else
begin
SELECT @newObject = 1;
INSERT INTO [dbo].[ParentObject]
([Code]
,[Name]
,[Latitude]
,[Longitude]
,[CountryId]
VALUES
(@code, @name, @lat, @lng, @country);
select @id = SCOPE_IDENTITY();
SELECT @err = @@error;
end
IF @err <> 0
PRINT '@err is ' + ltrim(str(@err)) + '.'
else
return @id;
END
and for prices:
ALTER PROCEDURE [dbo].[InsertUpdatePrices]
(
@from as datetime,
@to as datetime,
@parentId as int,
@price as decimal(18,2)
)
AS
BEGIN
declare @id int;
SET NOCOUNT ON
IF EXISTS (SELECT ID FROM prices where ArrivalDateFrom = @from and ArrivalDateTo= @to and ParentObjectId = @parentId )
begin
SELECT @id = ID FROM prices where ArrivalDateFrom = @from and ArrivalDateTo= @to and ParentObjectId = @parentId;
UPDATE [dbo].[Prices]
SET
[ArrivalDateFrom] = @from,
[ArrivalDateTo] = @to,
[Price] = @price,
[ParentObjectId] = @parentId
WHERE ID = @id
end
else
begin
INSERT INTO [dbo].[Prices]
([ArrivalDateFrom]
,[ArrivalDateTo]
,[Price]
,[ParentObjectId])
VALUES
(@from
,@to
,@price
,@parentId);
select @id = SCOPE_IDENTITY();
end
return @id;
END