none
How to escape @ sign in a SQL Query RRS feed

  • Question

  • There is an unavoidable @ sign in my sql query (eg: @score := ...). When I going to execute it an error arsing, saying @score is undefined. I assume that this is because .net identifies this as in put parameter. Therefore I want to escape this @ sign and please help me on this.

    * The query is a stored procedure query read from a .sql file in a project.

    * I call ctx.Database.ExecuteSqlCommand(script); to execute query 

    Thank you.

     
    • Moved by DotNet Wang Tuesday, July 12, 2016 1:45 AM EF related
    Monday, July 11, 2016 10:52 AM

Answers

  • Hi Milan Suresh,

    According to your description, it seems that you use @score in your sql statement, but you have not create related SqlParameter. Please add related SqlParameter. like this:

    string sql = "INSERT INTO CUSTOMERS(CUSTOMERID,COMPANYNAME,CONTACTNAME,COUNTRY) 
                  VALUES(@P0,@P1,@P2,@P3)";
    List<object> parameterList = new List<object>();
    parameterList.Add("AAAAA");
    parameterList.Add("Company 1");
    parameterList.Add("Contact 1");
    parameterList.Add("USA");
    object[] parameters1 = parameterList.ToArray();
    int result = db.Database.ExecuteSqlCommand(sql, parameters);

    Best regards,

    Cole Wu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, July 12, 2016 8:31 AM
    Moderator

All replies

  • If @score is a column name, not a parameter and not a variable, then try [@score].

    Or give more details.

    Monday, July 11, 2016 11:01 AM
  • Hi Milan Suresh,

    According to your description, it seems that you use @score in your sql statement, but you have not create related SqlParameter. Please add related SqlParameter. like this:

    string sql = "INSERT INTO CUSTOMERS(CUSTOMERID,COMPANYNAME,CONTACTNAME,COUNTRY) 
                  VALUES(@P0,@P1,@P2,@P3)";
    List<object> parameterList = new List<object>();
    parameterList.Add("AAAAA");
    parameterList.Add("Company 1");
    parameterList.Add("Contact 1");
    parameterList.Add("USA");
    object[] parameters1 = parameterList.ToArray();
    int result = db.Database.ExecuteSqlCommand(sql, parameters);

    Best regards,

    Cole Wu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, July 12, 2016 8:31 AM
    Moderator