2010-10-21 72 views
0

我遇到了訪問sql SELECT語句的問題。問題是,當後端是ACCESS-2007時它起作用;但是當後端是sql-server-2008時,它不會返回任何東西。這裏是語句:訪問sql語句不返回任何內容!

SELECT IIf([Lab Occurrence Form].[1 0 Preanalytical (Before Testing)] Like '*1.11*Other*','1.11 Other',[Lab Occurrence Form].[1 0 Preanalytical (Before Testing)]) AS [Occurrence Code], Count([Lab Occurrence Form].[1 0 Preanalytical (Before Testing)]) AS [Count] 
FROM [Lab Occurrence Form] 
WHERE ((([Lab Occurrence Form].[Occurrence Date]) Between Forms!Meeting_Reasons_Frequency!Text4 And Forms!Meeting_Reasons_Frequency!Text2)) And [Lab Occurrence Form].[1 0 Preanalytical (Before Testing)] Like '*1.*' 
GROUP BY IIf([Lab Occurrence Form].[1 0 Preanalytical (Before Testing)] Like '*1.11*Other*','1.11 Other',[Lab Occurrence Form].[1 0 Preanalytical (Before Testing)]) 
HAVING ((Count([Lab Occurrence Form].[1 0 Preanalytical (Before Testing)]))<>0) 
ORDER BY IIf([Lab Occurrence Form].[1 0 Preanalytical (Before Testing)] Like '*1.11*Other*','1.11 Other',[Lab Occurrence Form].[1 0 Preanalytical (Before Testing)]) 

請注意,什麼是不工作是這樣的:Forms!Meeting_Reasons_Frequency!Text4Forms!Meeting_Reasons_Frequency!Text2 - 當我更換那些他們工作的恆定值;然而,麻煩在於,由於某些原因,它們沒有正確讀取文本框中的內容。

再次:此查詢正在工作FINE與訪問作爲後端,但是當後端切換到SQL Server它不工作!原因再次是因爲forms!text的事。有誰知道爲什麼?

更新 多虧了鮑勃的建議,我運行了sql-server上的配置文件,發現有趣的東西。它確實工作正常,但它是這樣返回日期:

@P1 datetime,@P2 datetime','2010-04-30 00:00:00','2010-04-01 00:00:00' 
+2

IIF是訪問特定的,而不是SQL/TSQL – 2010-10-21 17:48:22

+0

@omg小馬:此查詢是由接入到SQL/T-SQL轉換。再次,它工作正常,如果我將這些值更改爲常量,而不是從文本框中獲取它們 – 2010-10-21 17:49:11

+0

您可以捕獲在SQL服務器上運行的SQL語句。你應該能夠使用SQL Server Profiler。 Access語句必須轉換爲SQL Server版本。 – bobs 2010-10-21 18:01:59

回答

1

嘗試定義爲參數表單控件引用:

PARAMETERS [Forms]![Meeting_Reasons_Frequency]![Text4] DateTime, 
    [Forms]![Meeting_Reasons_Frequency]![Text2] DateTime; 
    SELECT IIf([Lab Occurrence Form].[1 0 Preanalytical (Before Testing)] Like '*1.11*Other*','1.11 Other',[Lab Occurrence Form].[1 0 Preanalytical (Before Testing)]) AS [Occurrence Code], Count([Lab Occurrence Form].[1 0 Preanalytical (Before Testing)]) AS [Count] 
    FROM [Lab Occurrence Form] 
    WHERE ((([Lab Occurrence Form].[Occurrence Date]) Between Forms!Meeting_Reasons_Frequency!Text4 And Forms!Meeting_Reasons_Frequency!Text2)) And [Lab Occurrence Form].[1 0 Preanalytical (Before Testing)] Like '*1.*' 
    GROUP BY IIf([Lab Occurrence Form].[1 0 Preanalytical (Before Testing)] Like '*1.11*Other*','1.11 Other',[Lab Occurrence Form].[1 0 Preanalytical (Before Testing)]) 
    HAVING ((Count([Lab Occurrence Form].[1 0 Preanalytical (Before Testing)]))<>0) 
    ORDER BY IIf([Lab Occurrence Form].[1 0 Preanalytical (Before Testing)] Like '*1.11*Other*','1.11 Other',[Lab Occurrence Form].[1 0 Preanalytical (Before Testing)]) 
-2

這是在黑暗中拍攝,但我認爲這是你的樣子。 Access使用*作爲通配符,而SQL Server使用%。即使將語法轉換爲t-sql,我也不知道它是否也將*的值更改爲%。試試吧......

+0

@mario:聲明是在SQL服務器上工作沒有問題,問題是它不會從我的表單上的文本控件中獲取數據,那就是全部 – 2010-10-21 18:09:06

+0

嗯,我必須看看你的表單。 – Mario 2010-10-21 18:11:27

+0

@jenny:@馬里奧的答案可能是正確的。如果'*'沒有被替換爲'%',那麼搜索模式將是錯誤的,並且不會返回任何結果。 '*'將被字面解釋(列值中必須有星號才能匹配)。 – bobs 2010-10-21 18:12:55

1

你不能只是將控件堵塞到你的字符串中。

Between Forms!Meeting_Reasons_Frequency!Text4 
And Forms!Meeting_Reasons_Frequency!Text2 

您需要將它們分開,然後在即時窗口中查看您的SQL字符串。

"<beginning query> Between " & Forms!Meeting_Reasons_Frequency!Text4 & _ 
" And " & Forms!Meeting_Reasons_Frequency!Text2 & " <rest of query>" 

如果您在創建這個字符串的突破,你會發現,如果你將鼠標懸停在控件名稱文本框中的值將被顯示出來。

+0

你熟悉access sql嗎? – 2010-10-21 19:34:03

+0

@我是一個女孩 - 是的 – JeffO 2010-10-24 20:07:20