[英]Stored procedure runs successfully but fails when called from a job
I'm running into a very bizarre error in SQL that I would like some help with. 我遇到了一个非常奇怪的SQL错误,希望对此提供帮助。 The code below creates the two stored procedures I need for this report:
下面的代码创建了此报告所需的两个存储过程:
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
When I execute both stored procedures it runs succesfully, however when I call the stored procedure from a scheduled job it yields the following error: 当我执行两个存储过程时,它都成功运行,但是当我从计划作业中调用存储过程时,会产生以下错误:
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.
I ran profiler and saw that it fails at the update step to the CUSTOMER_ORDER table on both procedures. 我运行了探查器,发现在两个过程中CUSTOMER_ORDER表的更新步骤中它均失败。 The Status column is set as char(1) and the stored procedure works correctly when called from a query but it is failing when being called from a job.
Status列设置为char(1),并且从查询中调用时存储过程可以正常工作,但是从作业中调用时则失败。
Thanks 谢谢
After a lot of struggle I found the culprit. 经过很多努力,我找到了罪魁祸首。 It turns out that SQL does not like the fact that the user name running the job is over 50 characters long.
事实证明,SQL不喜欢运行作业的用户名超过50个字符的事实。 The query works fine when I added:
当我添加时查询工作正常:
EXEC AS USER = 'sysadm' 以用户身份执行='sysadm'
Which is my SQL admin account (same rights as the domain one). 这是我的SQL管理员帐户(与域一具有相同的权限)。 Hopefully this will save someone from the headache it caused me.
希望这可以使某人摆脱我的头痛。
Thanks to everyone who pitched in to help, you guys are great. 谢谢大家的帮助,你们很棒。
Regards, 问候,
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.