using a variable in a SQL statement.


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: using a variable in a SQL statement.

  1. #1
    Dean Earley Guest

    Re: using a variable in a SQL statement.

    > I was wondering if anyone knows if you can embed a variable into a SQL statement.ie:
    >
    > Dim dept as Integer
    > dept = 11
    > SELECT Name, Surname FROM Department WHERE deptno = '11';
    >
    > Replacing the number 11 in the sql statement with the variable name:
    >
    > SELECT Name, Surname FROM Department WHERE deptno = dept;?
    >
    > I want to be able to store the varible globally and use it to change the
    > WHERE clause in the SQL statement as and when the variable is changed.


    dept = 11
    SQL="SELECT Name, Surname FROM Department WHERE deptno = '" & CStr(dept) & "';"

    HTH

    --
    Dean Earley (dean.earley@icode.co.uk)
    Assistant Developer

    iCode Systems



  2. #2
    Des Guest

    using a variable in a SQL statement.


    I was wondering if anyone knows if you can embed a variable into a SQL statement.ie:

    Dim dept as Integer
    dept = 11
    SELECT Name, Surname FROM Department WHERE deptno = '11';

    Replacing the number 11 in the sql statement with the variable name:

    SELECT Name, Surname FROM Department WHERE deptno = dept;?

    I want to be able to store the varible globally and use it to change the
    WHERE clause in the SQL statement as and when the variable is changed.

  3. #3
    Craig Brown Guest

    Re: using a variable in a SQL statement.


    Des,

    If your Select statement is simply imbedded in your VB code, yes, you can
    do that.

    It would be something like:

    Dim sSQL As String
    Dim sVar as String
    Dim sQuote as String

    sQuote = Chr(39)
    sVar = "Whatever you want it to be now..."
    sSQL = "SELECT x, y, z From tblWhatever Where fldA = "
    & sQuote & sVar & sQuote

    If you are using a DBMS that allows stored procedures, you may want to look
    into putting your SQL statement there instead.

    Craig Brown




    "Des" <desmond.kavanagh@student.shu.ac.uk> wrote:
    >
    >I was wondering if anyone knows if you can embed a variable into a SQL statement.ie:
    >
    >Dim dept as Integer
    >dept = 11
    >SELECT Name, Surname FROM Department WHERE deptno = '11';
    >
    >Replacing the number 11 in the sql statement with the variable name:
    >
    >SELECT Name, Surname FROM Department WHERE deptno = dept;?
    >
    >I want to be able to store the varible globally and use it to change the
    >WHERE clause in the SQL statement as and when the variable is changed.



  4. #4
    Des Guest

    Re: using a variable in a SQL statement.


    Thanks, that's been really helpful. I didn't think i'd get a reply so soon.
    You both saved me a lot of blood, sweat and tears!

    Des

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center