如何在同一个存储过程中对单个表使用多个更新查询

时间:2017-05-19 13:26:26

标签: sql sql-server database sql-server-2008

我有

的存储过程

1)更新查询,我想添加

2)在同一商店程序中更新查询。我可以使用下面的代码吗?或者任何人都可以帮助我如何在同一个表中使用多个更新查询?

USE [Databse]
GO
/****** Object:  StoredProcedure [dbo].[sp_Tableupdate]    
Script Date: 5/19/2017 8:12:11 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[sp_Tableupdate]
@IncomeID int,
@MemberID int,
@ParticipantID int,
@IncomeTypeID int,
@PaymentFrequencyTypeID int,
@Employer varchar(max),
@Occupation varchar(max),
@TypeOfBusiness varchar(max),
@GrossAmount decimal(18,2),
@Verified bit
AS
BEGIN
1)UPDATE Table SET
    MemberID=@MemberID,
    ParticipantID=@ParticipantID,
    IncomeTypeID=@IncomeTypeID,
    PaymentFrequencyTypeID=@PaymentFrequencyTypeID,
    Employer=@Employer,
    Occupation=@Occupation,
    TypeOfBusiness=@TypeOfBusiness,
    GrossAmount=@GrossAmount,
    Verified=@Verified 
WHERE IncomeID=@IncomeID

2)Update table set ParticipantID = @ParticipantID where MemberID = @MemberID
END

1 个答案:

答案 0 :(得分:1)

这绝对是可能的,您可以根据需要在其中设置尽可能多的UPDATE语句。

请注意,如果触摸了相同的记录,您的上一个更新语句将覆盖您的第一个更新语句。

最佳做法是以分号结束每个语句,例如: ';'。)

USE [Databse]
GO
/****** Object:  StoredProcedure [dbo].[sp_Tableupdate]    
Script Date: 5/19/2017 8:12:11 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[sp_Tableupdate]
@IncomeID int,
@MemberID int,
@ParticipantID int,
@IncomeTypeID int,
@PaymentFrequencyTypeID int,
@Employer varchar(max),
@Occupation varchar(max),
@TypeOfBusiness varchar(max),
@GrossAmount decimal(18,2),
@Verified bit,
@run_proc int
AS
BEGIN
IF(@run_proc = 1)
BEGIN
PRINT 'Running Update 1';
UPDATE Table SET
    MemberID=@MemberID,
    ParticipantID=@ParticipantID,
    IncomeTypeID=@IncomeTypeID,
    PaymentFrequencyTypeID=@PaymentFrequencyTypeID,
    Employer=@Employer,
    Occupation=@Occupation,
    TypeOfBusiness=@TypeOfBusiness,
    GrossAmount=@GrossAmount,
    Verified=@Verified 
WHERE IncomeID=@IncomeID;
END

ELSE IF(@run_proc = 2) BEGIN
    PRINT 'Running Update 2';
    Update table set ParticipantID = @ParticipantID where MemberID = @MemberID  
END

END