sql—将变量从存储过程传递到另一个存储过程

noj0wjuj  于 2021-08-13  发布在  Java
关注(0)|答案(2)|浏览(418)

我有一个存储过程,需要将参数从一个过程传递到另一个过程,并将其显示为输出。我在我的程序[].[zzzz\u erp\u cyyyyyy]的标题中声明以下内容

DECLARE @ProcedureLogRowKey INT
 DECLARE @ProcedureRecordCount INT 
 DECLARE @ProcedureStartDateTime DATETIME 
 DECLARE @ProcedureLog_Note NVARCHAR(100)

 EXEC [XXX].[spciProcedurePerformanceStartRecord_help]
      '.[xxx].[zzzz_ERP_Cyyyyy]',
      1,
      @ProcedureStartDateTime,
      'Contract Check',
      @ProcedureLogRowKey OUTPUT

我得到以下错误:
msg 515,级别16,状态2,过程SPCProcedurePerformanceStartRecord\u帮助,第33行[批处理开始第17行]
无法将值null插入表“000..yperlog”的列“ystrtdtt0”;列不允许空值。插入失败。
下面是我从中获取变量以传递到过程[].[zzzz\u erp\u cyyyyyy]的过程

CREATE PROCEDURE [xxx].[spciProcedurePerformanceStartRecord_help]
     (@ProcedureName VARCHAR(200),
      @ProcedureRecordCount INT = 1,
      @ProcedureStartDateTime DATETIME = GETDATE,
      @ProcedureLog_Note NVARCHAR(100),
      @ProcedureLogRowKey INT OUTPUT  --- I am passing this into my proc and 
displaying it as output
     )
AS
BEGIN 
    -- Set Default return for @ProcedureLogRowKey, used if logging is not turned on. 
    SET @ProcedureLogRowKey = -1; 

    -- Check to see if performance logging is enabled 
    IF EXISTS(SELECT ROWID FROM LIVE.YPERCON 
              WHERE YPROCNM_0 = @ProcedureName AND YLOGENA_0 = 2) 
    BEGIN
        INSERT INTO xxx.YPERLOG (YROWKEY_0, YPROCNM_0, YRECCNT_0, YSTRTDTT_0, YENDDTT_0, YLOGNOTE_0, 
                                 YDURMS_0, CREDATTIM_0, UPDDATTIM_0, AUUID_0, CREUSR_0, UPDUSR_0)   
            SELECT 
                ISNULL(MAX(YROWKEY_0), 0) + 1, 
                @ProcedureName, @ProcedureRecordCount, @ProcedureStartDateTime, 
                '1753-01-01', 
                @ProcedureLog_Note, 0, 
                GETDATE(), GETDATE(), NEWID(), 'admin', 'admin'  
            FROM
                xxx.YPERLOG

        SELECT @ProcedureLogRowKey = ISNULL(MAX(YROWKEY_0), 0) 
        FROM xxx.YPERLOG
    END 
    ELSE
    BEGIN
        DECLARE @Count integer

        SELECT @Count = COUNT(0) 
        FROM LIVE.YPERERR 
        WHERE YPROCNM_0 = @ProcedureName 

        IS ISNULL(@Count, 0) = 0
            INSERT INTO LIVE.YPERERR (YPROCNM_0, YREQDT_0, YLASTDT_0, YERRMSG_0, 
                                      CREDATTIM_0, UPDDATTIM_0, AUUID_0, CREUSR_0, UPDUSR_0)
            VALUES (@ProcedureName, GETDATE(), '1753-01-01', 'Controller not defined or active', 
                    GETDATE(), GETDATE(), NEWID(), 'admin', 'admin')
        ELSE 
            UPDATE xxx.YPERERR
            SET YLASTDT_0 = GETDATE()
            WHERE YPROCNM_0 = @ProcedureName
       END
END

提前谢谢。

ryhaxcpt

ryhaxcpt1#

问题正在处理中 [].[spciProcedurePerformanceStartRecord_help] 带参数 @ProcedureStartDateTime DATETIME . 应按以下方式设置其默认值:
在声明中,将默认值设置为 NULL ```
@ProcedureStartDateTime DATETIME = NULL

看起来像是提斯

CREATE PROCEDURE [xxx].[spciProcedurePerformanceStartRecord_help]
(
@ProcedureName VARCHAR(200)
,@ProcedureRecordCount INT = 1
,@ProcedureStartDateTime DATETIME = NULL
,@ProcedureLog_Note NVARCHAR(100)
,@ProcedureLogRowKey INT OUTPUT
)
AS
BEGIN

-- procedure's body

END

在程序内部,首先检查 `@ProcedureStartDateTime` 参数的值为 `NULL` 如果是,则将其值设置为 `GETDATE()` .

SET @ProcedureStartDateTime = ISNULL(@ProcedureStartDateTime, GETDATE())

rfbsl7qr

rfbsl7qr2#

你已经宣布 DECLARE @ProcedureStartDateTime DATETIME 没有给它设置任何值。因此,它有空值,您将空值传递给过程执行

EXEC [XXX].[spciProcedurePerformanceStartRecord_help]
 '.[xxx].[zzzz_ERP_Cyyyyy]',
  1,
  @ProcedureStartDateTime, -- NULL value passed here
 'Contract Check',
 @ProcedureLogRowKey OUTPUT

作为目标列 'YSTRTDTT_0', table '000..YPERLOG' ,不允许空值,出现错误。

相关问题