我有 2 个 sql 表
[dbo].[Contract]
[ContractID] pk,.....other column
[dbo].[Installment] //this table has composite primary key
[ContractID] pk n fk // it is primary and foreign key
[installmentNum] pk, // contractID and installmentnum both are the primary keys
ContractID 有 6 或 12 个分期付款,我使用 installmentNum 列通过 winform 插入我编写了这个存储过程
ALTER PROCEDURE [dbo].[spInsertUpdateInstallment]
@ContractID int = 0,
@InstallmentNumber int = 0,
@month varchar(10),
@installment money = 0.00,
@commission money = 0.00,
@iupdate int = 0
AS
SET NOCOUNT ON
SET XACT_ABORT ON
BEGIN TRAN
if @ContractID<>0 and @iupdate=0 //i'm confused here
BEGIN
INSERT INTO [dbo].[Installment]
VALUES(@InstallmentNumber,@month,@installment,@commission)
END
if @ContractID <>0 and @month is not null and @iupdate=1
BEGIN
UPDATE [dbo].[Installment]
SET
[ContractID] = @ContractID,
[InstallmentNumber] = @InstallmentNumber,
[currentMonth] = @month,
[installment] = @installment,
[commission] = @commission
WHERE [ContractID] = @ContractID and [currentMonth] = @month
END
Commit
第二个用于逐一阅读所选合同的分期付款我正在尝试编写此存储过程
ALTER PROCEDURE [dbo].[spReadInstallment]
@ContractID int = 0
AS
SET NOCOUNT ON
SET XACT_ABORT ON
BEGIN tran
if @ContractID<>0
begin tran
BEGIN
SELECT ContractID, InstallmentNumber, currentMonth, installment, commission
FROM Installment
WHERE (ContractID = @contractid) // have doubt about the select statement?
END
commit
我对 spReadInstallment 的 SELECT 语句和 spInsertUpdateInstallment 的插入语句感到困惑我是初学者,无法理解背后的错误请指导我