PDA

View Full Version : how to pass the variable in WHERE clause instead of constant in QODBC?



mammaiap
9th March 2011, 07:27
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

ChrisW67
9th March 2011, 07:47
Look at QSqlQuery docs in the Details section for information related to binding variables into prepared queries.

mammaiap
9th March 2011, 12:27
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();