简体   繁体   中英

perl script with sql connection string seemingly failing over point(.)

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;

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".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM