I want to write a stored procedure to insert data into a table and also to check whether same data already exist?
If yes, then exception throw as already exist. But I don't know where should I add an exception. Please help.
ALTER PROCEDURE [dbo].[datakutipantest1]
AS
BEGIN
DECLARE @ModifiedDate datetime = GETDATE()
INSERT INTO spk_DataKutipan ([NO_BIL], [NO_AKAUN], [TKH_BAYAR],
[STESYEN], [AMAUN_BAYAR], [JENIS_BAYAR], [NO_RESIT], [STATUS], [NO_VOT], [TKH_MODIFIKASI])
SELECT
D.BillNo,
D.AccountNo,
D.TxDate,
D.ReferenceCode,
D.Amount,
PaymentTypeId,
D.ReferenceNo,
D.Status,
D.RevenueCode,
@ModifiedDate
FROM
(SELECT
B.ComponentId,
B.AccountNo,
B.BillNo,
B.RevenueCode,
B.Amount,
B.TxId,
ReferenceNo,
B.ReferenceCode,
status,
TxDate
FROM
(SELECT
A.ComponentId,
A.TxId,
AccountNo,
BillNo,
RevenueCode,
Amount,
C.ReferenceCode
FROM
rcs_TxBillItem A
INNER JOIN
(SELECT
ComponentId,
ComponentName,
ReferenceCode
FROM
rcs_Component
WHERE
IsDeleted = 0) C ON C.ComponentId = A.ComponentId) B
INNER JOIN
rcs_TxBill P ON P.TxId = B.TxId) D
INNER JOIN
rcs_TxBillPayment E ON E.TxId = D.TxId
END
GO