How do I execute a SQL script using a bind variable - sql-scripts

I'm running Oracle 18.c on a Windows 10 platform.
I have a large DOS script which calls SQL Plus to run the first SQL script, passing a parameter to it.
The parameter is successfully passed to the first SQL script. In that SQL script I'm trying to append some text to the passed parameter so that it can call a second script using the "#" feature.
DOS Script test1.bat
#echo off
SET value1=2020_01_19_17_00_01
sqlplus my_username/my_password#my_TNS as sysdba #C:\Backups\test1.sql %value1%
SQL Script test1.sql
SET SERVEROUTPUT ON
variable param1 varchar2(512);
variable full_file_name varchar2(512);
BEGIN
:param1 := '&&1';
dbms_output.put_line('The value of the passed parameter is: ' || :param1);
:full_file_name := :param1 || '_f104.sql';
dbms_output.put_line('The new filename would be: ' || :full_file_name);
#:full_file_name;
END;
/
I'm having problems getting the value in :full_file_name to execute from the test1.sql script. If a variable were not involved I would simply use the line #2020_01_19_17_00_01_f104.sql
How do I go about getting the script file whose name is stored in :full_file_name to execute?

I found a way to do this. My sql script now looks like:
Define ffn104 = '&&1._f104.sql'
#&ffn104
exit
Note that in the Define statement I had to put a period after the passed variable &&1 to end its definition. Then I appended _f104.sql.
That solved the problem.

Related

Got the error when use LUA script to query a list

when I use Lua script to query a list, I got the correctly result if the list is not empty. But got error if the list is empty.
Blow is my script:
const char * sLuaQueryServers = "local key_list = redis.call('KEYS',
KEYS[1]); return(redis.call('MGET', unpack(key_list)))";
I passed the "serverlist:*" as the key, it's successfully returned the server in list.
But if there no server in redis, I got below error:
ERR Error running script (call to
f_88620231033e13635dc3181f2947a740f91012dc): #user_script:1: #user_script:
1: Wrong number of args calling Redis command From Lua script
"
Please help.
To your question, add a check that the list isn't empty before calling MGET, e.g.:
local key_list = redis.call('KEYS', KEYS[1])
if #key_list > 0 then
return(redis.call('MGET', unpack(key_list)))
else
return nil
end
Note #1: no need for semicolons in Lua
Note #2: Using KEYS isn't recommended for anything, except debugging
Note #3: You're using the KEYS table to pass an argument, but since your script is running KEYS (the command) that's really a moot point

On IBM i, how to run a stored procedure via DB2 command of QShell?

How would you run a stored procedure via DB2 command of QShell, as I need a simple way to unit test a change to a stored procedure?
On IBM i
Started qshell with
QSH
Entered these db2 commands in various formats without success
db2 call libraryname.stroredprocedurename('param1value' 'param2value' ?)
db2 call libraryname.stroredprocedurename ('param1value' 'param2value' ?)
db2 call libraryname.stroredprocedurename (param1value param2value ?)
Only reference source I could find
Have tried using CLP to call stored proceduresbut there are no CLP examples
here
You can also use the JDBC client included with jt400.jar. You can run it from QSH by using the following command.
java -cp /qibm/proddata/os400/jt400/lib/jt400.jar com.ibm.as400.access.jdbcClient.Main jdbc:db2:localhost
The client will also handle stored procedure output parameters as shown by the following example.
create procedure add1(in inparm int, out outparm int) language sql begin set outparm = inparm +1; end
call add1(1,?)
Parameter 1 returned 2
I use Squirrel SQL Client http://squirrel-sql.sourceforge.net/ to test all of my SQL.
call libraryname.stroredprocedurename('param1value', 'param2value')
Note the separator is a comma ,, and ? is not a valid parameter marker when called interactively like this.
In addition to the client, you will need a JDBC driver. You can use the JTOpen driver for IBM i found here: http://jt400.sourceforge.net/
From the CL command line, or inside a CL program, you can use the RUNSQL command to execute an SQL statement.
If you're going to be trying multiple ad hoc SQL statements, you might use the STRSQL command. Personally, I tend to use the SQL window provided as part iNavigator.
Finally got the basic syntax
db2 "CALL lib.proc ('parmvalue1')"
Which resulted in:
DB20000I THE SQL COMMAND COMPLETED SUCCESSFULLY.

What is the proper way to execute a batchfile with multiple params?

I have a batchfile which I use for managing the translation of various programs.
Now I want a delphi application to call this batchfile and pass on the parameter it needs for further processing. Unfortunately the parameters contain spaces which leads to a splitup. Is there a way to keep all parameters tied up as intended?
this is how my batchfile looks:
ECHO Scan for new ressources
%MLDIR%\Ml7Build.exe s %1%
ECHO Import glossary for new translation
%MLDIR%\MlBuild.exe i %2%
ECHO Create translated application
%MLDIR%\Ml7Build.exe b %3%
I tried to use the ShellExecute-Command from ShellApi because I found several similar questions on SO, but none of them could help me in solving my problem. My delphi code looks like this:
param1 := ExtractFileName(hMLProj);
param2 := '-f: '+MLWorkDir+'Prev_'+ExtractFileName(hMLProj)+' -settings:Auftrag_Test.importsettings-method:2 -overwri:3 -error:2 '+ExtractFileName(hMLProj)+' ';
param3 := ExtractFileName(hMLProj);
ShellExecute(0,'open',PCHAR(MLWorkDir+'__AutomatedTranslationFUBAR.bat'),PChar(param1 +param2 +param3),nil,SW_SHOWDEFAULT);
ECHO Scan for new resources
%MLDIR%\Ml7Build.exe s %~1
ECHO Import glossary for new translation
%MLDIR%\MlBuild.exe i %~2
REM is 7 ^ omitted here?
ECHO Create translated application
%MLDIR%\Ml7Build.exe b %~3
Note that %n not %n% (n=1..9) refers to the parameter n supplied to the batch. The tilde removes "any enclosing quotes."
Parameters require to be "enclosed in quotes" (and they must be double-quotes) if they contain separators such as spaces.

Passing Variables from SQL Server 2008 R2 to a batch file

I have a Stored Procedure in SQL Server that takes some data and must to send it to a batch file, wich will send those data like variables to a jar file. BAT and JAR is all ok. But my problem is to make it possible from the Stored Procedure... Here is the code that I'm ussing, but for some reasson, it tell me that 'The system cannot find the path specified.'
ALTER PROCEDURE [dbo].[_testeCarimbadorPDF](
#Path VARCHAR(1000),
#numCarimb int
)
AS
BEGIN
DECLARE #CMDSQL VARCHAR(1000)
DECLARE #NUMERO VARCHAR(10)
SELECT #NUMERO = CONVERT(varchar(10),#numCarimb)
SET #CMDSQL = 'C:\TESTE\CarimbadorPDF.bat' + ' ' + #Path + ' ' + #NUMERO
exec master..xp_cmdshell #CMDSQL
waitfor delay '00:00:02'
end
Which one is my mistake? I need to send those variables to the batch file...
The path that you given is SQL Server related path not your local path. The exec master..xp_cmdshell #CMDSQL executes the command on the sql server machine not in your local machine. It can work only if you SQL server runs on your local machine.

Magento: Create Stored Procedure

I am trying to create a Magento module installer, that will in turn create a stored procedure.
The code for the procedure has been run through: Toad, phpmyadmin, and mysql.exe command line. It worked in all 3. However, it fails when Magento goes to execute it during the installation process.
Error:
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax;
Magento Code:
$installer = $this;
$installer->startSetup();
$sql = <<<____SQL
DELIMITER //
CREATE PROCEDURE GetStuff(
IN pSomeId int(11)
)
BEGIN
DECLARE pOtherId INT;
SELECT some_var INTO pOtherId FROM some_table WHERE id = pSomeId;
END;//
DELIMITER ;
____SQL;
$installer->run($sql);
$installer->endSetup();
There's some other code in between, of course, but simplifying did not change the error.
My guess is that it is something to do with Delimeters and how PDO/Magento is treating them. Thank you.
I figured it out.
I did not need to use any delimiters, PDO can take care of them on its own.
But I did need to switch the method from query (used by the installer) to exec.
So, I ended up with:
$write = Mage::getSingleton('core/resource')->getConnection('core_write');
$write->exec($sql);

Resources