这是查询
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 :(得分:2)
这是你的问题:
intRevisionIndex = 1;
foreach (var revisionId in dIndexAndRevisionId)
{
cmd.Parameters.AddWithValue("@RevisionId" + intRevisionIndex, revisionId.Key);
}
intRevisionIndex
永远不会增加。
每次都"@RevisionId" + intRevisionIndex
= @RevisionId1
。