-1

我的 sql server 数据库有这个存储过程...

ALTER PROCEDURE [dbo].[NewBudget]
@year int,
@int nvarchar(3)
AS
BEGIN
SET NOCOUNT ON;
INSERT [NAOLI].[dbo].[BudgetsEditTbl]
([F_Year],[O_OrgCode],[O_OrgDesc],[S_SubObject],[S_SubDescrip],[B_BudgetAmt],[B_Initials],[B_CIPrefNo],[B_OrgBudgetAmt],[keyfield]) 
SELECT @year as [F_Year],[O_OrgCode],[O_OrgDesc],[S_SubObject],[S_SubDescrip], 0.00 as [B_BudgetAmt],@int as [B_Initials],null as [B_CIPrefNo],null as [B_OrgBudgetAmt],NewID()
FROM [NAOLI].[dbo].[BudgetsEditTbl]
END

发生的事情是根据参数“@year”,它会自动更新数据库,其中包含关于年份变化的新一年的记录。只是没有用新的一年更新它,orgcode 和相应的子对象它给了我 6 条具有相同 orgcode 的记录。所以它本质上是运行该过程 6 次并将所有这些混乱存储在我的数据库中。知道如何解决这个问题吗?

2011 | 536003 | Engineering | 6302 | Roads and Components | 0.00 | MC | N/A | 0.00    
2011 | 536003 | Engineering | 6303 | Rights of Way -Acquire | 0.00 | MC | N/A | 0.00
2010 | 536003 | Engineering | 6302 | Roads and Components | 18300.00 | FG | N/A | 0.00    
2010 | 536003 | Engineering | 6303 | Rights of Way -Acquire | 18300.00 | FG | N/A | 0.00

因此,对于每一年,相同的代码和信息只会以不同的数量出现......

4

1 回答 1

0

您可以使用DISTINCT删除任何重复项

ALTER PROCEDURE [dbo].[NewBudget]
@year int,
@int nvarchar(3)
AS
BEGIN
SET NOCOUNT ON;
INSERT [NAOLI].[dbo].[BudgetsEditTbl]
([F_Year],[O_OrgCode],[O_OrgDesc],[S_SubObject],[S_SubDescrip],[B_BudgetAmt],[B_Initials],[B_CIPrefNo],[B_OrgBudgetAmt],[keyfield]) 
SELECT DISTINCT @year as [F_Year],[O_OrgCode],[O_OrgDesc],[S_SubObject],[S_SubDescrip], 0.00 as [B_BudgetAmt],@int as [B_Initials],null as [B_CIPrefNo],null as [B_OrgBudgetAmt],NewID()
FROM [NAOLI].[dbo].[BudgetsEditTbl]
END
于 2012-05-25T18:52:44.840 回答