I am trying to get a list of values in a SQL table as output based on an input parameter using the following SQL stored procedure.
CREATE PROCEDURE GetFirstNames
@LastName nvarchar(128),
@FirstNames nvarchar(128) OUTPUT
AS
SET @FirstNames = (SELECT FirstName FROM NamesTable WHERE LastName = @LastName)
GO
I am using the following code to get the list of first names from the table.
SqlParameter lastNameParam = new SqlParameter("@LastName", "Smith");
SqlParameter firstNamesParameter = new SqlParameter("@FirstNames", SqlDbType.NVarChar, 128);
firstNamesParameter.Direction = ParameterDirection.Output;
string sql = String.Format("EXEC dbo.GetFirstNames {0}, {1};",
lastNameParam.ParameterName,
firstNamesParameter.ParameterName);
context.Database.ExecuteSqlCommand(sql, lastNameParam, firstNamesParameter);
When I call the ExecuteSqlCommand method I get the following error:
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
- How I can resolve the query error so that I can get the list of first names?
- How can I return this list and use it in my c# code?
I would really appreciate if someone can help me with this. Thanks.