2014-10-31 77 views
0

這裏多個參數不工作是查詢查詢在SQL Server中

SELECT Distinct NSS.RevisionId,NSS.SheetName,NSS.SubmitId,NSS.FileName, 
NSS.UpdateTimeA,NSS.UpdateUserId,NSS.NG, NSS.Complete, CD.ControlName, NSS.ItemName, 
NSS.ItemValue, NSS.ExternalFile 
FROM (
SELECT rs.SheetName ,rs.SubmitId,rs.FileName,rs.UpdateTimeA,rs.UpdateUserId,rs.NG, 
rs.Complete, rd.ItemName, rd.ItemValue, rs.RevisionId, rd.ExternalFile 


    FROM Revisions AS rs 
INNER JOIN RevisionsData AS rd 
ON rs.SubmitId = rd.SubmitId AND rs.RevisionId = rd.RevisionId 
WHERE rs.FileSubmitId = @FileSubmitId AND rs.SheetName= @SheetName 
AND ( rd.RevisionId = @RevisionId1 OR rd.RevisionId = @RevisionId2 ) 
AND ( rd.ItemName IN (@ItemName1 , @ItemName2 , @ItemName3 , @ItemName4 , @ItemName5 , 
@ItemName6 , @ItemName7 , @ItemName8 , @ItemName9 , @ItemName10 , @ItemName11 , 
@ItemName12 , @ItemName13 , @ItemName14 , @ItemName15 , @ItemName16))) AS NSS 

     LEFT JOIN CSVData AS CD 
     ON NSS.ItemName = CD.ControlKey 
     WHERE CD.FileId = @FileId 
     Order By NSS.RevisionId desc 

,這裏是錯誤

The variable name '@RevisionId1' has already been declared. Variable names must be unique 
within a query batch or stored procedure. 
Must declare the scalar variable "@RevisionId2" 

此查詢是完全在MS Access 2010,但在SQL Server工作2005年,給出錯誤。

和我這是怎麼創造這個查詢

cmd.CommandText = String.Format(@"SELECT Distinct NSS.RevisionId,NSS.SheetName,NSS.SubmitId,NSS.FileName,NSS.UpdateTimeA,NSS.UpdateUserId,NSS.NG, NSS.Complete, CD.ControlName, NSS.ItemName, NSS.ItemValue, NSS.ExternalFile FROM (
               SELECT rs.SheetName ,rs.SubmitId,rs.FileName,rs.UpdateTimeA,rs.UpdateUserId,rs.NG, rs.Complete, rd.ItemName, rd.ItemValue, rs.RevisionId, rd.ExternalFile 
                         FROM Revisions AS rs 
                         INNER JOIN RevisionsData AS rd 
                         ON rs.SubmitId = rd.SubmitId AND rs.RevisionId = rd.RevisionId 
                         WHERE rs.FileSubmitId = @FileSubmitId AND rs.SheetName= @SheetName AND ({0}) AND ({1})) AS NSS 
               LEFT JOIN CSVData AS CD 
               ON NSS.ItemName = CD.ControlKey 
               WHERE CD.FileId = @FileId 
               Order By NSS.RevisionId desc", strRevisionQuery, strItemsQuery); 

          cmd.Parameters.AddWithValue("@FileSubmitId", FileSubmitId); 
          cmd.Parameters.AddWithValue("@SheetName", sheet.Name); 
          // Revisions 
          intRevisionIndex = 1; 
          foreach (var revisionId in dIndexAndRevisionId) 
          { 
           cmd.Parameters.AddWithValue("@RevisionId" + intRevisionIndex, revisionId.Key); 
          } 
          //Sheets Name && Item Name 
          intItemsIndex = 1; 
          foreach (var gItems in sheet.g) 
          { 
           cmd.Parameters.AddWithValue("@ItemName" + intItemsIndex, gItems[0]); 
           intItemsIndex++; 
          } 
          cmd.Parameters.AddWithValue("@FileId", FileId); 
+0

這不是整個查詢。 – gvee 2014-10-31 12:19:25

+1

我會在這裏留下這個。 http://blogs.msmvps.com/jcoehoorn/blog/2014/05/12/can-we-stop-using-addwithvalue-already/ – 2014-10-31 14:45:26

回答

2

這是你的問題:

intRevisionIndex = 1; 
foreach (var revisionId in dIndexAndRevisionId) 
{ 
    cmd.Parameters.AddWithValue("@RevisionId" + intRevisionIndex, revisionId.Key); 
} 

intRevisionIndex永遠不會增加。

因此"@RevisionId" + intRevisionIndex = @RevisionId1每一次。