cancel
Showing results for 
Search instead for 
Did you mean: 

passing string variable to select query not working

Former Member
0 Kudos

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

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

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)

arnd_schmidt
Active Contributor
0 Kudos

Be careful with Mr. O'Hara.

Former Member
0 Kudos

thanks..

i just modified to mentor="'+mid+'"

and its working fine...

thanks

Answers (3)

Answers (3)

Former Member
0 Kudos

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'

Former Member
0 Kudos

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

CobyKako
Advisor
Advisor
0 Kudos

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