MS SQL代码健全性检查

时间:2017-03-07 22:22:41

标签: sql sql-server-2012 left-join

我对MS Sql Server及其语法相对较新,只是想确保我正确理解这段代码。我已经通过了一些旧代码,看到了这个:

select * into [FedScope Employment].[dbo].[2011_2015_Employment] from (select 
  A.Year,
  D.AGYT as ParentAgency,
  D.AGYSUBT as ChildAgency,
  E.LOCTYPT as Country,
  E.LOCT as State,
  B.AGELVLT AS Age,
  H.edlvlt as Education,
  A.gender as Gender,
  A.gsegrd as GSEGrade,
  F.LOSLVLT as LengthofService,
  G.OCCT as Occupation,
  G.OCCFAMT as OccupationFamily,
  G.OCCTYPT as OccupationCategory,
  A.PPGRD as PayGrade,
  I.SALLVLT as SalaryLevel,
  A.TOA as TypeOfAppointment,
  J.WORKSCHT as WorkSchedule,
  A.Salary as Salary,
  A.LOS as LevelOfService,
  A.DATECODE as EffectiveDate,
  'Null' as SeparationType,
  'Null' as AccessionType,
  A.FedscopeType as FedscopeType

  from [FedScope Employment].[dbo].[2011_2015_Employment_raw] A Left JOIN 
    [FedScope Accessions].[dbo].[DTagelvl] B   ON A.AGELVL= B.AGELVL Left JOIN 
    [FedScope Accessions].[dbo].[FY14_DTagy]  D ON A.AGYSUB = D.AGYSUB Left JOIN
    [FedScope Accessions].[dbo].[DTloc] E ON A.LOC = E.LOC Left JOIN
    [FedScope Accessions].[dbo].[DTloslvl] F ON A.LOSLVL = F.LOSLVL Left JOIN
    [FedScope Accessions].[dbo].[FY14_DTocc] G ON A.OCC = G.OCC Left JOIN
    [FedScope Employment].[dbo].[FY15_DTedlvl] H on A.EDLVL = H.EDLVL Left JOIN
    [FedScope Accessions].[dbo].[DTsallvl] I on A.SALLVL = I.SALLVL Left JOIN
    [FedScope Accessions].[dbo].[DTwrksch] J on A.WORKSCH = J.WORKSCH) as tmp;

我对代码最开头的两个select语句感到困惑。代码不应该是这样的吗?

select 
  A.Year,
  D.AGYT as ParentAgency,
  D.AGYSUBT as ChildAgency,
  E.LOCTYPT as Country,
  E.LOCT as State,
  B.AGELVLT AS Age,
  H.edlvlt as Education,
  A.gender as Gender,
  A.gsegrd as GSEGrade,
  F.LOSLVLT as LengthofService,
  G.OCCT as Occupation,
  G.OCCFAMT as OccupationFamily,
  G.OCCTYPT as OccupationCategory,
  A.PPGRD as PayGrade,
  I.SALLVLT as SalaryLevel,
  A.TOA as TypeOfAppointment,
  J.WORKSCHT as WorkSchedule,
  A.Salary as Salary,
  A.LOS as LevelOfService,
  A.DATECODE as EffectiveDate,
  'Null' as SeparationType,
  'Null' as AccessionType,
  A.FedscopeType as FedscopeType


into [FedScope Employment].[dbo].[2011_2015_Employment]

  from [FedScope Employment].[dbo].[2011_2015_Employment_raw] A Left JOIN 
    [FedScope Accessions].[dbo].[DTagelvl] B   ON A.AGELVL= B.AGELVL Left JOIN 
    [FedScope Accessions].[dbo].[FY14_DTagy]  D ON A.AGYSUB = D.AGYSUB Left JOIN
    [FedScope Accessions].[dbo].[DTloc] E ON A.LOC = E.LOC Left JOIN
    [FedScope Accessions].[dbo].[DTloslvl] F ON A.LOSLVL = F.LOSLVL Left JOIN
    [FedScope Accessions].[dbo].[FY14_DTocc] G ON A.OCC = G.OCC Left JOIN
    [FedScope Employment].[dbo].[FY15_DTedlvl] H on A.EDLVL = H.EDLVL Left JOIN
    [FedScope Accessions].[dbo].[DTsallvl] I on A.SALLVL = I.SALLVL Left JOIN
    [FedScope Accessions].[dbo].[DTwrksch] J on A.WORKSCH = J.WORKSCH) as tmp;

这两个陈述是一回事吗?试着想想为什么需要选择*进入然后选择,或者如果那是完全不需要的。感谢。

0 个答案:

没有答案