I am trying to execute an oracle sql query in while loop of a PERL script as follows --
my $dbh = DBI->connect( 'dbi:Oracle:'.$dbname,
$dbusername,
$pass,
{ PrintError => 0,
RaiseError => 1
}
) || die "Erreur lors de la connexion: $DBI::errstr";
print ("Connexion à la base de données $dbname avec $dbusername OK \n");
$requete = "select distinct to_char(date_appli,'yyyymmdd') from DATE_APPLI ".
"where frequence = 'Q' and actif = 1";
$sth_sql = $dbh->prepare($requete);
$sth_sql->execute();
#row=$sth_sql->fetchrow_array;
$datesitu=#row[0];
$sth_sql->finish;
print "La date de situation est $datesitu \n";
# SQL to get validation script and table names
$requete = "select SCRIPT_NAME, table_name from fdf_scripts";
$sth_sql = $dbh->prepare($requete);
$sth_sql->execute();
$sth_sql->bind_columns(undef, \$script_name, \$table_name);
while ($sth_sql->fetch()) {
$script_sql="$sql_path\\"."FDF_Test_scripts\\".$script_name.".sql"; #validation script path
$script_log="$log_path\\".$script_name.".log"; #log files path
$rep_file_name="$sql_path\\"."FDF_Test_scripts\\".$table_name.".sql"; #reports SQL path
$csv_file="$sql_path\\"."FDF_Test_scripts\\".$table_name.".csv"; #report CSV path
#Load data into validation tables using validation scripts
$CmdText="sqlplus -s $connect \#$script_sql $script_log";
print "Inserting data into table : $table_name \n";
#system ("$CmdText");
$col_sql = "select COLUMN_NAME from all_tab_cols where TABLE_NAME = upper(\'$table_name\')\n";
print "$col_sql\n";
$sth_sql = $dbh->prepare($col_sql);
$sth_sql->execute();
$sth_sql->bind_columns(undef, \$COLUMN_NAME);
while ($sth_sql->fetch()) {
print "$COLUMN_NAME\n";
}
if (open (my $fh, '<:encoding(UTF-8)', $script_log)){
while (my $line = <$fh>){
if ($line=~m/\bERROR\b/){
print "Error While Loading $table_name table Please Check log file for errors at: $script_log \n";
}
}
}
else {
warn "Could open file: $script_log \n"
}
}
in above code i am fetching table names from a table and then looping them to get the column names of each table which is present in that table.
The code got executed for one iteration of inner while loop but throw error for next iteration as below --
DBD::Oracle::st fetch failed: ERROR no statement executing (perhaps you need to call execute first) [for Statement "select COLUMN_N\
AME from all_tab_cols where TABLE_NAME = upper('FDF_Bond_validation_results')
I am not proficient in PERL and just started to using this and not sure how can i overcome this problem.
Please let me know if more information is required.
In the loop, the line $sth_sql = $dbh->prepare($col_sql); overwrite the content of the $sth_sql variable. The second time the loop run, the content of $sth_sql isn't the same and the statement that overwrote it has already been exhausted (trying to fetch from it again is what causes the error).
To correct this issue you should either use a different name for the second $sth_sql variable or simply declare the second $sth_sql variable with my to keep it inside the loop (see perldoc -f my).
Note: This solution has been identified by JGNI in the comment of the question. My role has only be to summarize the issue to provide a proper answer to this question.
Related
I'm trying to get the source of a view in my .net app.
To do this, I query DBA_VIEWS: it has a column TEXT with exactly what I need. The type is LONG.
If I do it using the Unmanaged driver, everything works as expected.
The same code with Managed driver results in ORA-01009: missing mandatory parameter.
Adding a transaction around the command and using breakpoint and "slow" steps sometimes results in the same code working.
ODP.NET version is 19, Oracle DB is 18c Express Edition. Strangely enough, the same code works just fine with Oracle Database 12c regardless of driver type.
Is there maybe some setting I need to change on the database or in code? I'm completely lost here.
Code I'm using for testing:
Imports System.Data.Common
Imports Oracle.ManagedDataAccess
'Imports Oracle.DataAccess
Module Views
Function CreateCommand(Connection As DbConnection) As System.Data.Common.DbCommand
Dim cmd As Data.Common.DbCommand = Connection.CreateCommand()
With CType(cmd, Client.OracleCommand)
.BindByName = True
.FetchSize = &H100000 '1 Mb
.InitialLONGFetchSize = -1 'the entire LONG or LONG RAW data is prefetched and stored in the fetch array.
.InitialLOBFetchSize = -1 'the entire LOB data is prefetched and stored in the fetch array.
End With
Return cmd
End Function
Sub query()
Try
Using DBConnection = New Client.OracleConnection("User ID=TESTUSER;Password=TESTPWD;Data Source=TESTDB;Pooling=True")
DBConnection.Open()
Using DBConnection.BeginTransaction()
Using cmdSQL = CType(CreateCommand(DBConnection), Client.OracleCommand)
cmdSQL.CommandText = "select TEXT from DBA_VIEWS where VIEW_NAME = :0"
Dim p = cmdSQL.CreateParameter()
p.ParameterName = "0"
p.Value = "TEST_VIEW"
cmdSQL.Parameters.Add(p)
Dim sw = Stopwatch.StartNew
Using rdr = cmdSQL.ExecuteReader
rdr.FetchSize = 2 ^ 20
While rdr.Read
Dim row(rdr.FieldCount - 1) As Object
rdr.GetProviderSpecificValues(row)
Dim x = row(0)
Console.WriteLine($"{x.ToString.Length} bytes")
End While
End Using
Console.WriteLine($"{sw.ElapsedMilliseconds} ms")
End Using
End Using
End Using
Catch ex As Exception
Console.WriteLine(ex.ToString)
End Try
End Sub
End Module
You can use an anonymous block with output parameter and a call to ExecuteNonQuery. Your command text will be
"begin select TEXT into :0 from DBA_VIEWS where VIEW_NAME = :1; end;"
Add 2 parameters. Make sure that
' Parameter #1 has
p.Direction = ParameterDirection.Output
p.OracleDbType = OracleDbType.Long
p.Size = 1000000
And use command cmd.ExecuteNonQuery(). Then, when parameter is retrieved, just use its value
Dim txt As String = cmd.Parametersp[0].Value.ToString()
It's a pity, Oracle deprecated LONG data type for ages but LONG data is still used many times for internal data.
You could write a function and then get the data by calling the function:
create or replace function GetViewText(v in varchar2) return clob is
ret CLOB;
BEGIN
FOR aRow IN (SELECT TEXT FROM DBA_VIEWS WHERE VIEW_NAME = v) LOOP
ret := aRow.TEXT;
-- or ret := TO_CLOB(aRow.TEXT);
END LOOP;
RETURN ret;
END;
Yet another way from this answer is to (ab)use dbms_xmlgen.getxml.
We can either use it to query a single view's code (as in my original question)
with input as (
select
:0 as VIEW_NAME
from dual
)
SELECT
substr(
text_xml,
instr(text_xml, '<LONGCOL>') + length('<LONGCOL>'),
instr(text_xml, '</LONGCOL>', -1) - (instr(text_xml, '<LONGCOL>') + length('<LONGCOL>'))
) as TEXT
from
(
-- getxml can return malformed xml (which is good in this case)
-- while getxmltype can not.
select dbms_xmlgen.getxml(q'{
SELECT TEXT as LONGCOL
FROM SYS.DBA_VIEWS
WHERE VIEW_NAME = '}' || input.VIEW_NAME || q'{'
}') as text_xml
from input
)
or create our own DBA_VIEWS version.
create or replace view APP_SCHEMA.DBA_VIEWS
as
select
OWNER, VIEW_NAME, TEXT_LENGTH,
case
when (TEXT_VC is not null and TEXT_LENGTH <= 4000)
then to_clob(TEXT_VC)
when TEXT is NULL
then NULL
else (
SELECT
substr(
text_xml,
instr(text_xml, '<LONGCOL>') + length('<LONGCOL>'),
--instr(text_xml, '</LONGCOL>', -1) - (instr(text_xml, '<LONGCOL>') + length('<LONGCOL>'))
TEXT_LENGTH
) as TEXT
from
(
-- getxml can return malformed xml (which is good in this case)
-- while getxmltype can not.
select dbms_xmlgen.getxml(q'{
SELECT TEXT as LONGCOL
FROM SYS.DBA_VIEWS
WHERE OWNER = '}' || OWNER || q'{'
and VIEW_NAME = '}' || VIEW_NAME || q'{'
}') as text_xml
from dual
)
)
end as TEXT,
TEXT_VC, TYPE_TEXT_LENGTH, TYPE_TEXT, OID_TEXT_LENGTH, OID_TEXT, VIEW_TYPE_OWNER, VIEW_TYPE, SUPERVIEW_NAME, EDITIONING_VIEW, READ_ONLY, CONTAINER_DATA, BEQUEATH, ORIGIN_CON_ID, DEFAULT_COLLATION, CONTAINERS_DEFAULT, CONTAINER_MAP, EXTENDED_DATA_LINK, EXTENDED_DATA_LINK_MAP, HAS_SENSITIVE_COLUMN
from sys.dba_views
;
i want to insert a query but not work :
my $query1 = $db1->prepare("SELECT host, name, severity FROM XX");
my $query2 = $db2->prepare('UPDATE worldmap_table' . ' SET severity = ?, name = ? WHERE HOST = ?');
$query1->execute;
while (my #row = $query1->fetchrow_array) {
$query2->execute($row[2]);
print "$row[2]\n";
}
preparation query 3
my $query3 = $db1->prepare("SELECT host, name, severity
FROM XX);
preparation query 4
my $query4 = $db2->prepare('UPDATE worldmap_table' . ' SET severity = 6, name = ? WHERE HOST = ?');
$query3->execute;
this part not work
while (my #row2 = $query3->fetchrow_array) {
$query4->execute($row2[2], $row2[1], $row2[0]);
print "$row2[2], $row2[1], $row2[0] \n";
}
i have this error:
DBD::mysql::st execute failed: called with 3 bind variables when 2 are needed at worldmap2.pl line 103.
DBD::mysql::st execute failed: called with 3 bind variables when 2 are needed at worldmap2.pl line 103.
There are two placeholders in query 4:
my $query4 = $db2->prepare(
'UPDATE worldmap_table' . ' SET severity = 6, name = ? WHERE HOST = ?'
# ~ ~
);
But you're calling it with only one argument:
$query4->execute($row2[2]);
# ~~~~~~~~
The error message mentions different numbers, so I guess you simplified the code.
i want to insert a query but not work :
my $query1 = $db1->prepare("SELECT host, name, severity FROM XX");
my $query2 = $db2->prepare('UPDATE worldmap_table' . ' SET severity = ?, name = ? WHERE HOST = ?');
$query1->execute;
while (my #row = $query1->fetchrow_array) {
$query2->execute($row[2]);
print "$row[2]\n";
}
preparation query 3
my $query3 = $db1->prepare("SELECT host, name, severity
FROM XX);
preparation query 4
my $query4 = $db2->prepare('UPDATE worldmap_table' . ' SET severity = 6, name = ? WHERE HOST = ?');
$query3->execute;
this part not work
while (my #row2 = $query3->fetchrow_array) {
$query4->execute($row2[2], $row2[1], $row2[0]);
print "$row2[2], $row2[1], $row2[0] \n";
}
i have this error:
DBD::mysql::st execute failed: called with 3 bind variables when 2 are needed at worldmap2.pl line 103.
DBD::mysql::st execute failed: called with 3 bind variables when 2 are needed at worldmap2.pl line 103.
There are two placeholders in query 4:
my $query4 = $db2->prepare(
'UPDATE worldmap_table' . ' SET severity = 6, name = ? WHERE HOST = ?'
# ~ ~
);
But you're calling it with only one argument:
$query4->execute($row2[2]);
# ~~~~~~~~
The error message mentions different numbers, so I guess you simplified the code.
I am facing an issue with some SQL scripts where I use parameter passing.
I call a script like this:
DEFINE sowner = "SOWNER"
DEFINE auser = "AUSER"
DEFINE tbsp = "TAB"
DEFINE insp = "IND"
#R1/do_your_job.sql $owner $auser $tbsp $insp
And inside R1/do_your_job :
define sowner = $1
define auser = $2
define tbsp = $3
define insp = $4
alter session set current_schema=$sowner;
And I get: Ora 00911: invalid character marking my $ sign
Why is it not decoding $sowner to "SOWNER"?
My file is marked with "set define $" anyway...
Thank you.
Use '&variable' while passing and '&1', '&2' etc while reading.
DEFINE sowner = "SOWNER"
DEFINE auser = "AUSER"
DEFINE tbsp = "TAB"
DEFINE insp = "IND"
#R1/do_your_job.sql &sowner &auser &tbsp &insp
Your do_your_job.sql may use it as
select '&1','&2','&3','&4' from dual;
Result
#R1/do_your_job.sql &sowner &auser &tbsp &inspSQL> SQL> SQL> SQL> SQL>
old 1: select '&1','&2','&3','&4' from dual
new 1: select 'SOWNER','AUSER','TAB','IND' from dual
'SOWNE 'AUSE 'TA 'IN
------ ----- --- ---
SOWNER AUSER TAB IND
I'm still learning PowerBuilder and trying to get familiar with it. I'm receiving the following error when I try to run a program against a specific document in my database:
ct_fetch(): user api layer: internal common library error: The bind of result set item 4 resulted in an overflow. ErrCode: 2.
What does this error mean? What is item 4? This is only when I run this program against a specific document in my database, any other document works fine. Please see code below:
string s_doc_nmbr, s_doc_type, s_pvds_doc_status, s_sql
long l_rtn, l_current_fl, l_apld_fl, l_obj_id
integer l_pvds_obj_id, i_count
IF cbx_1.checked = True THEN
SELECT dsk_obj.obj_usr_num,
dsk_obj.obj_type,
preaward_validation_doc_status.doc_status,
preaward_validation_doc_status.obj_id
INTO :s_doc_nmbr, :s_doc_type, :s_pvds_doc_status, :l_pvds_obj_id
FROM dbo.dsk_obj dsk_obj,
preaward_validation_doc_status
WHERE dsk_obj.obj_id = :gx_l_doc_obj_id
AND preaward_validation_doc_status.obj_id = dsk_obj.obj_id
using SQLCA;
l_rtn = sqlca.uf_sqlerrcheck("w_pdutl095_main", "ue_run_script", TRUE)
IF l_rtn = -1 THEN
RETURN -1
END IF
//check to see if document (via obj_id) exists in the preaward_validation_doc_status table.
SELECT count(*)
into :i_count
FROM preaward_validation_doc_status
where obj_id = :l_pvds_obj_id
USING SQLCA;
IF i_count = 0 THEN
//document doesn't exist
// messagebox("Update Preaward Validation Doc Status", + gx_s_doc_nmbr + ' does not exist in the Preaward Validation Document Status table.', Stopsign!)
//MC - 070815-0030-MC Updating code to insert row into preaward_validation_doc_status if row doesn't already exist
// s_sql = "insert into preaward_validation_doc_status(obj_id, doc_status) values (:gx_l_doc_obj_id, 'SUCCESS') "
INSERT INTO preaward_validation_doc_status(obj_id, doc_status)
VALUES (:gx_l_doc_obj_id, 'SUCCESS')
USING SQLCA;
IF sqlca.sqldbcode <> 0 then
messagebox('SQL ERROR Message',string(sqlca.sqldbcode)+'-'+sqlca.sqlerrtext)
return -1
end if
MessageBox("PreAward Validation ", 'Document number ' + gx_s_doc_nmbr + ' has been inserted and marked as SUCCESS for PreAward Validation.')
return 1
Else
//Update document status in the preaward_validation_doc_status table to SUCCESS
Update preaward_validation_doc_status
Set doc_status = 'SUCCESS'
where obj_id = :l_pvds_obj_id
USING SQLCA;
IF sqlca.sqldbcode <> 0 then
messagebox('SQL ERROR Message',string(sqlca.sqldbcode)+'-'+sqlca.sqlerrtext)
return -1
end if
MessageBox("PreAward Validation ", 'Document number '+ gx_s_doc_nmbr + ' has been marked as SUCCESS for PreAward Validation.')
End IF
update crt_script
set alt_1 = 'Acknowledged' where
ticket_nmbr = :gx_s_ticket_nmbr and
alt_2 = 'Running' and
doc_nmbr = :gx_s_doc_nmbr
USING SQLCA;
Return 1
ElseIF cbx_1.checked = False THEN
messagebox("Update Preaward Validation Doc Status", 'The acknowledgment checkbox must be selected for the script to run successfully. The script will now exit. Please relaunch the script and try again . ', Stopsign!)
Return -1
End IF
Save yourself a ton of headaches and use datawindows... You'd reduce that entire script to about 10 lines of code.
Paul Horan gave you good advice. This would be simple using DataWindows or DataStores. Terry Voth is on the right track for your problem.
In your code, Variable l_pvds_obj_id needs to be the same type as gx_l_doc_obj_id because if you get a result, it will always be equal to it. From the apparent naming scheme it was intended to be long. This is the kind of stuff we look for in peer reviews.
A few other things:
Most of the time you want SQLCode not SQLDbCode but you didn't say what database you're using.
After you UPDATE crt_script you need to check the result.
I don't see COMMIT or ROLLBACK. Autocommit isn't suitable when you need to update multiple tables.
You aren't using most of the values from the first SELECT. Perhaps you've simplified your code for posting or troubleshooting.