質問

Trying to execute the following query on northwind access database throws an exception:

No value given for one or more required parameters

My simplified query is

SELECT * FROM (SELECT [Orders].[OrderDate] FROM [Orders]) t 
WHERE [Orders].[CustomerID]=?

The exception is only thrown if nested SELECT is used.

Question: is there a way to use nested SELECT and parameters?

My code is:

OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + _databasePath);
conn.Open();

OleDbCommand com = new OleDbCommand(@"SELECT * 
           FROM (SELECT [Orders].[OrderDate] FROM [Orders]) t 
           WHERE [Orders].[CustomerID]=?", conn);

com.Parameters.Add("Bla", OleDbType.WChar);
com.ExecuteReader(CommandBehavior.CloseConnection);
役に立ちましたか?

解決

You have a couple problems. One, you haven't selected the CustomerID field in your subquery so you can't filter on it from the outer query. Second, you are referencing the wrong table alias in the outer query. Try this instead:

SELECT * 
FROM (SELECT [Orders].[OrderDate], [Orders].[CustomerID] FROM [Orders]) t 
WHERE t.[CustomerID]=?

Alternatively, you could not select the CustomerID and put your parameter in your subquery:

SELECT * 
FROM (SELECT [Orders].[OrderDate] FROM [Orders] WHERE [Orders].[CustomerID]=?) t 
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top