Skip to Content

passing string variable to select query not working

Friends,

PB 7.0

DB: sql server 2008

string mid;

mid=get_user();

messagebox("test",mid);

above message box is showing the value of mid correctly...

but when i pass this variable to the below query i am not getting the value.

where i am making mistake....? please check the red colored text and correct me...my issue is only with the variable not with the query...

s4 = 'insert into student select RollNumber, FirstName,CSubjectCode, ' &

+ 'Internal_1,Mentor ' &

+ 'FROM Marks_Final ' &

+ 'WHERE (Internal_1 is not null) and ' &

+ '(feeactive="Y") and mentor="+mid+" ' &

+ 'GROUP BY RollNumber'

thanks

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

4 Answers

  • Best Answer
    author's profile photo Former Member
    Former Member
    Posted on Jul 25, 2014 at 07:27 PM

    Your mistake is the logic you use to construct the string used as the query. As the others have suggested, you should visually inspect the string after building it to see the problem. You have embedded the word "mid" into your query and not the value contained in the variable named mid (which isn't particularly well-named since "mid" is function name). In addition, you should not be using double quotes to delimit string in tsql since that will work ONLY if you have a specific and non-default setting. The code below demonstrates the issue more clearly.

    string ls_temp, ls_temp2
    string mid

    mid = "Jones"


    ls_temp = 'WHERE (Internal_1 is not null) and ' &
    + '(feeactive="Y") and mentor="+mid+" ' &
    + 'GROUP BY RollNumber'

    ls_temp2 = "WHERE (Internal_1 is not null) and (feeactive='Y') and mentor= '" + &
    mid + "' GROUP BY RollNumber"
    Messagebox ("test", "#1=" + ls_temp + "~r~n~r~n#2=" + ls_temp2)

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Jul 25, 2014 at 11:49 AM

    Hello Sathy,

    Some suggestions:

    + Use MessageBox() to display s4 and see if the INSERT statement is correct

    + Use the backslash escape character for the variable mid and replace doublequotes by single ones:

    and mentor=\' + mid + \' &

    + ' GROUP BY RollNumber'

    P.S: I think there is one blank missing in the GROUP statement

    Hope this helps

    Jacob

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Jul 25, 2014 at 11:55 AM

    Hi Sathy,

    Try replacing the variable mid with a different name (maybe ls_mid). The query might be confusing it with the function MID.

    HTH,

    Manuel Marte

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Jul 25, 2014 at 02:48 PM

    There's no aggregate function in the select, I'm wondering if the GROUP BY is necessary. Try to precede the double quote with a tilde and see if it makes a difference. You also missed the single quote before the +. Should be like the below.

    s4 = 'insert into student select RollNumber, FirstName,CSubjectCode, ' &

    + 'Internal_1,Mentor ' &

    + 'FROM Marks_Final ' &

    + 'WHERE (Internal_1 is not null) and ' &

    + '(feeactive=~"Y~") and mentor=~"' +mid+'~"' &

    + 'GROUP BY RollNumber'

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.