我创建一个Control-M作业来执行python脚本。该脚本用于在SQL Server上执行存储过程。存储过程将从表A中删除一些记录,然后打开表B的游标,然后根据游标中的信息将记录插入表A.对于表C和表D,将插入另外两个游标。
当我在SQL Server Management Studio 2012上手动运行存储过程时,它运行良好。例如,在运行存储过程后,表A将有400,000条记录。
但是,在运行control-M作业后,表A只有390,000条记录。通过将存储过程拆分为四个部分来解决该问题,一个用于从表A中删除记录,另一个用于根据游标为表B,C和D返回的记录将记录插入到表A中。
任何人都可以告知根本原因吗?
Python脚本
import pyodbc as mssql
import re
import base64
def ExecuteSQL(ConnectionString, SqlQuery):
print('Executing query.')
connection = ConnectionString
connectionTest = mssql.connect(connection, autocommit=True)
cursor = connectionTest.cursor()
cursor.execute(SqlQuery)
print('Query executed.')
connectionTest.commit()
if __name__ == '__main__':
with open(
r'<path>'
r'<configfile>','r') as config:
for line in config:
if re.match('^SERVER', line):
server = line.split(':')[1].strip()
elif re.match('^DB', line):
db = line.split(':')[1].strip()
elif re.match('^UID', line):
uid_en = line.split(':')[1].strip()
uid = base64.b64decode(uid_en).decode('utf-8')
elif re.match('^PWD', line):
pwd_en = line.split(':')[1].strip()
pwd = base64.b64decode(pwd_en).decode('utf-8')
connection = 'DRIVER={ODBC Driver 11 for SQL Server};SERVER=' + server + ';DATABASE=' + db + ';UID=' + uid + ';PWD=' + pwd
sql = 'EXEC MySchema.[USP_DATA_QUALITY_CHECK]'
ExecuteSQL(connection, sql)
存储过程
USE <MyDataBase>
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE MySchema.[USP_DATA_QUALITY_CHECK]
AS
BEGIN
--Delete raw score records for entities deactivated.
DELETE FROM MySchema.RPT_RAWSCORE
WHERE EntityID NOT IN (
SELECT PropertyID FROM MySchema.STG_PROPERTY
)
AND EntityType = 1
DELETE FROM MySchema.RPT_RAWSCORE
WHERE EntityID NOT IN (
SELECT AvailabilityID FROM MySchema.STG_AVAILABILITY
)
AND EntityType = 5
DELETE FROM MySchema.RPT_RAWSCORE
WHERE EntityID NOT IN (
SELECT ListingID FROM MySchema.STG_LISTING
)
AND EntityType = 6
--Delete raw score records that need to redo data quality check.
DELETE FROM MySchema.RPT_RAWSCORE
WHERE EntityID IN (
SELECT PropertyID FROM MySchema.STG_PROPERTY WHERE SkipDQ = 0
)
AND EntityType = 1
DELETE FROM MySchema.RPT_RAWSCORE
WHERE EntityID IN (
SELECT AvailabilityID FROM MySchema.STG_AVAILABILITY WHERE SkipDQ = 0
)
AND EntityType = 5
DELETE FROM MySchema.RPT_RAWSCORE
WHERE EntityID IN (
SELECT ListingID FROM MySchema.STG_LISTING WHERE SkipDQ = 0
)
AND EntityType = 6
UPDATE MySchema.RPT_RAWSCORE
SET DateKey = CONVERT(INT,CONVERT(VARCHAR, GETDATE(), 112))
DECLARE @Entity nvarchar(256), @DateKey INT, @market INT
DECLARE @getEntityID CURSOR
SELECT @DateKey=CONVERT(INT,CONVERT(VARCHAR, GETDATE(), 112))
SET @getEntityID = CURSOR FOR
SELECT PropertyID FROM MySchema.STG_PROPERTY WHERE SkipDQ = 0
OPEN @getEntityID
FETCH NEXT
FROM @getEntityID INTO @Entity
WHILE @@FETCH_STATUS = 0
BEGIN
exec MySchema.[USP_Completeness_Check] @DateKey,@Entity,@entityID=1
exec MySchema.[USP_Accuracy_Check] @DateKey,@Entity,@entityID=1
FETCH NEXT FROM @getEntityID INTO @Entity
END
CLOSE @getEntityID;
DEALLOCATE @getEntityID;
SET @getEntityID = CURSOR FOR
SELECT AvailabilityID FROM MySchema.STG_Availability WHERE SkipDQ = 0
OPEN @getEntityID
FETCH NEXT
FROM @getEntityID INTO @Entity
WHILE @@FETCH_STATUS = 0
BEGIN
exec MySchema.[USP_Completeness_Check] @DateKey,@Entity,@entityID=5
exec MySchema.[USP_Accuracy_Check] @DateKey,@Entity,@entityID=5
FETCH NEXT FROM @getEntityID INTO @Entity
END
CLOSE @getEntityID;
DEALLOCATE @getEntityID;
SET @getEntityID = CURSOR FOR
SELECT ListingID FROM MySchema.STG_LISTING WHERE SkipDQ = 0
OPEN @getEntityID
FETCH NEXT
FROM @getEntityID INTO @Entity
WHILE @@FETCH_STATUS = 0
BEGIN
exec MySchema.[USP_Completeness_Check] @DateKey,@Entity,@entityID=6
exec MySchema.[USP_Accuracy_Check] @DateKey,@Entity,@entityID=6
FETCH NEXT FROM @getEntityID INTO @Entity
END
CLOSE @getEntityID;
DEALLOCATE @getEntityID;
END;
GO