문제

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