Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Variable In SQL String

Variable In SQL String

Scheduled Pinned Locked Moved Database
databasedata-structurestutorial
9 Posts 4 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mike Certini
    wrote on last edited by
    #1

    I am currently trying to code a variable into a SQL string that is used with ADO to create a record within a database. I though am having problems with the proper syntax for a variable. The string works fine with a number but when I try to code a variable I have problems. I need to find out how to code an array as well.

    hr = rec2->Open("INSERT INTO mytable(id)VALUES("+ text +")",con2.GetInterfacePtr(), ADODB::adOpenForwardOnly, ADODB::adLockReadOnly, ADODB::adCmdText);

    L M W 3 Replies Last reply
    0
    • M Mike Certini

      I am currently trying to code a variable into a SQL string that is used with ADO to create a record within a database. I though am having problems with the proper syntax for a variable. The string works fine with a number but when I try to code a variable I have problems. I need to find out how to code an array as well.

      hr = rec2->Open("INSERT INTO mytable(id)VALUES("+ text +")",con2.GetInterfacePtr(), ADODB::adOpenForwardOnly, ADODB::adLockReadOnly, ADODB::adCmdText);

      M Offline
      M Offline
      Mycroft Holmes
      wrote on last edited by
      #2

      You need to enclose your varchar value in single quotes.

      hr = rec2->Open("INSERT INTO mytable(id)VALUES('"+ text +"')",con2.GetInterfacePtr(), ADODB::adOpenForwardOnly, ADODB::adLockReadOnly, ADODB::adCmdText);

      I find string.Format() to be VERY useful!

      Never underestimate the power of human stupidity RAH

      1 Reply Last reply
      0
      • M Mike Certini

        I am currently trying to code a variable into a SQL string that is used with ADO to create a record within a database. I though am having problems with the proper syntax for a variable. The string works fine with a number but when I try to code a variable I have problems. I need to find out how to code an array as well.

        hr = rec2->Open("INSERT INTO mytable(id)VALUES("+ text +")",con2.GetInterfacePtr(), ADODB::adOpenForwardOnly, ADODB::adLockReadOnly, ADODB::adCmdText);

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        in SQL string literals need quotes, so I would suggest you try:

        ...VALUES('"+ text +"')"...
        ^ ^

        Warning: make sure not to insert anything between the single and double quotes there! :)

        Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

        Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

        M 2 Replies Last reply
        0
        • L Luc Pattyn

          in SQL string literals need quotes, so I would suggest you try:

          ...VALUES('"+ text +"')"...
          ^ ^

          Warning: make sure not to insert anything between the single and double quotes there! :)

          Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

          Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

          M Offline
          M Offline
          Mike Certini
          wrote on last edited by
          #4

          Luc, Thank you for your reply.

          L 1 Reply Last reply
          0
          • M Mike Certini

            Luc, Thank you for your reply.

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #5

            You're welcome. :)

            Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

            Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

            1 Reply Last reply
            0
            • L Luc Pattyn

              in SQL string literals need quotes, so I would suggest you try:

              ...VALUES('"+ text +"')"...
              ^ ^

              Warning: make sure not to insert anything between the single and double quotes there! :)

              Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

              Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

              M Offline
              M Offline
              Mike Certini
              wrote on last edited by
              #6

              Luc, I am getting the error message: "error C2110: '+' : cannot add two pointers"

              L 1 Reply Last reply
              0
              • M Mike Certini

                Luc, I am getting the error message: "error C2110: '+' : cannot add two pointers"

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #7

                one cannot add pointers together, it doesn't make sense; adding house numbers wouldn't make sense either. What is your code, and what is the exact line where the error occurs? and what language and IDE are you using? :)

                Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

                Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                M 1 Reply Last reply
                0
                • L Luc Pattyn

                  one cannot add pointers together, it doesn't make sense; adding house numbers wouldn't make sense either. What is your code, and what is the exact line where the error occurs? and what language and IDE are you using? :)

                  Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

                  Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                  M Offline
                  M Offline
                  Mike Certini
                  wrote on last edited by
                  #8

                  Luc, The error is occuring at VALUES('"+ text + "'). My VS2008 is giving me this error. I am trying to send a record to MS Access.

                  1 Reply Last reply
                  0
                  • M Mike Certini

                    I am currently trying to code a variable into a SQL string that is used with ADO to create a record within a database. I though am having problems with the proper syntax for a variable. The string works fine with a number but when I try to code a variable I have problems. I need to find out how to code an array as well.

                    hr = rec2->Open("INSERT INTO mytable(id)VALUES("+ text +")",con2.GetInterfacePtr(), ADODB::adOpenForwardOnly, ADODB::adLockReadOnly, ADODB::adCmdText);

                    W Offline
                    W Offline
                    Wendelius
                    wrote on last edited by
                    #9

                    You cannot directly use local variables inside your SQL statements. You can use a Parameter object to pass a value from a variable to the statement. For example see, http://msdn.microsoft.com/en-us/library/ms677589(VS.85).aspx[^]

                    The need to optimize rises from a bad design.My articles[^]

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups