I am trying to execute a stored procedure called getLastFeatureUpdate
.
I will explain below the problem, step by step:
I have created a table in SQL like this:
CREATE TABLE testTable
(
DayTime INT NOT NULL, /*yyddhhmm, 1010102345*/
FeatureNbr SMALLINT NOT NULL,
Val FLOAT(53) NOT NULL
);
I have now created a stored procedure called getLastFeatureUpdate
. Important to notice here is that I use 2 parameters @maxDateTime
and @tableName
, as those are different each time. So I will then pass in those 2 parameters in the C# code that follows in the end.
The stored procedure (if I remove @tableName text
from the procedure and the C# code. Then the code does work to mention)
CREATE PROCEDURE getLastFeatureUpdate
@maxDateTime float(53) = 0,
@tableName text
AS
SELECT
test.FeatureNbr,
test.DayTime,
test.Val
FROM
@tableName test
WHERE
DayTime = (SELECT MAX(DayTime)
FROM @tableName
WHERE FeatureNbr = test.FeatureNbr --This is what you are missing
AND DayTime <= @maxDateTime) --10102248
The C# code where I want to return the data from testTable
. Which are shown in: MessageBox.Show(d1 + "," + d2 + "," + d3);
But here is where I get the error:
Procedure or function getLastFeatureUpdate has too many arguments specified
Notice, that if I don't pass on the 2nd row here with @tableName
, the code will work (I then have to remove @tableName
as a parameter also in the stored procedure getLastFeatureUpdate
)
cmd.Parameters.Add(new SqlParameter("@maxDateTime", 10102248));
cmd.Parameters.Add(new SqlParameter("@tableName", "testTable")); //If not using this parameter, the code will work
C# code:
void getLastFeatureUpdate()
{
using (SqlConnection conn = new SqlConnection(GetConnectionString()))
{
conn.Open();
// 1. create a command object identifying the stored procedure
SqlCommand cmd = new SqlCommand("getLastFeatureUpdate", conn);
// 2. set the command object so it knows to execute a stored procedure
cmd.CommandType = CommandType.StoredProcedure;
// 3. add parameter to command, which will be passed to the stored procedure
cmd.Parameters.Add(new SqlParameter("@maxDateTime", 10102248));
cmd.Parameters.Add(new SqlParameter("@tableName", "testTable")); //If not using this parameter, the code will work
// execute the command
using (SqlDataReader rdr = cmd.ExecuteReader())
{
// iterate through results, printing each to console
while (rdr.Read())
{
int v1 = (int)rdr["DayTime"];
int v2 = (Int16)rdr["FeatureNbr"];
double v3 = (double)rdr["Val"];
MessageBox.Show(v1 + "," + v2 + "," + v3);
}
}
}
}
static private string GetConnectionString()
{
return "Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=C:\\Users\\andre\\source\\repos\\TestDatabaseCreation\\DatabaseTest.mdf;Integrated Security=True;Connect Timeout=30";
}
You cannot parameterize the table name in SQL Server, so: that SQL is invalid, and the CREATE PROC
did not in fact run. What the contents of the old proc are: only you can know, but: it isn't the code shown. It was probably a dummy version you had at some point in development. Try typing:
exec sp_helptext getLastFeatureUpdate;
Specifically, the server should have told you:
Msg 1087, Level 16, State 1, Procedure getLastFeatureUpdate, Line 12 [Batch Start Line 0]
Must declare the table variable "@tableName".
Msg 1087, Level 16, State 1, Procedure getLastFeatureUpdate, Line 19 [Batch Start Line 0]
Must declare the table variable "@tableName".
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.