简体   繁体   English

使用表中的值执行存储过程SQL

[英]Execute stored procedure SQL using value from table

Check my table below: 检查我的下表:

ID  Number01  Number02  Number03
-----------------------------------
1   10        20        4510
2   5         2         545
3   4         4         664
4   10        1         NULL
5   1         4         NULL

"Number03" field is a calculated field which is Number01 + Number02. “ Number03”字段是一个计算字段,它是Number01 + Number02。 I am using a stored procedure to calculate it. 我正在使用存储过程来计算它。 Why I am using a stored procedure? 为什么要使用存储过程? Because I have the interface which made by asp.net. 因为我有asp.net制作的界面。

This is the stored procedure: 这是存储过程:

ALTER PROCEDURE _mySP
    (@Number01 decimal(18,0), @Number02 decimal(18,0))
AS
BEGIN
    DECLARE @myCOM1 float
    DECLARE @myCOM2 float

    SET @myCOM1 = @Number1 + 500
    SET @myCOM2 = POWER(@Number2, 2) * 10

    INSERT INTO _myTable(Number01, Number02, Number03) 
    VALUES (@Number01, @Number02, @myCOM1 + @myCOM2)
END

The question is, how can I execute the stored procedure without entering the value one by one? 问题是,如何在不逐一输入值的情况下执行存储过程? Because the value is already in the table. 因为该值已在表中。 I want to update all the null value on "Number03" field. 我想更新“ Number03”字段上的所有空值。 Also any idea how execute my question using CURSOR? 还知道如何使用CURSOR执行我的问题吗?

EDIT: It seems that my previous question is too simple. 编辑:似乎我以前的问题太简单了。 So I make it complex a little bit. 所以我有点复杂。

Since you're using SQL Server 2008, you can use a computed field to generate the value from the other two fields. 由于使用的是SQL Server 2008,因此可以使用计算字段从其他两个字段生成值。

If you just want to update the table without doing this, you could use the following SQL: 如果您只想更新表而不执行此操作,则可以使用以下SQL:

UPDATE _myTable SET Number03 = ISNULL(Number01,0) + ISNULL(Number02,0)

This will update all rows. 这将更新所有行。

You could update the SP to take a 3rd param: 您可以更新SP以采用第三个参数:

ALTER PROCEDURE _mySP
    (
      @Number01 DECIMAL(18, 0) ,
      @Number02 DECIMAL(18, 0) ,
      @IsUpdate BIT = 0
    )
AS
    BEGIN
        IF ( @IsUpdate = 0 )
            BEGIN
                INSERT  INTO _myTable
                        ( Number01 ,
                          Number02 ,
                          Number03
                        )
                VALUES  ( @Number01 ,
                          @Number02 ,
                          ISNULL(@Number01,0) + ISNULL(@Number02,0)
                        )
            END
        ELSE
            BEGIN
                UPDATE  _myTable
                SET     Number03 = ISNULL(Number01,0) + ISNULL(Number02,0)
                WHERE Number03 IS NULL
            END
    END

EDIT : I have added ISNULL to the calculation for any numbers that are null , it will use 0 instead. 编辑 :我已经将ISNULL添加到任何为null数字的计算中,它将改为使用0

If you really want to use the SP to do the work you have to do it one at a time. 如果您真的想使用SP来完成工作,则必须一次执行一次。 Write a cursor to select all the rows with a NULL value and call the SP for each row passing Number01 and Number02. 编写一个游标以选择具有NULL值的所有行,并为通过Number01和Number02的每一行调用SP。

I suspect your actual example is rather more complicated than the code you've shown. 我怀疑您的实际示例比您显示的代码还要复杂。 If you are able to go into more detail we may be able to suggest better approaches. 如果您能够更详细地介绍我们,则可以提出更好的方法。 If it is indeed this simple a computed column or simple update statement would be the way to go. 如果确实如此简单,那么可以使用计算列或简单的update语句。

You can use computed columns like this. 您可以使用像这样的计算列。 It will automatically fill all data in number03 column. 它将自动填充number03列中的所有数据。

CREATE TABLE _myTable(
ID INT IDENTITY(1,1),
Number01  INT,
Number02  INT,
Number03 AS (Number01 + Number02)
)

------OR if you already have table then ------或者,如果您已经有桌子,那么

Alter table _myTable Drop column Number03;
Alter table _myTable Add Number03 AS (Number01 + Number02);

------------- EDITED ACCORDING TO YOUR EDITING IN QUESTIONS -------------根据您的问题进行编辑

You can still put this formula in computed column. 您仍然可以将此公式放在计算列中。 But as you are asking another way, so You can edit your SP like this 但是,当您问另一种方式时,您可以像这样编辑SP

ALTER PROCEDURE _mySP
AS
BEGIN
Update _myTable
set Number03 = ((Number01 + 500) + (POWER(Number02, 2) * 10))
WHERE Number03 is NULL;
END

--------Executing Stored procedure will update table without providing any parameters
Exec _mySP

Here is how you could do it: 这是您可以执行的操作:

CREATE TABLE #Test(
ID int,
Number1 decimal(18,0),
number2 decimal(18,0),
number3 decimal(18,0))

INSERT INTO #Test
VALUES
(1  , 10, 20, 30),
(2  , 5 , 2,  7),
(3  , 4,  4 , 8),
(4  , 10, 1,  NULL),
(5  , 1,  4,  NULL)

UPDATE #Test
Set Number3 = Number1 + number2

SELECT * FROM #Test
DROP TABLE #Test

Example at SQLFiddle SQLFiddle中的示例

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

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