Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 如何使用一些declard和setted值更新行_Sql_Stored Procedures_Sql Update_Declare - Fatal编程技术网

Sql 如何使用一些declard和setted值更新行

Sql 如何使用一些declard和setted值更新行,sql,stored-procedures,sql-update,declare,Sql,Stored Procedures,Sql Update,Declare,您好,我很难处理添加“NULL”而不是数字的存储过程 那么为什么下面的过程会添加“NULL”而不是0到无穷大之间的值? 这是我的程序 ALTER PROCEDURE [dbo].[Plan_Abschluss] -- My parameters for the stored procedure @date AS datetime2(7), @Einrichtung AS Int, @Mitarbeiter

您好,我很难处理添加“NULL”而不是数字的存储过程

那么为什么下面的过程会添加“NULL”而不是0到无穷大之间的值?

这是我的程序

ALTER PROCEDURE [dbo].[Plan_Abschluss]

    -- My parameters for the stored procedure
    @date                   AS datetime2(7),
    @Einrichtung            AS Int,
    @Mitarbeiter            AS Int

AS
BEGIN

    -- declare my parameters
    DECLARE @PlanStunden            AS decimal(18, 2)= null,
            @PlanUrlaub             AS Int= null,

            @oldDate                AS datetime2(7)= null,
            @oldUrlaubskonto        AS Int= null,
            @oldStundenKonto        AS decimal(18, 2)= null;

    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- set the previous month
    SET @oldDate= DATEADD(month, -1, @date);

    -- get some values from the previous row and set it to my parameters
    SELECT  @oldUrlaubskonto =  ISNULL(CurrentUrlaubskonto,0) ,
            @oldStundenKonto =  ISNULL(CurrentStundenKonto,0)

    FROM    [Plan]

    WHERE   [Jahr]              = YEAR(@oldDate)
    AND     [Monat]             = MONTH(@oldDate)
    AND     [RefMitarbeiterId]  = @Mitarbeiter
    AND     [RefEinrichtungId]  = @Einrichtung;

    -- get some values from the row i want to update and set it to my parameters
    SELECT  @PlanStunden =  ISNULL(PlanStunden,0) ,
            @PlanUrlaub =   ISNULL(PlanUrlaub,0)

    FROM    [Plan]

    WHERE   [Jahr]              = YEAR(@date)
    AND     [Monat]             = MONTH(@date)
    AND     [RefMitarbeiterId]  = @Mitarbeiter
    AND     [RefEinrichtungId]  = @Einrichtung;

    -- update the row and do a calculation with my parameters
    UPDATE  [Plan]

    SET     Abgeschlossen       = 1,
            CurrentUrlaubskonto = @oldUrlaubskonto+ @PlanUrlaub,
            CurrentStundenKonto = @oldStundenKonto+ @PlanStunden 

    WHERE   [Jahr]              = YEAR(@date)
    AND     [Monat]             = MONTH(@date)
    AND     [RefMitarbeiterId]  = @Mitarbeiter
    AND     [RefEinrichtungId]  = @Einrichtung

END

如果未返回任何行,则在
select
中不会设置变量。我的猜测是,使用
@OldDate
的第一个
选择
,根本不匹配任何行

特别是,变量
@oldUrlaubskonto
@oldstudenkonto
被初始化为
NULL
,因此在没有匹配记录时,永远不会设置它们。解决这一问题的一个简单方法是使用聚合——您仍然需要一行,所以这是可以的:

SELECT  @oldUrlaubskonto =  ISNULL(max(CurrentUrlaubskonto), 0) ,
        @oldStundenKonto =  ISNULL(max(CurrentStundenKonto), 0

如果该值仍然为
NULL

,则您也可以在之后设置该值。将哪些值设置为
NULL
?@GordonLinoff CurrentUrlaubskonto和CurrentStundenKonto