Forum Discussion

raskenny's avatar
raskenny
Occasional Contributor
13 years ago

SQL parametized insert

Could someone help me with the syntax preferably jScript for inserting data into DB table using parameters.

i am getting an error with the parameters Thanks



Function InsertToDb(var1 ,var2)

 {  

       var Qry;

      Qry = ADO.CreateADOQuery();

        var conStr = "Provider=*****.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=####;Data Source=   ####";       

      Qry.ConnectionString = conStr;

         try     

        {   

           Qry.SQL = "INSERT INTO example (column1,column2) VALUES >= :var1,:var2 ";  //

           Qry.Parameters.ParamByName("column1").Value = var1;

           Qry.Parameters.ParamByName("column2").Value = var2;



           Qry.ExecSQL();

        }

        catch(e ) 

      {            Log.Error(e.description);        }                     


       Qry.Close();   }





   

6 Replies

  • raskenny's avatar
    raskenny
    Occasional Contributor
      i get error " Parameter var1 not found "

     


    All i want to do is insert whatever paameters passed to the method into the database i just cant figure outh the syntax for the insert statement.



    selection statements work fine for me using parameters but not insert



    Thanks 

  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor
    I don't think you need to worry about using parameters on the query itself... you can do it simply with concatenated strings like so...



    function InsertToDb(var1 ,var2)

    {  

        var Qry;

        Qry = ADO.CreateADOQuery();

        var conStr = SQLUtilities.ConnectionString; 

        Qry.ConnectionString = conStr;

        try     

        {   

            Qry.SQL = "INSERT INTO example (column1,column2) VALUES (\'" + var1 + "\',\'" + var2 +"\')"

            Qry.ExecSQL();

        }

        catch(e ) 

        {

            Log.Error(e.description);        

        }                     

        Qry.Close();   

    }




    Note that, in my example, I'm assuming Var1 and Var2 are strings so I needed to include the "\'" special character in order to designate the string identifiers.
  • raskenny's avatar
    raskenny
    Occasional Contributor
    I am callinng the method



    InsertToDB("test","test2"); //passed as strings



    i get an error 

    Incorrect syntax near 's (Column1,Column2) VALUES ( '



  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor
    Paste in what you have because I did pretty much your exact same test and it worked fine...  Would like to see if, perhaps, you're missing something.
  • raskenny's avatar
    raskenny
    Occasional Contributor
    Robert , it worked after checking it twice.

    Thanks