2012-08-13 79 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