简体   繁体   English

为存储过程传递参数

[英]Passing parameters for stored procedure

I have this stored procedure: 我有这个存储过程:

CREATE PROCEDURE [Test]
    (@ID INT,
     @month VARCHAR(10),
     @Low INT,
     @standard FLOAT = 0)
AS
    IF EXISTS (SELECT 1 FROM [Table1] 
               WHERE ID = @ID AND month = @month 
                 AND TYPE = 'S' AND TYPEID = @Low)
    BEGIN
        UPDATE [Table1] 
        SET Add = @standard
        WHERE ID = @ID AND month = @month 
          AND TYPE = 'S' AND TYPEID = @Low
    END
    ELSE
    BEGIN
        INSERT INTO [Table1] (ID, month, Add, TYPE, TYPEID) 
        VALUES (@ID, @month, @standard, 'S', @Low)
    END
GO

This is the table which is missing March dates 这是缺少三月日期的表格

ID     Month    Add    Type    TypeID
---------------------------------------
333    feb      4       S        111
333    April    4       S        111

So I thought by refreshing the stored procedure this would fill in for the month of March. 因此,我认为通过刷新存储过程,该过程将在三月份进行。

But when I execute the procedure using 但是当我使用执行程序时

Use [DB1]
GO

DECLARE @RV int
DECLARE @ID int
DECLARE @month varchar(10)
DECLARE @Low int
DECLARE @Add int
DECLARE @standard float

EXECUTE @RV = [Test] 
              @ID int,
              @month varchar(10),
              @Low int,
              @Add int,
              @standard float
GO

I get 我懂了

  ID        Month      Add        Type     TypeID
  -----------------------------------------------
  333       feb         4           S        111
  333       April       4           S        111
  NULL      NULL        NULL        S        NULL

Although I am looking for 虽然我在找

  ID        Month      Add        Type      TypeID
  ------------------------------------------------
  333       feb         4           S        111
  333       April       4           S        111
  333       Mar         4           S        111

I am assuming I am not passing the parameters correctly or something. 我假设我没有正确传递参数或其他东西。 Can anyone advise on this? 有人可以建议吗? Thanks 谢谢

You're declaring your parameters to pass into the stored procedure but you're not assigning them a value anywhere, so the procedure has nothing to work with. 您是在声明要传递给存储过程的参数,但没有在任何地方分配它们的值,因此该过程没有任何用处。

You need to change the execution statement to something like this: 您需要将执行语句更改为以下内容:

Use [DB1]
GO
DECLARE @RV int = 0;
DECLARE @ID int = 333;
DECLARE @month varchar(10) = 'Mar';
DECLARE @Low int = 111;
DECLARE @Add int = 4;
DECLARE @standard float = 4;

EXECUTE @RV = [Test] 
@ID = @ID,
@month = @month,
@Low = @Low,
@Add = @Add,
@standard = @standard;
GO

Replace the 0's and 1.0 with whatever values want to use. 用要使用的任何值替换0和1.0。

Also, month is a string column in your table but you're passing in an INT to the procedure. 此外, month是表中的字符串列,但您要将INT传递给过程。 You need to change the @month parameter to match the datatype of your table column. 您需要更改@month参数以匹配表列的数据类型。
(removed after your edit) (在您编辑后删除)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM