使用视图在sql server代理中创建作业

时间:2016-03-10 12:06:25

标签: sql sql-server view scheduled-tasks jobs

我有一个问题如果我可以通过使用每周作业在sql server代理中执行View来填充数据到表,还是只能使用存储过程?

Job_Steps

Job_Failed

View1: 
ALTER VIEW [dbo].[Destinations_1415]

AS

select
sy.styr_student_id,'2014/15' 'Year',q.Surname
,q.Forename,q.Course,pit.prpi_title
,CASE WHEN q.Section = 'BATT' THEN 'BATS' ELSE q.Section collate Latin1_General_CI_AS END AS'Section'
,q.Funding_Type,q.Funding_Body,q.Partner
,c.GNIC_Description 'Outcome',c2.GNIC_Description 'Outcome_Specific'
,q.Completion_Status,sd.stud_mobile_telephone,sd.stud_home_telephone_no
,CASE WHEN sy.STYR_Age_end_Aug < 16 THEN '1416' WHEN sy.STYR_Age_end_Aug < 19 THEN '1618' ELSE '19+' END AgeBand
from sql10.ng.dbo.styrstudentyr sy
Inner join(
SELECT  [Partner Name] 'Partner',[Funding Body] 'Funding_Body'
,[Funding Line] 'Funding_Type', [LearnRefNumber] 'learnerrefnumber'
,ROW_NUMBER()OVER(Partition By LearnRefNumber, [funding line] Order By CompStatus) 'row_no'
,[FamilyName] 'Surname',[GivenNames] 'Forename',[Full Name]
,[ProvSpecDelMon_A] 'Course',[ProvSpecDelMon_B] 'Section',[LearnAimRef] 'Learning_Aim'
,[LearnAimRefTitle] 'Learning_Aim_Title',[FundModel] 'Fundmodel'
,[CompStatus] 'Completion_Status'
FROM [CollegeReporting].[dbo].[PFR_1415_tbl]
where [Funding Line] not like '%appre%'
union all
SELECT  [Partner Name] 'Partner',[Funding Body] 'Funding_Body'
,[Funding Line] 'Funding_Type',[LearnRefNumber] 'learnerrefnumber'
,ROW_NUMBER()OVER(Partition By LearnRefNumber, [funding line] Order By CompStatus) 'row_no'
,[FamilyName] 'Surname',[GivenNames] 'Forename',[Full Name]
,[ProvSpecDelMon_A] 'Course',[ProvSpecDelMon_B] 'Section',[LearnAimRef] 'Learning_Aim'
,[LearnAimRefTitle] 'Learning_Aim_Title',[FundModel] 'Fundmodel',[CompStatus] 'Completion_Status'
FROM [CollegeReporting].[dbo].[PFR_1415_tbl]
where [Funding Line]  like '%appre%'
and LearnAimRef = 'ZPROG001')q on q.learnerrefnumber collate SQL_Latin1_General_CP1_CI_AS = sy.styr_student_id collate SQL_Latin1_General_CP1_CI_AS and q.row_no = '1'
left join sql10.NG.dbo.STDPOutcome o on o.STDP_Student_ID = sy.styr_student_id and o.STDP_Course_Text = '2014'
left join sql10.ng.dbo.GNICodes c on c.GNIC_Code = STDP_Outcome_Type and c.GNIC_Type = 'OUT'
left join sql10.ng.dbo.GNICodes c2 on c2.GNIC_Code = STDP_Outcome_Code and STDP_Outcome_Type = c2.GNIC_Type
left join sql10.ng.dbo.studstudent sd on sd.stud_student_id = sy.styr_student_id
inner join sql10.ng.dbo.PRPIProvisionInstance pit on pit.prpi_code collate Latin1_General_CI_AS = q.Course collate Latin1_General_CI_AS and pit.prpi_instance = '141501'
where sy.styr_year = '2014'

View 2:

ALTER VIEW [dbo].[Destinations_1516]
AS
select
sy.styr_student_id,'2015/16' as 'Year'
,q.Surname,q.Forename,q.Course
,pit.prpi_title,q.Section collate Latin1_General_CI_AS 'Section',q.Funding_Type
,q.Funding_Body,q.Partner,c.GNIC_Description 'Outcome'
,c2.GNIC_Description 'Outcome_Specific',q.Completion_Status
,sd.stud_mobile_telephone,sd.stud_home_telephone_no
, CASE WHEN sy.STYR_Age_end_Aug < 16 THEN '1416' WHEN sy.STYR_Age_end_Aug < 19 THEN '1618' ELSE '19+' END AgeBand
from sql10.ng.dbo.styrstudentyr sy
Inner join(
SELECT  Partner 'Partner'
,[Funding Body] 'Funding_Body',[Funding Line] 'Funding_Type'
,[LearnRefNumber] 'learnerrefnumber'
,ROW_NUMBER()OVER(Partition By LearnRefNumber, [funding line] Order By CompStatus) 'row_no'
,[FamilyName] 'Surname',[GivenNames] 'Forename'
,[Full Name],[coursecode] 'Course'
,[section] 'Section',[LearnAimRef] 'Learning_Aim'
,[coursetitle] 'Learning_Aim_Title',[Fund Model] 'Fundmodel'
,[CompStatus] 'Completion_Status'
FROM [CollegeReporting].[dbo].[PFR_1516]
where [Funding Line] not like '%appre%'
union all
SELECT  Partner 'Partner',[Funding Body] 'Funding_Body',[Funding Line] 'Funding_Type'
,[LearnRefNumber] 'learnerrefnumber'
,ROW_NUMBER()OVER(Partition By LearnRefNumber, [funding line] Order By CompStatus) 'row_no'
,[FamilyName] 'Surname',[GivenNames] 'Forename'
,[Full Name],[coursecode] 'Course'
,[section] 'Section',[LearnAimRef] 'Learning_Aim'
,[coursetitle] 'Learning_Aim_Title',[Fund Model] 'Fundmodel'
,[CompStatus] 'Completion_Status'
FROM [CollegeReporting].[dbo].[PFR_1516]
where [Funding Line]  like '%appre%'
and LearnAimRef = 'ZPROG001')q on q.learnerrefnumber collate SQL_Latin1_General_CP1_CI_AS = sy.styr_student_id collate SQL_Latin1_General_CP1_CI_AS and q.row_no = '1'
left join sql10.NG.dbo.STDPOutcome o on o.STDP_Student_ID = sy.styr_student_id and o.STDP_Course_Text = '2015'
left join sql10.ng.dbo.GNICodes c on c.GNIC_Code = STDP_Outcome_Type and c.GNIC_Type = 'OUT'
left join sql10.ng.dbo.GNICodes c2 on c2.GNIC_Code = STDP_Outcome_Code and STDP_Outcome_Type = c2.GNIC_Type
left join sql10.ng.dbo.studstudent sd on sd.stud_student_id = sy.styr_student_id
inner join sql10.ng.dbo.PRPIProvisionInstance pit on pit.prpi_code collate Latin1_General_CI_AS = q.Course collate Latin1_General_CI_AS and pit.prpi_instance = '151601'
where sy.styr_year = '2015'

View 3 (Union of View 1 and View2 ): 

ALTER VIEW [dbo].[Destinations_1415_1516_Union]

AS

SELECT * FROM [dbo].[Destinations_1415] 

UNION

SELECT * FROM [dbo].[Destinations_1516] 

错误:

OLE DB提供程序&#34; SQLNCLI10&#34;对于链接服务器&#34; sql10&#34;报告表#34;&#34; ng&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34; ; DBO&#34;&#34; styrstudentyr&#34;&#34;

请告诉我。

1 个答案:

答案 0 :(得分:1)

http://www.sqlservercentral.com/Forums/Topic542765-145-1.aspx

如上所述 - 重新创建视图,如果有任何重新创建同义词。

另一件事 - 如果您在视图定义中有select * - 您将保证此视图出现问题,需要定期重新创建此视图。例如,当底层视图或表的列列表发生更改时 - 无论列是否已删除,添加或更改它的位置(列顺序)。我现在不记得一个错误消息列表 - 其中一些与你得到的一样。关于编译和对象定义的 strange 。某些情况不会导致异常,但会产生意外结果。

您的代码中设计的另一个问题 - insert into没有指定列列表。您保存了一两分钟并且没有编写插入列顺序,没有编写选择列顺序,但是将不得不浪费更多时间来确定为什么一切都出错了。

效果问题:为什么有UNION代替UNION ALL?第一个视图为year返回一个常量列值,类似于第二个视图,这些常量也不同 - 那么为什么UNION?没有行可以重复。 Year列始终不同。

  1. 重新创建第一级视图和同义词
  2. 重写第二级视图以避免由select *
  3. 引起的union和不必要的排序+不同
  4. 使用已定义列列表重写插入