如何避免动态sql

时间:2011-10-12 13:58:08

标签: sql sql-server

我可以得到一些帮助,我试图避免动态sql,动态sql工作,但它有任何其他方式来构建这没有动态的SQL,我尝试了很多东西,但得到错误,你的帮助将不胜感激

declare @strSQL varchar(8000)

set @strSQL = 'Select 
ParentFName, ParentLName, ParentMiddleName, 
MailingAddressLine1, MailingAddressLine2, City, State, ZipCode,  
PS.SchoolID, PS.SchoolName, FirstName, LastName, MiddleName, 
CONVERT(varchar(12), SE.Birthday, 101) AS [Birthday], 
SE.GTID, SystemID,Grade, Report_Type, Race, Sex, NON_Funded_Flag, 
DATEDIFF(YY,Birthday , getdate()) -
CASE
WHEN(
  (MONTH(BirthDay)*100 + DAY(BirthDay)) >
  (MONTH(getdate())*100 + DAY(getdate()))
) THEN 1
ELSE 0 END AS Age,
KG, GR01_03, GR04_05, GR06_08_Middle_Grade, GR06_08_middle_school, 
GR09_12, EIP_KG, EIP_01_03,EIP_04_05,LEVEL_1,LEVEL_2,LEVEL_3,LEVEL_4,LEVEL_5,GIFTED, REMEDIAL,VOCATIONAL_HS_LAB, SED, --STUDY_HALL, 
NON_FUNDED, --POST_SECOND_OPTION,
ESOL_ITINERANT, ESOL_NON_ITINERANT, TOTAL_ESOL_SEGMENTS,
FiscalYear, TOTAL_FTE_SEGMENTS, AnnualTuition, 
CONVERT(varchar(12), SE.EnrollmentDate,101) AS [Enrollment Date], 
CONVERT(varchar(12), SE.WithdrawalDate,101) AS [Withdrawal Date],
WD.LookupValueDescription as WithdrawalReason,
FREE_REDUCED_LUNCH, PRIMARY_AREA, GNET_FLAG, 
EstAwardAmount--, previous_year
From 
StudentEnrollment SE 
    inner join (
        select * from openquery([FINANCE], ''select * from scholarship.sp_eligible_students Where Fiscal_Year = ''''' + convert(varchar, @FiscalYear) + ''''' '') ) ES ON
        SE.GTID = ES.GTID Inner Join 
        (select FP.FiscalYear, PrivateSchool.* from PrivateSchool
            INNER JOIN FiscalYearPrivateSchool FP ON PrivateSchool.PrivateSchoolID = FP.PrivateSchoolID AND FP.FiscalYear = ' + @FiscalYear + ') PS 
        ON SE.PrivateSchoolID = PS.PrivateSchoolID AND ES.Fiscal_Year =  PS.FiscalYear Left outer Join 
        vLookUps WD ON SE.WithdrawalreasonId = WD.LookUpID
    WHERE SE.isActive = 1 AND PS.isactive = 1
    AND (' + @SchoolID + ' = -1 or SchoolID = ' + @SchoolID + ' )
    AND (' + @FiscalYear + ' = -1 OR FiscalYear = ' + @FiscalYear + ')
ORDER BY SchoolName, LastName, Firstname, GTID'
--print (@strSQL)
exec (@strSQL)
END

1 个答案:

答案 0 :(得分:1)

SET NOCOUNT ON

declare @oq varchar(200)

set @og = 'select * from scholarship.sp_eligible_students Where Fiscal_Year ='+ convert(varchar, @FiscalYear)

select * into #a from openquery([FINANCE], @oq) 

Select  
ParentFName, ParentLName, ParentMiddleName,  
MailingAddressLine1, MailingAddressLine2, City, State, ZipCode,   
PS.SchoolID, PS.SchoolName, FirstName, LastName, MiddleName,  
CONVERT(varchar(12), SE.Birthday, 101) AS [Birthday],  
SE.GTID, SystemID,Grade, Report_Type, Race, Sex, NON_Funded_Flag,  
DATEDIFF(YY,Birthday , getdate()) - 
CASE 
WHEN( 
  (MONTH(BirthDay)*100 + DAY(BirthDay)) > 
  (MONTH(getdate())*100 + DAY(getdate())) 
) THEN 1 
ELSE 0 END AS Age, 
KG, GR01_03, GR04_05, GR06_08_Middle_Grade, GR06_08_middle_school,  
GR09_12, EIP_KG, EIP_01_03,EIP_04_05,LEVEL_1,LEVEL_2,LEVEL_3,LEVEL_4,LEVEL_5,GIFTED, REMEDIAL,VOCATIONAL_HS_LAB, SED,  
NON_FUNDED, 
ESOL_ITINERANT, ESOL_NON_ITINERANT, TOTAL_ESOL_SEGMENTS, 
FiscalYear, TOTAL_FTE_SEGMENTS, AnnualTuition,  
CONVERT(varchar(12), SE.EnrollmentDate,101) AS [Enrollment Date],  
CONVERT(varchar(12), SE.WithdrawalDate,101) AS [Withdrawal Date], 
WD.LookupValueDescription as WithdrawalReason, 
FREE_REDUCED_LUNCH, PRIMARY_AREA, GNET_FLAG,  
EstAwardAmount 
From  
StudentEnrollment SE  
    inner join #a ES ON 
        SE.GTID = ES.GTID Inner Join  
        (select FP.FiscalYear, PrivateSchool.* from PrivateSchool 
            INNER JOIN FiscalYearPrivateSchool FP ON PrivateSchool.PrivateSchoolID = FP.PrivateSchoolID AND FP.FiscalYear = @FiscalYear) PS  
        ON SE.PrivateSchoolID = PS.PrivateSchoolID AND ES.Fiscal_Year =  PS.FiscalYear Left outer Join  
        vLookUps WD ON SE.WithdrawalreasonId = WD.LookUpID 
    WHERE SE.isActive = 1 AND PS.isactive = 1 
    AND (@SchoolID = -1 or SchoolID = @SchoolID ) 
    AND (@FiscalYear = -1 OR FiscalYear = @FiscalYear) 
ORDER BY SchoolName, LastName, Firstname, GTID

drop table #a

如果失败的话。尝试修复它。