2012-08-13 67 views
1

我寫了一個存儲過程來更新滿足一定條件的記錄。我想檢查傳入的值是否包含在表中。我正在使用循環遍歷整個表來檢查值sql server

declare @disp_sname varchar(100); 
declare @disp_type varchar(100); 
declare @disp_sub_type varchar(100); 
declare @disp_date date; 

select @disp_sname= voucher_sname,@disp_type=voucher_type, 
@disp_sub_type=voucher_sub_type,@disp_date=voucher_date 
from voucher_master 

這隻返回最後一行值。

我的整個存儲過程是

USE [new_esatnam] 
GO 
/****** Object: StoredProcedure [dbo].[spUpdateVoucherNo] Script Date: 08/13/2012 13:36:24 ******/ 
SET ANSI_NULLS ON 
GO 
SET QUOTED_IDENTIFIER ON 
GO 

ALTER PROCEDURE [dbo].[spUpdateVoucherNo] 
(
@voucher_id as int, 
@voucher_separate_numbering as varchar(2), 
@voucher_method_numbering as varchar(2), 
@voucher_last_number as int, 
@voucher_sname as varchar(15), 
@voucher_type as varchar(2), 
@voucher_sub_type as varchar(2), 
@voucher_date as datetime, 
@company_code as varchar(50), 
@updated_by as int, 
@updated_on as datetime 
) 
AS 
BEGIN 
SET NOCOUNT ON; 
declare @disp_sname varchar(100); 
declare @disp_type varchar(100); 
declare @disp_sub_type varchar(100); 
declare @disp_date date; 

select @disp_sname= voucher_sname,@disp_type=voucher_type, 
@disp_sub_type=voucher_sub_type,@disp_date=voucher_date 
from voucher_master 

if @[email protected]_sname and @[email protected]_type and @[email protected]_sub_type and @[email protected]_date 
BEGIN 
update voucher_master set [email protected]_type,[email protected]_sub_type, 
[email protected]_sname, 
[email protected]_separate_numbering, 
[email protected]_method_numbering, 
[email protected]_date, 
[email protected]_last_number, 
[email protected]_code, 
[email protected]_by, 
[email protected]_on where [email protected]_id 
END 
if @[email protected]_sname 
BEGIN 
update voucher_master set [email protected]_type,[email protected]_sub_type, 
[email protected]_sname, 
[email protected]_separate_numbering, 
[email protected]_method_numbering, 
[email protected]_date, 
[email protected]_last_number, 
[email protected]_code, 
[email protected]_by, 
[email protected]_on where [email protected]_id 
END 

return @@ROWCOUNT 
END 

我希望我的存儲過程來滿足這個條件

voucher_type voucher_sub_type  date  voucher_sname 
INV    DOM    1/1/2000  ID   allowed 
INV    DOM    15/1/2000  ID   allowed 

INV    INT    1/1/2000  ID   not allowed 
INV    INT    15/3/2012  ID   not allowed 

券SNAME應該允許同一voucher_type和voucher_sub_type不同日期和同一voucher_sname應不允許voucher_type和voucher_sub_type的不同組合。

任何想法如何我的sp將達到此?

回答

0

只是在你的第一AQL查詢--voucher_id = @使用voucher_id

ALTER PROCEDURE [dbo].[spUpdateVoucherNo] 
     (
     @voucher_id as int, 
     @voucher_separate_numbering as varchar(2), 
     @voucher_method_numbering as varchar(2), 
     @voucher_last_number as int, 
     @voucher_sname as varchar(15), 
     @voucher_type as varchar(2), 
     @voucher_sub_type as varchar(2), 
     @voucher_date as datetime, 
     @company_code as varchar(50), 
     @updated_by as int, 
     @updated_on as datetime 
     ) 
     AS 
     BEGIN 
     SET NOCOUNT ON; 
     declare @disp_sname varchar(100); 
     declare @disp_type varchar(100); 
     declare @disp_sub_type varchar(100); 
     declare @disp_date date; 

DECLARE @minid int,@maxid int 
select @minid=min(voucherid),@maxid=max(voucherid) from voucher_master 

while(@minid <= @maxid) 
BEGIN 
     select @disp_sname= voucher_sname,@disp_type=voucher_type, 
     @disp_sub_type=voucher_sub_type,@disp_date=voucher_date 
     from voucher_master where [email protected] 

     if @[email protected]_sname and @[email protected]_type and @[email protected]_sub_type and @[email protected]_date 
     BEGIN 
     update voucher_master set [email protected]_type,[email protected]_sub_type, 
     [email protected]_sname, 
     [email protected]_separate_numbering, 
     [email protected]_method_numbering, 
     [email protected]_date, 
     [email protected]_last_number, 
     [email protected]_code, 
     [email protected]_by, 
     [email protected]_on where [email protected]_id 
     END 
     if @[email protected]_sname 
     BEGIN 
     update voucher_master set [email protected]_type,[email protected]_sub_type, 
     [email protected]_sname, 
     [email protected]_separate_numbering, 
     [email protected]_method_numbering, 
     [email protected]_date, 
     [email protected]_last_number, 
     [email protected]_code, 
     [email protected]_by, 
     [email protected]_on where [email protected]_id 
     END 

SET @[email protected]+1 
END 

END 
+0

這是從OP的代碼有什麼不同?如果是這樣,可能值得強調你已經做出更改 – 2012-08-13 08:29:02

+0

我剛剛在第一個sql select的where子句中添加了這個(where voucher_id = @ voucher_id)。 – AnandPhadke 2012-08-13 08:37:50

+0

這裏循環你可以使用while循環根據voucher_id的最小和最大值 – AnandPhadke 2012-08-13 08:39:32