Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Incorrect syntax near stored procedure error

I'm updating a long list of records. In my code, everything run as predicted until it execute the query. I get an

Incorrect syntax near 'TempUpdatePhysicalCityStateZip'

(my stored procedure name). I've tested it with SQL Server Management Studio and it runs fine. So, I'm not quite sure where I got it wrong. Below is my stored procedure and code:

ALTER PROCEDURE [dbo].[TempUpdateCityStateZip] 
    @StoreNo nvarchar (11),
    @City nvarchar(50),
    @State nvarchar(2),
    @Zip nvarchar(5)    
AS
BEGIN
    SET NOCOUNT ON;

    UPDATE StoreContact
    SET City = @City, State = @State, Zip = @Zip
    WHERE StoreNo = @StoreNo
END

Here is my code:

Dictionary<string, string> CityStateZipList = getCityStateZipList(dbPath);

using (SqlConnection conn = new SqlConnection(dbPath))
{
    conn.Open();

    SqlCommand cmdUpdate = new SqlCommand("TempUpdateCityStateZip", conn);

    foreach (KeyValuePair<string, string> frKeyValue in CityStateZipList)
    {
        cmdUpdate.Parameters.Clear();

        string[] strCityStateZip = frKeyValue.Value.Split(' ');
        cmdUpdate.Parameters.AddWithValue("StoreNo", frKeyValue.Key.ToString());

        foreach (String i in strCityStateZip)
        {
            double zipCode;

            if (i.Length == 2)
            {
                cmdUpdate.Parameters.AddWithValue("State", i);
            }
            else if (i.Length == 5 && double.TryParse(i, out zipCode))
            {
                cmdUpdate.Parameters.AddWithValue("Zip", i);
            }
            else
            {
                cmdUpdate.Parameters.AddWithValue("City", i);
            }
        }

        cmdUpdate.ExecuteNonQuery();
    }
}
like image 475
Jack Avatar asked Nov 10 '08 16:11

Jack


People also ask

How do you resolve Incorrect syntax near?

This happens mostly when someone switched from one relational database to another relational database, from MySQL to MS SQL Server for example. Even though SQL is standard, each database management system has it own extensions thus the syntax might vary. So the solution is to search for the right query and replace it.

What does Incorrect syntax near mean in SQL?

This indicates there is an incorrect symbol in the criteria of the query.

How do I fix a stored procedure in SQL?

Use SQL Server Management StudioExpand Databases, expand the database in which the procedure belongs, and then expand Programmability. Expand Stored Procedures, right-click the procedure to modify, and then select Modify. Modify the text of the stored procedure. To test the syntax, on the Query menu, select Parse.

How do I force a stored procedure to recompile?

There are three ways to do this: WITH RECOMPILE option in the procedure definition or when the procedure is called, the RECOMPILE query hint on individual statements, or by using the sp_recompile system stored procedure.


2 Answers

I believe you can get that puzzling error message if you don't specify the command type:

cmdUpdate.CommandType = CommandType.StoredProcedure;
like image 138
DOK Avatar answered Oct 28 '22 09:10

DOK


Don't you need the @ sign before the parameter?

 cmdUpdate.Parameters.AddWithValue("@State", i);

FWIW, Thats kind of a dirty piece of code there, you will probably have many issues trying to maintain that. For performance reasons you may want to parse out the CityStateZipList before you open the connection, that way you aren't keeping it open longer than you need.

like image 22
StingyJack Avatar answered Oct 28 '22 10:10

StingyJack