0

I'm having some trouble with inserting a return from a sql function into a variable. When I run the code below (the connection string is definitely working) I get the following errors :

Bareword found where operator expected at Script.pl line 34, near "'SELECT IDENT_CURRENT ('Database" (Missing operator before Database?) syntax error at Script.pl line 34, near "'SELECT IDENT_CURRENT ('Database" Bad name after Table' at Script.pl line 34.

I successfully prepared/executed sql statements before with this syntax.
I tried using $dbh->prepare ("SELECT IDENT_CURRENT ('Database.dbo.Table')") note the "".
Then the script executes, but my variable $RID just contains -1 which is not correct.
I ran the pure sql code as query on the sql server and that worked just fine.
Any help would be appreciated.

#!/usr/bin/perl
## PERL MODULES
 use DBI;
 use DBD::ODBC;
 use Text::CSV;
 use POSIX;
 use List::MoreUtils qw( each_array );
 no strict "vars";

    my $dbh = DBI->connect($data_source, $user, $password, {RaiseError => 0, PrintError => 1}) or die "Can't connect to $data_source: $DBI::errstr";


    my $id_return = $dbh->prepare ('SELECT IDENT_CURRENT ('Database.dbo.Table')');
    my $RID = $id_return->execute;
    print $RID;

1 Answer 1

2

Don't turn off strict. That never* fixes anything, it just hides errors. Turn on warnings. That's a good idea too.

Your problem here is - you're nesting single quotes:

''SELECT IDENT_CURRENT ('Database.dbo.Table')''

That isn't going to work. Escape them with \' or just use:

q{SELECT IDENT_CURRENT ('Database.dbo.Table')}

* OK, ok. almost never. For the purposes of questions on Stack Overflow it's a good enough approximation to "always".

Sign up to request clarification or add additional context in comments.

1 Comment

This does indeed solve the syntax error. After a bit more testing I found the code below working. $query = "SELECT IDENT_CURRENT ('dbo.Table')"; $sth = $dbh->prepare($query); $sth->execute(); $RID = $sth->fetchrow(); print $RID;

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.