NI TestStand

cancel
Showing results for 
Search instead for 
Did you mean: 

How do I use a variable within a sql statement

I am trying to use a local variable within an open SQL step but I keep getting an error.

 

My sql command looks like this "SELECT BoardDetailID FROM BoardDetails WHERE SerialNumber = " +  locals.CurrentSerialNo

 

If I replace the locals.CurrentSerialNo with an actual value such as below the statement works fine.

 

"SELECT BoardDetailID FROM BoardDetails WHERE SerialNumber = " +  " 'ABC001' " 

 

Can someone tell me how to correctly format the statement to use a variable?

0 Kudos
Message 1 of 6
(2,180 Views)

Hi,

 

It could be that locals.CurrentSerialNo does not exist and that its should be Locals.CurrentSerialNo (note the capital L) or that you haven't defined a string variable Locals.CurrentSerialNo in your sequence.

 

What was the actual error?

What version of TestStand are you using?

Regards

Ray Farmer

Regards
Ray Farmer
0 Kudos
Message 2 of 6
(2,174 Views)

Hi,

 

I have attached the sequence file. What I have noticed is that the sql statement is not available in the Data Operation step.

 

Thanks

 

Stuart

0 Kudos
Message 3 of 6
(2,166 Views)

Stuart,

 

I think the problem is that your locals.BoardDetailID is a number, but the expression is expecting a string so that it can use the + operator to concatinate.  I think if you use the below (notice the addition of the str() function) it will work:

"SELECT BoardDetailID FROM BoardDetail WHERE SerialNumber = " + Str(Locals.BoardDetailID)

 

Also, just to clarify from above, the TestStand expression language is case-insensitive, so it doesn't matter if you use "Locals." or "locals.", they will work the same.

Josh W.
Certified TestStand Architect
Formerly blue
0 Kudos
Message 4 of 6
(2,153 Views)

Hi,

 

Thanks for the reply. I have changed the required variable to a string, but with no success. I have reattached my updated sequence file and an image of the error.

 

When looking at the Data operation step I see that the sql statement is missing everything after the last quotation mark.

 

Thanks again,

 

Stuart

Download All
0 Kudos
Message 5 of 6
(2,147 Views)

Hi,

 

All sorted. I changed the statement to look like the statement below and it worked

 

Thanks

 

Stuart

 

"SELECT BoardDetailID FROM BoardDetails WHERE SerialNumber = " + "'" + Locals.CurrentSerialNo + "'"

0 Kudos
Message 6 of 6
(2,144 Views)