2014-09-23 26 views
1

我已通過SO搜索,但未找到任何有助於解決問題的內容。我不知道如何解釋有關我的查詢的錯誤消息。TSQL錯誤:在期望條件的上下文中指定的非布爾類型的表達式

我的查詢是:

select AST_ID, 
    case when CRA_StatusID=1 then 'Wait on Info' 
    when CRA_StatusID=2 then 'Wait PrePay' 
    when CRA_StatusID=3 then 'Acquire Chart' 
    when CRA_StatusID=4 then 'Copy Chart' 
    when CRA_StatusID=5 then 'Need Invoice' 
    when CRA_StatusID=6 then 'Wait Payment' 
    when CRA_StatusID=7 then 'Ready for Delv' else 'Complete' end as AST_Status, 
    case when AST_WOPrinted is null then '' else 'Y' end as AST_WOPrinted, 
    case when AST_DeliveryDate is null then '' else 'Y' end as AST_Delivered, 
    AST_PatientLName+'', ''+AST_PatientFName+' '+AST_PatientMName as PatientName, 
    case when len(AST_RequestorName) > 0 then AST_RequestorName else AST_RequestorContact end as AST_RequestorName, 
    AST_Created,AST_ProviderName 
    from dbo.AST 
    inner join dbo.fnASTCurrentStatus() on AST_ID=CRA_ASTID 
    where ' + @WhereClause + ' 

    union all 
    select AST_ID, 
    case 
    when CRA_StatusID=1 then 'Wait on Info' 
    when CRA_StatusID=2 then 'Wait PrePmt' 
    when CRA_StatusID=3 then 'Aquire Chart' 
    when CRA_StatusID=4 then 'Copy Chart' 
    when CRA_StatusID=5 then 'Need Invc' 
    when CRA_StatusID=6 then 'Wait Pmt' 
    when CRA_StatusID=7 then 'Ready for Delv' else 'Complete' 
    end as AST_Status, 
    case when AST_WOPrinted is null then '' else 'Y' end as AST_WOPrinted, 
    case when AST_DeliveryDate is null then '' else 'Y' end as AST_Delivered, 
    AST_PatientLName+'', ''+AST_PatientFName+' '+AST_PatientMName as PatientName, 
    case when len(AST_RequestorName) > 0 then AST_RequestorName else AST_RequestorContact end as AST_RequestorName, 
    AST_Created,AST_ProviderName 
    from dbo.Archive_AST 
    inner join dbo.fnArchiveASTCurrentStatus() on AST_ID=CRA_ASTID 
    where ' + @WhereClause + ' 



set @WhereClause=' AST_ProviderID in (select ProviderID from dbo.UserProvider where CSA_UserID = ' + convert(varchar,55) + ')' 

錯誤信息上寫着:

An expression of non-boolean type specified in a context where a condition is expected, near 'union'.

An expression of non-boolean type specified in a context where a condition is expected, near ' + @WhereClause + '.

我怎樣才能解決這個錯誤嗎?

+4

'where'+ @WhereClause +''不是有效的where子句..? – thebjorn 2014-09-23 19:49:24

+0

@whereClause在存儲過程中單獨定義。 – user3929962 2014-09-23 19:54:14

+0

@where的價值是什麼?只是一個例子,所以我們可以理解它的樣子。但是,是的,比爾森是正確的,你的條款是無效的 – SQLChao 2014-09-23 19:55:58

回答

3

你不能這樣做。您正在將純SQL查詢與動態SQL混合使用。您必須選擇是否使用動態SQL。如果您這樣做,請檢查EXECUTE關鍵字或sp_executesql系統存儲過程。

如果是動態SQL,您的where子句變量應該發生構建查詢之前

您查詢很容易被寫成這樣:

保持整個查詢到WHERE子句和

... 
WHERE AST_ProviderID in (select ProviderID from dbo.UserProvider where CSA_UserID = convert(varchar,55)) 

如果你願意,你也可以使用一個變量或參數化查詢:

DECLARE @myvar INT = 55; 
... 
WHERE AST_ProviderID in (select ProviderID from dbo.UserProvider where CSA_UserID = convert(varchar, @myvar)) 
+0

查詢是動態sql。我將它轉換爲常規的SQL以便在SO上發佈,以便更容易理解 – user3929962 2014-09-23 20:04:37

+0

@ user3929962您的錯誤消息是指您在何處構建SQL,而不是在何處執行它。您尚未正確轉換它。 – Laurence 2014-09-23 20:06:23

1

基於其它響應,你應該做一個Print @WhereClause,看到輸出的格式化,但它絕對是@whereclause造成問題

相關問題