存储过程在exec,代理作业失败时起作用

时间:2018-08-02 03:16:22

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

我有一个存储过程,该存储过程在使用EXEC运行时可以正常运行,但是作为步骤添加到代理作业时会失败。存储过程不需要任何参数。它用于处理供应商数据库中的许多地理形状,并缓存生成的MS地理对象以供其他进程使用。

存储过程:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[sp_job_update_shapes_ex]
AS
BEGIN
    SET NOCOUNT ON

    --debug 
    DECLARE @ProcessName varchar(50) = ISNULL(OBJECT_NAME(@@PROCID), 'update_shape_ex')
    DECLARE @Message varchar(1000)
    DECLARE @SessionName varchar(50) = 'ugp_' + CONVERT(VARCHAR(20), GETDATE(), 120)

    DECLARE @LogSevereError int = 1
    DECLARE @LogError int = 2
    DECLARE @LogInfo int = 3
    DECLARE @LogVerbose int = 4
    DECLARE @LogDebug int = 5

    DECLARE @ShapeId int
    DECLARE @Polygon geography
    DECLARE @RC int
    DECLARE @NodeTypeId int
    DECLARE @NodeTypeGz int = 5
    DECLARE @NodeTypePoi int = 15
    DECLARE @ShapeName varchar(10)
    DECLARE @ShapeNameGz varchar(10) = 'Geozone'
    DECLARE @ShapeNamePoi varchar(10) = 'POI'

    SET @Message = 'Begin ' + @ProcessName 
    EXEC sp_Log @ProcessName, @Message, @SessionName, @LogInfo

    /****************************************************************************/
    SET @Message = 'Insert new Geozones into GeozoneEx'
    EXEC sp_Log @ProcessName, @Message, @SessionName, @LogInfo

    INSERT INTO GeozoneEx (GeozoneId, GeozoneUsageId)
    SELECT GeozoneId = NodeId,
        GeozoneUsageId = 0
    FROM Ctrack6..Nodes n
    WHERE n.NodeTypeId = @NodeTypeGz
    and n.NodeIsActive = 1
    and n.NodeId NOT IN (SELECT GeozoneId FROM GeozoneEx)

    SET @Message = 'Rows Inserted: ' + CONVERT(VARCHAR(5), @@ROWCOUNT)
    EXEC sp_Log @ProcessName, @Message, @SessionName, @LogInfo


    /****************************************************************************/
    SET @Message = 'Insert new POI into PoiEx'
    EXEC sp_Log @ProcessName, @Message, @SessionName, @LogInfo

    INSERT INTO PoiEx (PoiId)
    SELECT NodeId
    FROM Ctrack6..Nodes n
    WHERE n.NodeTypeId = @NodeTypePoi
    and n.NodeIsActive = 1
    and n.NodeId NOT IN (SELECT PoiId FROM PoiEx)

    SET @Message = 'Rows Inserted: ' + CONVERT(VARCHAR(5), @@ROWCOUNT)
    EXEC sp_Log @ProcessName, @Message, @SessionName, @LogInfo


    /****************************************************************************/
    SET @Message = 'Greate Polygons for shapes with NodeType (' + @ShapeNameGz + ', ' + @ShapeNamePoi + ')'
    EXEC sp_Log @ProcessName, @Message, @SessionName, @LogInfo

    DECLARE _shape CURSOR FOR
    SELECT NodeId, NodeTypeId
    FROM Ctrack6..Nodes
    WHERE NodeTypeId IN (@NodeTypeGz, @NodeTypePoi)
    AND NodeIsActive = 1
    --SELECT NodeId
    --FROM vwGeozones
    --WHERE GzPolygon IS NULL

    OPEN _shape
        FETCH NEXT FROM _shape INTO @ShapeId, @NodeTypeId
        WHILE @@FETCH_STATUS = 0
        BEGIN
            SET @RC = 0
            SET @Polygon = NULL

            IF @NodeTypeId = @NodeTypeGz
            BEGIN
                SET @ShapeName = @ShapeNameGz
            END
            ELSE IF @NodeTypeId = @NodeTypePoi
            BEGIN
                SET @ShapeName = @ShapeNamePoi
            END
            ELSE
            BEGIN
                SET @ShapeName = 'Unknown'
            END

            SET @Message = 'Creating polygon for shape ' + @ShapeName + ': ' + CONVERT(VARCHAR(10), @ShapeId)
            EXEC sp_Log @ProcessName, @Message, @SessionName, @LogInfo

            EXEC @RC = sp_convert_shape_to_geography @SessionName, @ShapeId, @Polygon OUTPUT

            IF @RC = 0
            BEGIN
                IF @NodeTypeId = @NodeTypeGz
                BEGIN
                    UPDATE GeozoneEx
                    SET Polygon = @Polygon
                    WHERE GeozoneId = @ShapeId
                END
                ELSE IF @NodeTypeId = @NodeTypePoi
                BEGIN
                    UPDATE PoiEx
                    SET Polygon = @Polygon
                    WHERE PoiId = @ShapeId
                END
                ELSE
                BEGIN
                    SET @Message = 'Successfully created polygon, but cannot save as shape is ' + @Shapename + ': ' + CONVERT(VARCHAR(10), @ShapeId)
                    EXEC sp_Log @ProcessName, @Message, @SessionName, @LogError
                END
            END
            ELSE
            BEGIN
                SET @Message = 'Error creating polygon for shape ' + @Shapename + ': ' + CONVERT(VARCHAR(10), @ShapeId)
                EXEC sp_Log @ProcessName, @Message, @SessionName, @LogError
            END

            FETCH NEXT FROM _shape INTO @ShapeId, @NodeTypeId
        END
    CLOSE _shape
    DEALLOCATE _shape

    SET @Message = 'End ' + @ProcessName 
    EXEC sp_Log @ProcessName, @Message, @SessionName, @LogInfo

END

SQL Server代理作业:

将存储过程作为作业中的一个步骤创建时,它将失败,并且没有可用的实际日志信息。

创建为作业的存储过程:

Stored procedure created as job

作业失败消息:

Job failed message

工作历史记录日志:

Job History Log

历史记录:

Date        2/08/2018 12:59:08 PM
Log     Job History (Cache Geozones and POI)

Step ID     1
Server      KINGSFELD
Job Name        Cache Geozones and POI
Step Name       exec sp_job_update_shapes_ex
Duration        00:00:01
Sql Severity    16
Sql Message ID  6522
Operator Emailed    
Operator Net sent   
Operator Paged  
Retries Attempted   0

Message
Executed as user: NT AUTHORITY\SYSTEM. ..._shapes_ex    Begin sp_job_update_shapes_ex 
[SQLSTATE 01000] (Message 50000)  3 2018-08-02 12:59:08 ugp_2018-08-02 12:59:08 sp_job_update_shapes_ex Insert new Geozones into GeozoneEx 
[SQLSTATE 01000] (Message 50000)  3 2018-08-02 12:59:08 ugp_2018-08-02 12:59:08 sp_job_update_shapes_ex Rows Inserted: 0 
[SQLSTATE 01000] (Message 50000)  3 2018-08-02 12:59:08 ugp_2018-08-02 12:59:08 sp_job_update_shapes_ex Insert new POI into PoiEx 
[SQLSTATE 01000] (Message 50000)  3 2018-08-02 12:59:08 ugp_2018-08-02 12:59:08 sp_job_update_shapes_ex Rows Inserted: 0 
[SQLSTATE 01000] (Message 50000)  3 2018-08-02 12:59:08 ugp_2018-08-02 12:59:08 sp_job_update_shapes_ex Greate Polygons for shapes with NodeType (Geozone, POI) 
[SQLSTATE 01000] (Message 50000)  3 2018-08-02 12:59:08 ugp_2018-08-02 12:59:08 sp_job_update_shapes_ex Creating polygon for shape POI: 500 
[SQLSTATE 01000] (Message 50000)  3 2018-08-02 12:59:08 ugp_2018-08-02 12:59:08 sp_convert_shape_to_geography   Process:    Begin sp_convert_shape_to_geography 
[SQLSTATE 01000] (Message 50000)  3 2018-08-02 12:59:08 ugp_2018-08-02 12:59:08 sp_convert_shape_to_geography   Vars:   Declare Variables 
[SQLSTATE 01000] (Message 50000)  3 2018-08-02 12:59:08 ugp_2018-08-02 12:59:08 sp_convert_shape_to_geography   Vars:   Declare @Coords Temp Table 
[SQLSTATE 01000] (Message 50000)  3 2018-08-02 12:59:08 ugp_2018-08-02 12:59:08 sp_convert_shape_to_geography   Data:   Get Shape NodeType 
[SQLSTATE 01000] (Message 50000)  3 2018-08-02 12:59:08 ugp_2018-08-02 12:59:08 sp_convert_shape_to_geography   Data:   NodeType: POI 
[SQLSTATE 01000] (Message 50000)  3 2018-08-02 12:59:08 ugp_2018-08-02 12:59:08 sp_convert_shape_to_geography   Data:   POI: 500:Kingsfeld Pleasure Pt Depot 
[SQLSTATE 01000] (Message 50000)  3 2018-08-02 12:59:08 ugp_2018-08-02 12:59:08 sp_convert_shape_to_geography   Data:   Shape: -1:Point 
[SQLSTATE 01000] (Message 50000)  5 2018-08-02 12:59:08 ugp_2018-08-02 12:59:08 sp_convert_shape_to_geography   Point:  Lat/Lng: -33.967829, 150.979922 
[SQLSTATE 01000] (Message 50000)  4 2018-08-02 12:59:08 ugp_2018-08-02 12:59:08 sp_convert_shape_to_geography   Results:    Polygon Details: POLYGON ((150.98460895783668 -33.970080208112478, 150.98496365755059 -33.966194313013752, 150.98153028383197 -33.963525070916354, 150.97687086935508 -33.964106438622437, 150.97452729769748 -33.967487342906665, 150.97622732051045 -33.971121920298295, 150.98072449201612 -33.972286713684234, 150.98460895783668 -33.970080208112478)) 
[SQLSTATE 01000] (Message 50000)  3 2018-08-02 12:59:08 ugp_2018-08-02 12:59:08 sp_convert_shape_to_geography   Process:    End sp_convert_shape_to_geography 
[SQLSTATE 01000] (Message 50000)  3 2018-08-02 12:59:08 ugp_2018-08-02 12:59:08 sp_job_update_shapes_ex Creating polygon for shape POI: 557 
[SQLSTATE 01000] (Message 50000)  3 2018-08-02 12:59:08 ugp_2018-08-02 12:59:08 sp_convert_shape_to_geography   Process:    Begin sp_convert_shape_to_geography 
[SQLSTATE 01000] (Message 50000)  3 2018-08-02 12:59:08 ugp_2018-08-02 12:59:08 sp_convert_shape_to_geography   Vars:   Declare Variables 
[SQLSTATE 01000] (Message 50000)  3 2018-08-02 12:59:08 ugp_2018-08-02 12:59:08 sp_convert_shape_to_geography   Vars:   Declare @Coords Temp Table 
[SQLSTATE 01000] (Message 50000)  3 2018-08-02 12:59:08 ugp_2018-08-02 12:59:08 sp_convert_shape_to_geography   Data:   Get Shape NodeType 
[SQLSTATE 01000] (Message 50000)  3 2018-08-02 12:59:08 ugp_2018-08-02 12:59:08 sp_convert_shape_to_geography   Data:   NodeType: POI 
[SQLSTATE 01000] (Message 50000)  3 2018-08-02 12:59:08 ugp_2018-08-02 12:59:08 sp_convert_shape_to_geography   Data:   POI: 557:Tandem Tipper Hire 
[SQLSTATE 01000] (Message 50000)  3 2018-08-02 12:59:08 ugp_2018-08-02 12:59:08 sp_convert_shape_to_geography   Data:   Shape: -1:Point 
[SQLSTATE 01000] (Message 50000)  5 2018-08-02 12:59:08 ugp_2018-08-02 12:59:08 sp_convert_shape_to_geography   Point:  Lat/Lng: -33.935224, 150.940033 
[SQLSTATE 01000] (Message 50000)  4 2018-08-02 12:59:08 ugp_2018-08-02...  The step failed.

我尝试运行配置文件(SP模板),但是显然我做错了,因为尽管有存储输出,但我看不到存储过程开始。午餐后我会再试。

为什么使用Job Agent运行存储过程失败?我如何找到原因?

1 个答案:

答案 0 :(得分:0)

TSQL作业步骤在作业所有者的凭据下运行。在这里,它以NT AUTHORITY\SYSTEM的身份执行。将作业所有者更新为sa或其他具有正确凭据的用户。

或者,如果可能的话,或者在特定凭据下运行存储过程,例如

CREATE PROCEDURE <procedure name>
EXECUTE AS OWNER 

or

CREATE PROCEDURE <procedure name>
EXECUTE AS 'specificUser'

有关Execute AS的更多信息