i have sql file that i am trying to execute from a perl script like that-
connect to DB
my $dsn = "dbi:Oracle:$ENV{MPR_DB_INSTANCE}";
my $tg_dbh = DBI->connect($dsn,$ENV{MPR_DB_USER},$ENV{MPR_DB_PASS},{AutoCommit=>0});
unless ($tg_dbh) {&PrintAndLog("\nDBI connect failed: Unable to connect to database $tg_dbh");}
print("\nConnected to DB: $ENV{MPR_DB_USER}\#$ENV{MPR_DB_INSTANCE}\n");
print("\nRunning SQL for SY Test\n");
$sql_statement = $tg_dbh->prepare("$ENV{BIN_PATH}/SY_TG_Test.sql");
$sql_statement->execute();
$tg_dbh->commit;
I am getting ORA-00900: invalid SQL statement execute.
any idea what am i doing wrong?
The argument to the prepare method should be an SQL statement (with placeholders), not a file name.
open my $SQL, '<', "$ENV{BIN_PATH}/SY_TG_Test.sql" or die $!;
my $sql_statement = $tg_dbh->prepare(do {local $/; <$SQL>});
Related
I'm trying to execute SQL script that has lot of DML commands using Perl.
Basically there is some reference number which is required to be updated every time in SQL script before it executes, so I'm passing it as a parameter to Perl script which will further replace with parameter and execute. I'm doing this task in IntelliJ IDEA 2022.3.1 (Community Edition).
Problem: Unable to understand but Database connectivity is completely fine, and is fetching SQL script correctly, even replacement of reference number is also fine. But when trying to execute, it's not doing any changes in the Database. It looks like SQL commands are not really executing (as it is executing quickly which is not supposed to). I added warn with execute, so I got that warn message, but there is no error/exception message.
Please try to understand more details via code.
If anywhere I'm doing it wrong, please tell me. I have basic knowledge of Perl so might be doing it wrong. Can't provide more details about SQL script, it is confidential. But I can make sure that there is no error, as it is working fine if execute in it's dedicated tool. Thank you in advance!
#!/usr/bin/perl
use strict;
use warnings FATAL => 'all';
use DBI;
# Connect to the database
my $EnvDatabase = $ENV{"DATABASE"};
if (! defined $EnvDatabase) {
&ERROR ("The environment variable DATABASE is not defined");
exit (-1);
}
print "Updating in Database: $EnvDatabase\n" ;
my ($DBUser, $DBPass, $SID) = split (/\/|\#/,$EnvDatabase);
if (! (defined $DBUser && defined $DBPass && defined $SID )) {
&ERROR ('The environment variable DATABASE is not properly defined');
exit (-1);
}
my $dbh = DBI->connect( "dbi:Oracle:".$SID, $DBUser, $DBPass,{AutoCommit=>0,PrintError=>0})
|| &errorExit( "Unable to connect to database [$DBI::errstr] ");
# Get the input parameters
my $param1 = shift;
my $param2 = "###123###";
# Read the SQL file into a variable
my $sql = do {
local $/ = undef;
open my $fh, "<", "C:/someScript/someSampleSqlWithDMLCommands.sql" or die "could not open file: $!";
<$fh>;
};
# Replace with the parameter
$sql =~ s/$param2/$param1/g;
# Execute the SQL statements
$dbh->do($sql) || warn("Unable to execute ", $dbh->errstr);
$dbh->commit;
$dbh->disconnect;
OUTPUT: Updating in Database: database name declared in environment
Unable to execute ORA-00900: invalid SQL statement (DBD ERROR: OCIStmtExecute) at C:/**/autoScript.pl line 37.
Process finished with exit code 0
I am trying to connect to SQL server using Perl DBI module, I have tried all the connection string format still Perl is throwing invalid connection string error.
I have already tried code snippet available on perl monk website.
#!/usr/bin/perl -w
use strict;
use DBI;
# Set up variables for the connection
my $server_name = '00.120.124.1;3181';
my $database_name = 'abcd';
my $database_user = 'kkkk';
my $database_pass = 'password';
my $DSN = 'driver={SQL Server};server=$server_name;da
+tabase=$database_name;uid=$database_user;pwd=$database_pass;';
my $dbh = DBI->connect("DBI:ODBC:$DSN") || die "Couldn't open database
+: $DBI::errstr\n";
Expected result is to connect to Database.
failed: [Microsoft][ODBC SQL Server Driver][DBNETLIB]SQL Server does not exist or access denied. [Microsoft][ODBC SQL Server Driver][DBNETLIB]ConnectionOpen (Connect()). (SQL-01000) [state was 01000 now 01S00] [Microsoft][ODBC SQL Server Driver]Invalid connection string attribute (SQL-01S00) at perl.pl line 16. Couldn't open database +: [Microsoft][ODBC SQL Server Driver][DBNETLIB]SQL Server does not exist or access denied. (SQL-08001) [state was 08001 now 01000] Invalid connection string attribute (SQL-01S00)
The lines of your post that begin with +
+tabase=$database_name;uid=$database_user;pwd=$database_pass;';
+: $DBI::errstr\n";
were incorrectly copied and pasted from perlmonks.org. The leading + sign is a convention that indicates a long line was split. You should delete the + and join the line to the previous line, so that your code will read
my $DSN = 'driver={SQL Server};server=$server_name;database=$database_name;uid=$database_user;pwd=$database_pass;';
my $dbh = DBI->connect("DBI:ODBC:$DSN") || die "Couldn't open database: $DBI::errstr\n";
Your error message contains the following:
SQL Server does not exist or access denied
Your set-up code contains the following:
my $server_name = '00.120.124.1;3181';
'00.120.124.1;3181' is not a valid server name or IP address. You should correct the IP address section (it can't start with '00') and remove the port into a separate parameter.
You also have the username and password as part of your DSN. I don't know if DBD::ODBC supports that usage (it's not mentioned in the documentation) but it's more traditional to have those as separate parameters to the connect() call.
All in all, I think you want something more like this:
my $server_name = '00.120.124.1'; # But this needs to be corrected
my $server_port = 3181;
my $database_name = 'abcd';
my $database_user = 'kkkk';
my $database_pass = 'password';
my $DSN = "driver={SQL Server};server=$server_name;port=$server_port;database=$database_name";
my $dbh = DBI->connect("DBI:ODBC:$DSN", $database_user, $database_pass)
|| die "Couldn't open database: $DBI::errstr\n";
Also note that I've changed the quote characters used to create your $DSN variable from single quotes to double quotes. Variables are not expanded in signel quotes, so you weren't getting the values of $server_name, etc. in your DSN.
The better way to connect is with the DBD::Sybase module. The name wouldn't suggest it but SQL Server takes its lineage from Sybase. That way you can avoid ODBC. http://metacpan.org/pod/DBD::Sybase#Using-DBD::Sybase-with-MS-SQL
I am able to connect to my MSSQL server but getting a permission error when trying
to query the database. I see the "YAY" in the script below, but get this error:
*[Microsoft][ODBC SQL Server Driver][SQL Server]The SELECT permission was denied on the object 'TICKET_TYPES', database 'DBNAME', schema 'dbo'. (SQL-42000)(DBD: st_execute/SQLExecute err=-1)*
If I add "DOMAIN\DOMAIN Users" with DBO access it works, but when I take that away it doesn't. I think it's connecting to the database as the logon user, not the SQL user that is set up in the connect string but not positive.
The SQL Driver is set to use "With SQL Server Authentication using a login ID..." and not "Windows NT..."
Classic ASP connects and queries fine. I can access/query the DB using the SQL account in the connect string via ASP classic and SQL Server Management Studio.
It may be relevant that it also works perfectly against SQL2005 but not SQL2008 which we are trying to upgrade to. Everything is fine with our upgrade except our couple Perl scripts just don't get into the tables.
use lib "cgi-bin";
use DBI;
use CGI;
use CGI::Carp qw(fatalsToBrowser);
require "Babel.pm";
my $y = new Babel;
my $q = new CGI;
$SERVER_N = $q->server_name() ;
### CONNECT
$database = "DBNAME";
$dbusername = "SQLUSERNAME";
$dbpassword = "SQLUSERPASSWORD";
$server="SQLSERVERIP";
print "Content-type: text/html\n\n";
$dbh = DBI-> connect( "DBI:ODBC:driver={SQL Server};
Server=$server;
Database=$database;
UID=$dbusername;
PWD=$dbpassword") || die &DBerror;
if ($DBI::errstr) {print "Error: Connecting to the Database!";
exit;}else{
print "YAY! <br><br>";
}
### END: CONNECT
$v2SQL = "select distinct ROUTE from TICKET_TYPES";
$css1 = $dbh->prepare ($v2SQL);
$css1->execute () || print $css1->errstr;
while ((#row) = $css1 ->fetchrow_array) {
$MANA = #row[0];
print "$MANA";
}
$css1->finish () || print $css1->errstr;
We have a Perl script which runs a SQL and puts data in the table.
Now instead of supplying a single SQL statement, we want to pass bunch of them putting them together in a .sql file. We know that our program will fail because it expects a single SQL statement, not s bunch of them (that too from a .sql file). How do we make it work with a .sql file (having multiple INSERT statements?). We are using the DBI package.
A small snippet of code:
$sth = $dbh->prepare("/home/user1/tools/mytest.sql");
$sth->execute || warn "Couldn't execute statement";
$sth->finish();
There is a sort of workaround for DDL. You need to slurp SQL file first and then enclose it's contents into BEGIN ... END; keywords. Like:
sub exec_sql_file {
my ($dbh, $file) = #_;
my $sql = do {
open my $fh, '<', $file or die "Can't open $file: $!";
local $/;
<$fh>
};
$dbh->do("BEGIN $sql END;");
}
This subroutine allows to run DDL (SQL) scripts with multiple statements inside (e.g. database dumps).
Not exactly sure what you want...
Once you create a DBI object, you can use it over and over again. Here I'm reading SQL statement after SQL statement from a file and processing each and every one in order:
use DBI;
my $sqlFile = "/home/user1/tools/mytest.sql"
my $dbh = DBI::Connect->new($connect, $user, $password)
or die("Can't access db");
# Open the file that contains the various SQL statements
# Assuming one SQL statement per line
open (SQL, "$sqlFile")
or die("Can't open file $sqlFile for reading");
# Loop though the SQL file and execute each and every one.
while (my $sqlStatement = <SQL>) {
$sth = dbi->prepare($sqlStatement)
or die("Can't prepare $sqlStatement");
$sth->execute()
or die("Can't execute $sqlStatement");
}
Notice that I'm putting the SQL statement in the prepare and not the file name that contains the SQL statement. Could that be your problem?
You don't need perl for this at all. Just use the mysql command line client:
mysql -h [hostname] -u[username] -p[password] [database name] < /home/user1/tools/mytest.sql
replace the [variables] with your information.
Note no space after -u or -p. If your mysql server is running on the same machine you can omit -h[hostname] (it defaults to localhost)
Here is how I've done it. In my case I dont assume one SQL per line and I assume, my example is a bit better :)
sub get_sql_from_file {
open my $fh, '<', shift or die "Can't open SQL File for reading: $!";
local $/;
return <$fh>;
};
my $SQL = get_sql_from_file("SQL/file_which_holds_sql_statements.sql");
my $sth1 = $dbh1->prepare($SQL);
$sth1->execute();
I based my code on the answer to the question Access get all tables, but am getting the following error:
DBD::ODBC::st execute failed:
[Microsoft][ODBC Microsoft Access
Driver] Record(s) cannot be read; no
read permission on 'MSysObjects'.
(SQL-42000) at direct.pl line 22.
[Microsoft][ODBC Microsoft Access
Driver] Record(s) cannot be read; no
read permission on 'MSysObjects'.
(SQL-42000) at direct.pl line 22.
Here is what I have tried so far. I commented out my first attempt. The current attempt is based on SELECT "Table" AS [Table] which astander mentioned in his answer to said question. I get the same error either way. Here is my code, in its entirety:
use strict;
use warnings;
use DBI;
my $dbh = DBI->connect('DBI:ODBC:MutantDB','','')
or die 'could not connect to database' . DBI::errstr;
my $sth = $dbh->prepare('SELECT "Table" AS [Table],
MSysObjects.Name,
MSysObjects.Type
FROM MSysObjects
WHERE MSysObjects.Type =1
Or MSysObjects.Type=6
ORDER BY MSysObjects.Name;')
or die 'could not prepare statement' . $dbh->errstr();
# my $sth = $dbh->prepare('SELECT MSysObjects.*, MSysObjects.Type
# FROM MSysObjects
# WHERE (((MSysObjects.Type)=1)) OR (((MSysObjects.Type)=6));'
# ) or die 'could not prepare statment' . $dbh->errstr();
$sth->execute() or die $sth->errstr();
while( my ($name, $type) = $sth->fetchrow()){
print "name: $name \t type: $type \n";
}
How about a schema?
http://www.cpan.org/authors/id/T/TL/TLOWERY/DBD-ADO-2.1.readme