2017-10-06 112 views
0

我第一次使用AccessSQL查詢不能在Access中工作

下面的查詢在SQL Server中工作,但是當我把它帶入存取時,我得到了下面的錯誤信息。

DECLARE @CandidateNumber INT 
SET @CandidateNumber = 5921368 
UPDATE XXX 
SET @CandidateNumber = CandidateNumber = @CandidateNumber + 1 
GO 

錯誤消息:無效的SQL語句預期「刪除」「插入」,「選擇」,「更新」

+1

訪問不使用像這樣的變量,所以這絕對不會工作。這個陳述是做什麼的?也許我們可以幫助用一種可以在Access中工作的方式來重寫。 – JNevill

+0

嗨@JNevill謝謝你的幫助。我想添加一列到x行的表中。這一欄中的第一個數字應該是5921368,最後一個數字應該是5921368 + x。 –

回答

0

如果您正在使用從SQL-Server鏈接表時,您可以使用Access中的pass-through query。這種類型的查詢不是由Access引擎(JetEngine)執行的,而是直接發送到執行它的SQL-Server。


如果您正在使用真正的Access查詢,那麼請使用參數。 請參閱:Passing parameter to query for Access database

+0

不幸的是,這不是。該表格包含在Access中。 –

0

它不起作用,因爲Access使用與SQL Server不同的SQL方言。特別是Access SQL(JET)不支持DECLARE -ing變量。

不幸的是,JET SQL沒有很好的記錄在微軟,但有第三方資源可用(例如,一個JET Database Wikibook)。

+0

謝謝@Hininzi。我不知道他們是不同的。 –

0

也許你所需要的只是一個帶有行計數器的查詢

您可以使用此功能,只需添加CandidateNumber值:

Public Function RowCounter(_ 
    ByVal strKey As String, _ 
    ByVal booReset As Boolean, _ 
    Optional ByVal strGroupKey As String) _ 
    As Long 

' Builds consecutive RowIDs in select, append or create query 
' with the possibility of automatic reset. 
' Optionally a grouping key can be passed to reset the row count 
' for every group key. 
' 
' Usage (typical select query): 
' SELECT RowCounter(CStr([ID]),False) AS RowID, * 
' FROM tblSomeTable 
' WHERE (RowCounter(CStr([ID]),False) <> RowCounter("",True)); 
' 
' Usage (with group key): 
' SELECT RowCounter(CStr([ID]),False,CStr[GroupID])) AS RowID, * 
' FROM tblSomeTable 
' WHERE (RowCounter(CStr([ID]),False) <> RowCounter("",True)); 
' 
' The Where statement resets the counter when the query is run 
' and is needed for browsing a select query. 
' 
' Usage (typical append query, manual reset): 
' 1. Reset counter manually: 
' Call RowCounter(vbNullString, False) 
' 2. Run query: 
' INSERT INTO tblTemp (RowID) 
' SELECT RowCounter(CStr([ID]),False) AS RowID, * 
' FROM tblSomeTable; 
' 
' Usage (typical append query, automatic reset): 
' INSERT INTO tblTemp (RowID) 
' SELECT RowCounter(CStr([ID]),False) AS RowID, * 
' FROM tblSomeTable 
' WHERE (RowCounter("",True)=0); 
' 
' 2002-04-13. Cactus Data ApS. CPH 
' 2002-09-09. Str() sometimes fails. Replaced with CStr(). 
' 2005-10-21. Str(col.Count + 1) reduced to col.Count + 1. 
' 2008-02-27. Optional group parameter added. 
' 2010-08-04. Corrected that group key missed first row in group. 

    Static col  As New Collection 
    Static strGroup As String 

    On Error GoTo Err_RowCounter 

    If booReset = True Then 
    Set col = Nothing 
    ElseIf strGroup <> strGroupKey Then 
    Set col = Nothing 
    strGroup = strGroupKey 
    col.Add 1, strKey 
    Else 
    col.Add col.Count + 1, strKey 
    End If 

    RowCounter = col(strKey) 

Exit_RowCounter: 
    Exit Function 

Err_RowCounter: 
    Select Case Err 
    Case 457 
     ' Key is present. 
     Resume Next 
    Case Else 
     ' Some other error. 
     Resume Exit_RowCounter 
    End Select 

End Function 

見內嵌的典型用法評論。