我想执行一个存储过程,使用夜间作业将数据填充到表中。
sp_WarehouseAttendance_New:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[sp_WarehouseAttendance_New]
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
---------delete existing warehouse table------------------------------
IF OBJECT_ID('dbo.WarehouseAttendance_New', 'U') IS NOT NULL
DROP TABLE dbo.WarehouseAttendance_New;
--------Create New Table--------------------------------------------------
---Weeks------------------------------------------------------------------
With weeks as
(select VDC_week_no, VDC_cal_year, min(VDC_day_date) as WeekCommence
from sql10.ng.dbo.Vdaily_calender
where VDC_avail = 'T'
group by VDC_week_no, VDC_cal_year),
---TTWeeks----------------------------------------------------------------
TTWeeks as
(SELECT VDC_Cal_Year as REMSYear, [VDC_week_no] as WholeYearWeekNo, WeekCommence,ROW_NUMBER() OVER(PARTITION BY vdc_cal_year ORDER BY VDC_week_no) as TermTimeWeekNo FROM weeks)
---Main----------------------------------------------------------------------
SELECT CASE WHEN STYR_Age_end_Aug < 16 THEN '1416' WHEN STYR_Age_end_Aug < 19 THEN '1618' ELSE '19+' END AgeBand,
REGT_Year, RTRIM(PRPH_ML1) AS PRPH_ML1,
RTRIM(PRPH_ML2) AS PRPH_ML2,
ML2.GNCD_Description AS [Curriculum Area],
RTrim(PRPH_ML2) AS Section,
q.GNCD_Description AS Section_Name, LEFT(q.GNCD_Description, 3) AS Dept,
rtrim(REGT_Provision_Code) as REGT_Provision_Code, rtrim(PRPH_Title) as PRPH_Title,
REGT_Student_ID, STEN_Student_ID, STEN_Funding_Stream, STYR_Age_end_Aug, REGS_Session_No,
rtrim(REGH_Class_Register) as REGH_Class_Register, RTRIM(REGH_Register_Title) as REGH_Register_Title, REGH_Day,
CASE WHEN REGH_Day = '1' THEN 'Sunday' WHEN REGH_Day = '2' THEN 'Monday'
WHEN REGH_Day = '3' THEN 'Tuesday' WHEN REGH_Day = '4' THEN 'Wednesday'
WHEN REGH_Day = '5' THEN 'Thursday' WHEN REGH_Day = '6' THEN 'Friday'
WHEN REGH_Day = '7' THEN 'Saturday' END AS Register_Day, [REGH_Start_Time] ,[REGH_End_Time],
CASE WHEN (REGTrgstudt.REGT_Provision_Code LIKE '27%' OR
REGT_Provision_Code LIKE 'MA27%' OR REGT_Provision_Code LIKE 'FS%')
THEN 'FunctionalSkill'
WHEN REGT_Provision_Code LIKE '16%' THEN 'GCSE' WHEN REGT_Provision_Code LIKE '%/F%' OR REGT_Provision_Code LIKE '%/D%' OR
REGT_Provision_Code LIKE '%/E%' OR
REGT_Provision_Code LIKE '%/X%' THEN 'Main' ELSE 'Addition' END AS CourseType,
CASE WHEN isnull(RGAT_Present,'X') IN ('N', 'Y','X') and REGS_Session_Date<GETDATE()
THEN REGS_Duration - isnull(REGD_Mins_Late,0) ELSE 0 END AS Mins_Poss,
CASE WHEN isnull(RGAT_Present,'X') = 'Y' and REGS_Session_Date<GETDATE() THEN
REGS_Duration - REGD_Mins_Late ELSE 0 END AS Mins_Att,
CASE WHEN RGAT_Present = 'Y' and REGS_Session_Date<GETDATE() THEN REGD_Mins_Late ELSE 0 END AS Mins_Late,
[REGS_Session_Date] as Session_Date,
TermTimeWeekNo, REGH_ISN, PRPH_ISN, STUD_Surname,
STUD_Forename_1, REGD_Attendance_Mark,
REGT_start_date, REGT_End_date, WeekCommence,
STFM_LearnFAMCode,
CASE
WHEN STFM_LearnFAMCode=1 THEN '14-15 year old learner is eligible for free meals'
WHEN STFM_LearnFAMCode=2 THEN '16-19 year old learner is eligible for and in receipt of free meals'
ELSE 'N/A'
END AS FreeMealsIndicator,
CASE
STFM_LearnFAMCode=36 THEN 'Care to Learn (C2L) (EFA funded only)'
STFM_LearnFAMCode=55 THEN '16-19 Bursary Fund - learner member of a vulnerable group (EFA funded only)'
WHEN STFM_LearnFAMCode=56 THEN '16-19 Bursary Fund - learner awarded discretionary bursary (EFA funded only)'
WHEN STFM_LearnFAMCode=57 THEN 'Residential support (EFA funded only)'
WHEN STFM_LearnFAMCode=58 THEN '19+ Hardship (Skills Funding Agency funded learners only)'
WHEN STFM_LearnFAMCode=59 THEN '20+ Childcare (Skills Funding Agency funded learners only)'
WHEN STFM_LearnFAMCode=60 THEN 'Residential Access Fund (Skills Funding Agency funded learners only)'
WHEN STFM_LearnFAMCode IN (61, 62, 63, 64, 65) THEN 'Unassigned'
ELSE 'N/A'
END AS Bursary, CASE WHEN STUD_Gender='M' THEN 'Male'
WHEN STUD_Gender='F' THEN 'Female'
END AS Gender,
CASE
WHEN STUD_Ethnicity= 31 THEN 'White - English / Welsh / Scottish / Northern Irish / British'
WHEN STUD_Ethnicity= 32 THEN 'White - Irish'
WHEN STUD_Ethnicity= 33 THEN 'White - Gypsy or Irish Traveller'
WHEN STUD_Ethnicity= 34 THEN 'White - Any Other White background'
WHEN STUD_Ethnicity= 35 THEN 'Mixed / Multiple Ethnic group - White and Black Caribbean'
WHEN STUD_Ethnicity= 36 THEN 'Mixed / Multiple Ethnic group - White and Black African'
WHEN STUD_Ethnicity= 37 THEN 'Mixed / Multiple Ethnic group - White and Asian'
WHEN STUD_Ethnicity= 38 THEN 'Mixed / Multiple Ethnic group - Any Other Mixed / multiple ethnic background'
WHEN STUD_Ethnicity= 39 THEN 'Asian / Asian British - Indian'
WHEN STUD_Ethnicity= 40 THEN 'Asian/ Asian British - Pakistani'
WHEN STUD_Ethnicity= 41 THEN 'Asian / Asian British - Bangladeshi'
WHEN STUD_Ethnicity= 42 THEN 'Asian / Asian British - Chinese'
WHEN STUD_Ethnicity= 43 THEN 'Asian / Asian British - Any other Asian background'
WHEN STUD_Ethnicity= 44 THEN 'Black / African / Caribbean / Black British - African'
WHEN STUD_Ethnicity= 45 THEN 'Black / African / Caribbean / Black British - Caribbean'
WHEN STUD_Ethnicity= 46 THEN 'Black / African / Caribbean / Black British - Any other Black / African / Caribbean background'
WHEN STUD_Ethnicity= 47 THEN 'Other ethnic group - Arab'
WHEN STUD_Ethnicity= 98 THEN 'Any Other'
WHEN STUD_Ethnicity= 99 THEN 'Not provided'
END AS Ethnicity,
CASE
WHEN g.GNUC_Flag_1 = 1 THEN 'Yes'
WHEN g.GNUC_Flag_1 = 0 THEN 'No'
END AS [Looked After Child],
CASE
WHEN SUB1.GNUC_Type = 'STYR' THEN 'Yes'
WHEN SUB1.GNUC_Type != 'STYR' THEN 'No'
END AS ALS
INTO WarehouseAttendance_New
FROM
sql10.ng.dbo.REGTrgstudt
INNER JOIN
sql10.ng.dbo.REGSrgsessn ON REGT_REGH_ISN = REGS_REGH_ISN
inner join sql10.ng.dbo.Vdaily_calender
ON REGS_Session_Date=VDC_day_date
INNER JOIN TTWeeks on REGT_year=TTWeeks.REMSYear
and VDC_week_no=WholeYearWeekNo and VDC_cal_year=REMSYear
INNER JOIN
sql10.ng.dbo.REGHrghdr ON REGH_ISN = REGT_REGH_ISN
INNER JOIN
sql10.ng.dbo.PRPHProvisionHeader ON REGT_Provision_Code = PRPH_Code
INNER JOIN
sql10.ng.dbo.GNCDgncodes AS ML2 ON ML2.GNCD_General_Code = PRPH_ML2 AND ML2.GNCD_Code_Type = 'M2'
INNER JOIN
sql10.ng.dbo.STEN ON STEN_Student_ID = REGTrgstudt.REGT_Student_ID AND STEN_Provision_Code = REGTrgstudt.REGT_Provision_Code AND
STEN_Provision_Instance = REGT_Provision_Instance
LEFT OUTER JOIN sql10.ng.dbo.STFMLearnerFAM ON STFM_Year = STEN_Year AND STFM_Student_ID = STEN_Student_ID
INNER JOIN
sql10.ng.dbo.STYRstudentYR ON STYR_Student_ID = REGT_Student_ID AND STYR_Year = REGT_Year
INNER JOIN (SELECT GNUC_Type, GNUC_Year, GNUC_Entity_ISN FROM sql10.ng.dbo.GNUCustom
WHERE (GNUC_Year IN ('2013', '2014', '2015'))) AS SUB1 ON REGT_Year = SUB1.GNUC_Year
AND STYR_ISN = SUB1.GNUC_Entity_ISN
INNER JOIN
(SELECT ACYR_College_Year
FROM sql10.ng.dbo.ACYR
WHERE (ACYR_College_Year in (2015, 2014, 2013, 2012))) AS SUB ON REGT_Year = SUB.ACYR_College_Year
INNER JOIN sql10.ng.dbo.STUDStudent on STYR_Student_ID=STUD_Student_ID
JOIN sql10.ng.dbo.GNUCustom g ON STUD_ISN = g.GNUC_Entity_ISN AND g.GNUC_Type = 'STUD'
INNER JOIN sql10.ng.dbo.REGDropin ON REGT_REGH_ISN = REGD_REGH_ISN AND REGT_Student_ID = REGD_Student_ID
AND
REGD_Session_No = REGS_Session_No
INNER JOIN
sql10.ng.dbo.RGATAttendance ON REGD_Attendance_Mark = RGAT_Attendance_Code
INNER JOIN
sql10.ng.dbo.PRPIProvisionInstance AS pit ON pit.PRPI_Code = REGT_Provision_Code
and pit.prpi_instance = REGT_Provision_Instance
INNER JOIN
(SELECT GNCD_General_Code, GNCD_Description
FROM sql10.ng.dbo.GNCDgncodes
WHERE (GNCD_Code_Type = 'M2')) AS q ON q.GNCD_General_Code = RTrim(PRPH_ML2)
END
GO
我想确保我在SQL Server代理中使用正确的SQL - &gt;步骤
DECLARE @return_value int
EXEC @return_value = [dbo].[sp_WarehouseAttendance_New]
SELECT 'Return Value' = @return_value
我想知道上面的查询是否删除了表中的现有数据,或者只是附加到不应该是这种情况的现有数据。
此外,每当我使用sql job或在查询中执行此过程时,它都会抛出锁定超时请求错误,如下所示。
请让我知道如何解决这个问题。
谢谢, 的Ar
下面的执行计划(源服务器 - SQL10.NG):
客户统计:
答案 0 :(得分:2)
你发布的声明是有效的,但更好的是你在申请之前测试,记住这个作业将是这句话的简单执行者,我不明白这个问题:
“我想知道上面的查询是否删除了表格中的现有数据”
此操作需要在您的SP中执行,如果您需要在执行之前清理所有数据,您可以使用
TRUNCATE TABLE YourTable
答案 1 :(得分:1)
在存储过程的开头,如果表已经存在,您的脚本将删除该表:
IF OBJECT_ID('dbo.WarehouseAttendance_New', 'U') IS NOT NULL
DROP TABLE dbo.WarehouseAttendance_New;
SELECT稍后会将数据重定向到具有相同名称的新表:
....
INTO WarehouseAttendance_New
...
您的答案的答案是:脚本删除表(如果存在),然后使用新数据再次创建。如果删除表失败,脚本将失败而不是附加数据
答案 2 :(得分:1)
t-clausen.dk的答案是正确的,我建议你尝试在你的Sp中添加一个TRANSACTION,我读过的关于这个主题的最好的文章是:
http://www.sommarskog.se/error_handling/Part1.html
其他方面是drop的真正必要性并且总是重新创建表,为什么不只是截断表?
答案 3 :(得分:1)
首先,如果您要使用drop table然后选择into,则需要在select into中指定架构。如果没有dbo权限的人(实际上应该是每个触摸数据库的人)执行proc,它将在错误的模式中创建一个新表,然后在表中找不到表的任何查询。所以
Into WarehouseAttendance_New
行应该是:
Into dbo.WarehouseAttendance_New
就我个人而言,我找不到drop table并选择除临时表之外的任何内容。实际上,您不会在新表上创建任何索引,这对以后的查询肯定是个问题。我希望你截断然后插入。根据记录的数量,更新任何已更改的记录并插入新记录可能会更好,而不是删除包含一百万条记录的表,然后重新插入一百万条记录,而实际上只有15-20条记录受到影响。
就您的性能问题而言,我对这个特定查询花费大量时间并不感到惊讶。首先,可能返回很多记录,第二,你有很多不具备高效的语法结构,包括case语句,大量连接,甚至还有一些喜欢使用通配符作为第一个字符。根据您从中获取此信息的数据库表的繁忙程度,您可能还会遇到很多阻塞。
这不是我们必须能够在互联网上帮助您的事情。这种类型的性能调整和设计是数据库专家在几小时或几天的工作中找到并解决问题的工作。
答案 4 :(得分:1)
我可以在执行计划中看到很多问题,在开始使用大量的Paralellism运算符和Hash Match时,我相信你创建一个新的帖子来询问解决明显问题的策略会更好与查询相关,请记住,问题不在于Jobs,主要问题是查询的成本以及这是通过链接服务器执行的事实。
问候!!!