存储过程成功运行,但从作业调用时失败

时间:2015-02-12 16:26:18

标签: sql sql-server stored-procedures sql-agent-job sql-job

我在SQL中遇到了一个非常奇怪的错误,我想要一些帮助。下面的代码创建了此报告所需的两个存储过程:

USE [ONDTTEST]

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE ONDT_OPS_OTD_STEP1 
AS
    BEGIN
        SET NOCOUNT ON;

        /** Create Table **/
        CREATE TABLE ONDT_OPS_OTD_SMARTVIEW
        (
            ID      varchar(15) NOT NULL,
            STATUS  char(1) NOT NULL
        );

        /** Insert data into table **/
        INSERT INTO      [ONDT_OPS_OTD_SMARTVIEW]
        SELECT           [ID]
                        ,[STATUS]
        FROM             [CUSTOMER_ORDER]
        WHERE            [STATUS] = 'H';

        /** Update customer order table from H to R **/
        UPDATE           [CUSTOMER_ORDER]
        SET              [STATUS] = 'R'
        WHERE            [STATUS] = 'H';
    END
GO

CREATE PROCEDURE ONDT_OPS_OTD_STEP2 
AS
    BEGIN
        SET NOCOUNT ON;

        /** Update records back to H **/
        UPDATE  [CUSTOMER_ORDER]
        SET     [STATUS] = 'H'
        WHERE   [ID] IN (SELECT [ID] FROM [ONDT_OPS_OTD_SMARTVIEW]);

        /** Drop Table **/
        DROP TABLE ONDT_OPS_OTD_SMARTVIEW;
    END
GO

当我执行两个存储过程时,它会成功运行,但是当我从预定作业调用存储过程时,会产生以下错误:

Executed as user: admin. String or binary data would be truncated. [SQLSTATE 22001] (Error 8152)  The statement has been terminated. [SQLSTATE 01000] (Error 3621).  The step failed.

我运行了探查器,发现在两个程序的更新步骤中它都无法通过CUSTOMER_ORDER表。 Status列设置为char(1),并且从查询调用时存储过程可以正常工作,但从作业调用时失败。

由于

1 个答案:

答案 0 :(得分:0)

经过多次奋斗,我找到了罪魁祸首。事实证明,SQL不喜欢运行作业的用户名超过50个字符的事实。我添加时查询工作正常:

EXEC AS USER =' sysadm'

哪个是我的SQL管理员帐户(与域名相同的权限)。希望这可以帮助别人避免它给我带来的麻烦。

感谢所有投入帮助的人,你们都很棒。

此致