Results 1 to 3 of 3

Thread: how to pass the variable in WHERE clause instead of constant in QODBC?

  1. #1
    Join Date
    Feb 2010
    Posts
    28
    Qt products
    Qt4
    Platforms
    Windows

    Question how to pass the variable in WHERE clause instead of constant in QODBC?

    Hi Friends,

    i am using QODBC for connecting "SQL Server"...

    i want to pass the int variable in WHERE clause in QSqlQuery ...

    below is the code snippet:

    QSqlQuery query;

    query.exec("SELECT AOA,CL,CD,CM FROM Test_Validity WHERE (Campaign_Nr=232 AND Serie_Nr=8)");

    in the above query i want to pass the int variable in WHERE clause instead of the constant...

    i had searched in google related to this problem... and found some stuffs which are related to the current problem..

    solution#1:http://www.mssqltips.com/tip.asp?tip=1160

    1. Writing a query with parameters(Dynamic SQL statement in SQL Server..)

    [if you only need to pass parameters into your WHERE clause of your SQL statement. Let's say we need to find all records from the customers table where City = 'London'. This can be done easily such as the following example shows.
    DECLARE @city varchar(75)
    SET @city = 'London'
    SELECT * FROM customers WHERE City = @city]

    as per the above example....if i write
    DECLARE @cno int
    SET @cno = currentcno

    then it gives problem...

    solution#2:
    creating the STORED PROCEDURE

    i dont know how to create the "STORED PROCEDURE" and use it in QT


    please help me to solve this problem..

    Thanks in Advance,
    Muthu

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: how to pass the variable in WHERE clause instead of constant in QODBC?

    Look at QSqlQuery docs in the Details section for information related to binding variables into prepared queries.

  3. #3
    Join Date
    Feb 2010
    Posts
    28
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to pass the variable in WHERE clause instead of constant in QODBC?

    Hi Chris,

    thanks a lot... i found the solution...

    QSqlQuery query;
    query.prepare("SELECT AOA,CL,CD,CM FROM Test_Validity WHERE (Campaign_Nr=? AND Serie_Nr=?)");
    query.addBindValue(currentcno);
    query.addBindValue(currentsno);
    query.exec();

Similar Threads

  1. Replies: 10
    Last Post: 6th April 2011, 10:05
  2. Pass variable from a form to another form
    By dark1988 in forum Qt Programming
    Replies: 5
    Last Post: 8th February 2011, 18:19
  3. qsqltablemodel "order by" clause in setfilter?
    By jtdavidson in forum Newbie
    Replies: 5
    Last Post: 10th August 2010, 01:58
  4. Replies: 1
    Last Post: 30th November 2007, 10:03
  5. Replies: 6
    Last Post: 4th April 2006, 07:13

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.