2017-01-30 91 views
2

我有這個表Sql和插入Form重複更新我阻止用戶進入同一HDate爲同一HNumber如何防止SQL存儲過程

RecID  HDate  HNumber  HComb 
---------------------------------------------- 
1   2017-1-30  1   12 
3   2017-1-29  1   15 
5   2017-1-30  2   12 
6   2017-1-30  3   12 
9   2017-1-30  4   12 

但在編輯表單我不」知道如何防止這種情況,

我嘗試在存儲過程的代碼,但它的一些HNumber工作,但它防止一些其他HNumber在它自己的日期進行編輯

Create Procedure UpdCombHarByRecID 

@RecID int, 
@HarvestDate Date, 
@HiveNumber int, 
@HoneyComb Float, 

as 

if NOT Exists (Select * From tHoneyHarvest Where [email protected]) 
return 0 
//there is no record to be updated 

if Exists (Select * From tHoneyHarvest Where [email protected] AND 
HiveNumber=HiveNumber And 
[email protected]) 
// I hoped this should do the job 
//(RecID is PrimaryKey and it is identity) 
return 2 

Update tHoneyHarvest 

Set [email protected], 
[email protected], 
[email protected], 
[email protected] 

Where [email protected] 
return 1 

現在問題在哪裏?

+1

使用*單* UPDATE ... FROM ... WHERE語句,它的'WHERE'條款消除重複。多個'Exists'語句使得代碼更難閱讀和理解。 –

+0

@PanagiotisKanavos用戶無法更改HNumber,但可以編輯HDate,我想檢查HNumber是否已在新編輯的HDate中有記錄,以防止更新任何字段並且什麼都不做 – SaraniO

+1

HiveNumber = HiveNumber將始終爲真。應該是HiveNumber = @ HiveNumber? – Vijay

回答