2011-10-25 149 views
-2

我有給我一定的查詢問題,我不知道如何解決這個問題..這裏是查詢..SQL Server 2008 R2的查詢錯誤:附近有語法錯誤:「

SELECT dcm.code DCM_No, 
     dcm.datetime_created DCM_Date, 
     lne.seq_number, 
     par.code, 
     par.description particular, 
     ft.description fee_type, 
    lne.amount, 
     case when lne.drcr =1 then 'Debit' else 'Credit' end DrCr, 

     case when isnull(dcm.is_sd,'N') = 'Y' then 'Salary Deduction' else null end is_sd, 
     org.description Company, 
     org.postal_address company_addr, 
     'S.Y. ' + substring(sy.school_year,1,4)+'-'+substring(sy.school_year,5,4) +' '+sem.description SY, 
     entity_a.code + ' - ' + coalesce(entity_a.stud_lname,'') +', '+coalesce(entity_a.stud_fname,'') +' '+coalesce(entity_a.stud_mname,'') Student, 

    case when isnull(dm.description2,'') = '' then 
       crs.course_short_name + '-'+convert(char(1),entity_a.stud_grade) 
     else crs2.course_short_name + '-'+convert(char(1),reg.stud_grade) end as Course, 
     isnull(dm.description2,'Not Enrolled') 

    FROM es.lib_rgn_students entity_a 
     left join ars.ars_dcm dcm on dcm.entity_id = entity_a.id 
     LEFT JOIN afs.entity entity_b ON dcm.prepared_by_id = entity_b.id 
     LEFT JOIN ua.user_account ON dcm.prepared_by_id = ua.user_account.id 
     left join ars.ars_dcm_line lne on lne.ars_dcm_id = dcm.id 
     left join es.lib_fin_fees_master par on par.id = lne.ars_particular_id 
     left join es.lib_fin_feetype ft on ft.code = lne.fee_code 
     left join es.stp_sysem sy on sy.id = dcm.period_id 
     inner join es.stp_semesterms sem on sem.code = sy.semester and sem.trimester = 0 
     inner join afs.organization org on org.id = sy.company_id 
     inner join es.lib_crs crs on crs.id = entity_a.course_id 
     left join es.trn_rgn_reg_hdr reg on reg.period = sy.id and reg.stud_id = :as_studid 
     left join document_status_map dm on dm.code = reg.document_status 
     left join es.lib_crs crs2 on crs2.id = reg.course_id 

    WHERE 
     (dcm.entity_id = :as_studid) AND 
     (dcm.period_id = :as_period) AND 
     (dcm.document_status_code not in(2,3))  

,我有這樣的錯誤:

Incorrect syntax near ':'.

請幫助我不擅長這個:(

感謝

+1

在管理工作室中運行它。雙擊錯誤消息。它向你顯示'reg.stud_id =:as_studid'是問題所在。如果您嘗試使用參數,那麼在TSQL中有一個「@」前綴。 –

+0

我不知道該怎麼辦這個問題:(我似乎有關於「:」的錯誤..我該怎麼辦?> _ < – ravenxi

+1

我們應該如何知道你根據什麼告訴我們?一個列的名稱?如果按照Ross的回答進行分隔,它是否應該是一個參數?如果是這樣,將其聲明爲TSQL參數並帶前綴'@' –

回答

1

:as_studid:as_period事情13759某些客戶端軟件,該查詢的最有可能的參數。在實際執行查詢之前,:name位被@P1@as_studid替換(通常由一些中間層軟件),所以服務器從不會在那裏看到冒號(並且從不抱怨)。

如果您不是客戶端軟件開發人員,並且被簡單地分配了執行查詢任務的任務,那麼您需要手動應用上述轉換。我建議你聲明變量@as_studid@as_period並分別用它們代替:as_studid:as_period。另外,您可能需要爲它們分配一些值,以便在運行查詢時可以看到一些結果。下面是修改後的腳本如何可能看起來像一個例子:

DECLARE 
    @as_studid int, 
    @as_period int; /* those are just my guesses, the actual types 
         might need to be something other than int */ 

SET @as_studid = 10; 
SET @as_period = 20; /* again, those are arbitrary values off the top 
         of my head but you might have to discover some 
         real ones before going on */ 

SELECT dcm.code DCM_No, 
     dcm.datetime_created DCM_Date, 
     lne.seq_number, 
     par.code, 
     par.description particular, 
     ft.description fee_type, 
    lne.amount, 
     case when lne.drcr =1 then 'Debit' else 'Credit' end DrCr, 

     case when isnull(dcm.is_sd,'N') = 'Y' then 'Salary Deduction' else null end is_sd, 
     org.description Company, 
     org.postal_address company_addr, 
     'S.Y. ' + substring(sy.school_year,1,4)+'-'+substring(sy.school_year,5,4) +' '+sem.description SY, 
     entity_a.code + ' - ' + coalesce(entity_a.stud_lname,'') +', '+coalesce(entity_a.stud_fname,'') +' '+coalesce(entity_a.stud_mname,'') Student, 

    case when isnull(dm.description2,'') = '' then 
      crs.course_short_name + '-'+convert(char(1),entity_a.stud_grade) 
     else crs2.course_short_name + '-'+convert(char(1),reg.stud_grade) end as Course, 
     isnull(dm.description2,'Not Enrolled') 

    FROM es.lib_rgn_students entity_a 
     left join ars.ars_dcm dcm on dcm.entity_id = entity_a.id 
     LEFT JOIN afs.entity entity_b ON dcm.prepared_by_id = entity_b.id 
     LEFT JOIN ua.user_account ON dcm.prepared_by_id = ua.user_account.id 
     left join ars.ars_dcm_line lne on lne.ars_dcm_id = dcm.id 
     left join es.lib_fin_fees_master par on par.id = lne.ars_particular_id 
     left join es.lib_fin_feetype ft on ft.code = lne.fee_code 
     left join es.stp_sysem sy on sy.id = dcm.period_id 
     inner join es.stp_semesterms sem on sem.code = sy.semester and sem.trimester = 0 
     inner join afs.organization org on org.id = sy.company_id 
     inner join es.lib_crs crs on crs.id = entity_a.course_id 
     left join es.trn_rgn_reg_hdr reg on reg.period = sy.id and reg.stud_id = :as_studid 
     left join document_status_map dm on dm.code = reg.document_status 
     left join es.lib_crs crs2 on crs2.id = reg.course_id 

WHERE 
     (dcm.entity_id = @as_studid) AND -- the vars declared above are used 
     (dcm.period_id = @as_period) AND -- here instead of the ':'-bits 
     (dcm.document_status_code not in(2,3)) 

或者,也可以不變量來做到,只需更換:as_studid:as_period用一些真實值。但是這樣你就不得不記住原始查詢中參數化了什麼,無論如何,如果你的任務是修改查詢本身,你可能必須提交它與: -names,就像你一樣收到了。

+0

非常感謝您!! iv'e一直在尋找答案關於這個大約2天現在..我真的不知道如何處理我發生的錯誤..我對此沒有太多的想法..我只是被分配去做這個..你救了我的生命非常感謝:D – ravenxi

0

如果您列確實被稱爲:as_studid然後用方括號括起來。 e.g.:-

WHERE 
     (dcm.entity_id = [:as_studid]) AND 
     (dcm.period_id = [:as_period]) AND 
     (dcm.document_status_code not in(2,3))  

這應該解決這個問題