heatherb0123
Technical User
I'm trying to create a stored procedure that updates records when they exist, and creates a new record when they don't. The code I'm using is duplicating the existing records. Can anyone help?
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
USE [cashNotification]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[Upt_DCF_Balances]
@HiNetMajor varchar (12),
@HiNetMinor varchar (12),
@PostDate datetime,
@Currency varchar (3),
@Balance float,
@BalanceType varchar (50),
@Source varchar (50),
@UserID varchar (12)
AS
BEGIN
SET NOCOUNT ON;
F NOT EXISTS (SELECT HiNetMinor FROM tblDCF_Balances WHERE HiNetMajor = @HiNetMajor AND HiNetMinor = @HiNetMinor AND EffDate = @PostDate AND CurrCode = @Currency AND BalanceType = @BalanceType)
BEGIN
INSERT INTO tblDCF_Balances (
HiNetMajor,
HiNetMinor,
EffDate,
CurrCode,
Balance,
BalanceType,
DataSource,
UserID)
SELECT
@HiNetMajor,
@HiNetMinor,
@PostDate,
@Currency,
@Balance,
@BalanceType,
@Source,
@UserID
END
ELSE
BEGIN
UPDATE tblDCF_Balances
SET
Balance = @Balance,
DataSource = @Source,
UserID = @UserID
WHERE HiNetMajor = @HiNetMajor AND
HiNetMinor = @HiNetMinor AND
EffDate = @PostDate AND
CurrCode = @Currency AND
BalanceType = @BalanceType
END
--Grant execute on Upt_DCF_Balances to public
END
Heather B...
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
USE [cashNotification]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[Upt_DCF_Balances]
@HiNetMajor varchar (12),
@HiNetMinor varchar (12),
@PostDate datetime,
@Currency varchar (3),
@Balance float,
@BalanceType varchar (50),
@Source varchar (50),
@UserID varchar (12)
AS
BEGIN
SET NOCOUNT ON;
F NOT EXISTS (SELECT HiNetMinor FROM tblDCF_Balances WHERE HiNetMajor = @HiNetMajor AND HiNetMinor = @HiNetMinor AND EffDate = @PostDate AND CurrCode = @Currency AND BalanceType = @BalanceType)
BEGIN
INSERT INTO tblDCF_Balances (
HiNetMajor,
HiNetMinor,
EffDate,
CurrCode,
Balance,
BalanceType,
DataSource,
UserID)
SELECT
@HiNetMajor,
@HiNetMinor,
@PostDate,
@Currency,
@Balance,
@BalanceType,
@Source,
@UserID
END
ELSE
BEGIN
UPDATE tblDCF_Balances
SET
Balance = @Balance,
DataSource = @Source,
UserID = @UserID
WHERE HiNetMajor = @HiNetMajor AND
HiNetMinor = @HiNetMinor AND
EffDate = @PostDate AND
CurrCode = @Currency AND
BalanceType = @BalanceType
END
--Grant execute on Upt_DCF_Balances to public
END
Heather B...