出于某种原因,我无法在此处获得正确的语法来添加OPTION
Database db = DatabaseFactory.CreateDatabase(Config.DbConnectionString);
using (DbCommand cmd = db.GetSqlStringCommand(string.Format(@" SELECT t.customerID,
t.Photo
FROM SomeTable t
WHERE t.PhotoID = (SELECT MAX(PhotoID) FROM SomeTable WHERE customerID = @{0} OPTION (OPTIMIZE FOR UNKNOWN)
GROUP BY CustomerID ) ", "customerID")))
{
try
{
db.AddInParameter(cmd, "@customerID", DbType.String, customerID);
答案 0 :(得分:3)
您在子查询之后,在分组之前放置了提示。
看起来应该是这样的:
SELECT t.customerID,
t.Photo
FROM SomeTable t
WHERE t.PhotoID = (
SELECT MAX(PhotoID)
FROM SomeTable
WHERE customerID = @{0}
)
GROUP BY CustomerID
OPTION (OPTIMIZE FOR UNKNOWN)
您的混淆可能是由于提示与参数值有关,因此您认为必须将其放在参数之后。但OPTION
总是引入了query hint。也许,牢记这一点将有助于您记住正确的语法,即OPTION
应该始终在整个查询的末尾。