2

I'm trying to use a dynamically constructed query to get data from an ms sql server(2008) in perl.

The sql-string looks like this:

$sql = (q/SELECT COUNT(DISTINCT COLUMNa) FROM TABLEa WHERE COLUMNb = '$var'/);

When I try to execute this I get the following error message:

DBD::Sybase::st execute failed:
  Server message number=245 severity=16 state=1 line=1 server=HOSTNAME
  text=Conversion failed when converting the varchar value '$var' to data type tinyint.
  at ./scriptname.pl line 32.
3
  • 2
    I am compelled to point out that you have the possibility of sql injection there. You really should be looking at a placeholders and bind values. Additionally, there is not enough information there (what is the definition of TABLEa? How are you executing the command? What is in $var?
    – user289086
    Commented Jun 28, 2014 at 0:40
  • oh well thats not web code im writing there - this is never gonna leave my workstation. TABLEa COLUMNb is indeed tinyint. the content of $var is a digit constructed in perl.
    – Sven
    Commented Jun 28, 2014 at 0:43
  • ok looks that my approach was wrong. I have to solve my original problem using T-SQL. My issue was that i didnt know how to loop in SQL. Guess this can be closed.
    – Sven
    Commented Jun 28, 2014 at 1:02

1 Answer 1

3

q() is literal. You need qq() if you want $var interpolated.

$sql = qq/SELECT COUNT(DISTINCT COLUMNa) FROM TABLEa WHERE COLUMNb = '$var'/;

Also, I agree with the comment on SQL injection. You should use a bind variable instead.

1
  • thanks! that did it! and yes i know this aint sane code, this is a script im using on my workstation.
    – Sven
    Commented Jun 28, 2014 at 2:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.