sql存储过程

时间:2011-04-29 13:59:55

标签: sql vb.net

create PROC insert_Items(@Store varchar(20),@ID varchar(20),@Name varchar(20),@IDate varchar(11))
As
Declare @UDates varchar(11),
Declare @Quantity int,
Declare @Defects int
Select  @Quantity  From inserted 
Select  @Defects  From Inserted
set @UDates = getdate(), 
     @Remanders = @Defects - @Quantity
Insert Items(StoreName,ProductID,ProductName,Quantity,Defects,InsertedDate,UpdatedDate)
Values(@Store,@ID,@Name,@Quantity,@Defects,@IDate,@UDates)

问题是我想在我的表l

中获取Remainder的值

2 个答案:

答案 0 :(得分:0)

如果您的表已包含Remainder字段,则只需将其添加到INSERT语句中,如下所示:

    Insert Items(StoreName, ProductID, ProductName, Quantity, Defects, InsertedDate, UpdatedDate, Remainder) 
Values(@Store, @ID, @Name, @Quantity, @Defects, @IDate, @UDates, @Remainder)

更好的选择可能是使表中的剩余列成为计算列。这样,即使未通过存储过程添加数据,您也始终拥有该值。

答案 1 :(得分:0)

一般做法是declare an OUTPUT parameter for the procedure。然后调用者将该值作为参数 例如

create proc outputdemo(@invalue int, @outvalue int output)
as
    set @outvalue = @invalue * 2
go

declare @myvalue int
exec outputdemo 10, @myvalue output
select @myvalue