SELECT COUNT(Field1_)
FROM
(
SELECT Field1_
FROM Table1_
WHERE Field1_= @Field1
UNION ALL
SELECT Field1_
FROM Table2_
WHERE DeliveryPoint_ = @Field1
UNION ALL
SELECT Table3_
FROM GasSupplied_
WHERE DeliveryPoint_ = @Field1
UNION ALL
SELECT Table4_
FROM Gnsnominations_
WHERE DeliveryPoint_ = @Field1
UNION ALL
SELECT Table5_
FROM HourlyProfileReports_
WHERE DeliveryPoint_ = @Field1
)
And I have problems with this code. Please help me.
DECLARE
CountedRows NUMBER;
BEGIN
SELECT COUNT(*) INTO CountedRows
FROM Profiles_
WHERE Field1_ = @Param1 AND RowNum < 2;
IF(CountedRows > 0)
THEN
UPDATE Profiles_
SET
Field2_ = @Param2,
Field3_ = @Param3,
Field4_ = @Param4
WHERE Field1_ = @Param1;
ELSE
INSERT INTO Profiles_
(
Field1_,
Field2_,
Field3_,
Field4_
)
VALUES
(
@Param1,
@Param2,
@Param3,
@Param4 );
END IF;
END;
Please help me. I read many books and tutorials but I can’t understand this.
Your first one needs a table alias
Your second one should begin
To be syntactically correct. Also use
END;notEND IF;However it gives you a potential race condition. As you are on SQL Server 2008 you should look intoMERGEfor doing this kind ofUPSERTAs far as I can see the whole of the second one can be replaced with.