Passing Variables from SQL Server 2008 R2 to a batch file - stored-procedures

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.

Related

How do I execute a SQL script using a bind variable

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.

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.

Executing a SSIS package from stored procedure is timing out

I have a conundrum and I'm stumped and hoping y'all can help.
Here is my scenario: I am loading data into our database through a web interface. First, I upload the file to the SQL box, then I call the script which extracts and loads the data into SQL.
I recently changed this process so that now, instead of executing the package from within the web application, I am calling a stored procedure to execute the script.
Here is the core of my code:
CREATE PROCEDURE [dbo].[ExecuteSsisPackage]
#Package NVARCHAR(100),
AS
BEGIN
SET NOCOUNT ON;
DECLARE #executionID BIGINT
EXEC SSISDB.catalog.create_execution
#folder_name = 'Imports', -- nvarchar(128)
#project_name = 'DataImports, -- nvarchar(128)
#package_name = #Package, -- nvarchar(260)
#use32bitruntime = 1, -- bit
#execution_id = #executionID OUTPUT -- bigint
SELECT #executionId
EXEC [SSISDB].[catalog].[set_execution_parameter_value] #executionId, 50, N'SYNCHRONIZED', #parameter_value = 1 -- turn on synchronization
EXEC ssisdb.catalog.start_execution #executionID;
RETURN (#executionID)
END
Here is the odd part: when I run the stored procedure from management studio it executes fine. However, when I execute this from the web application it times out. But, if I turn OFF synchronization it executes just fine through the web app. The problem with disabling synchronization is that I need to pull some summary info off the data after it has been loaded.
I've even tried rolling my own 'synchronization' from a post I found:
DECLARE #status AS BIGINT = 1;
WHILE(#status = 1 OR #status = 2 OR #status = 5 OR #status= 8)
BEGIN
PRINT 'Status: ' + CAST(#status AS VARCHAR(10))
PRINT 'waiting 2 seconds for Package to finish'
WAITFOR DELAY '00:00:2';
SET #status = (SELECT [Status] FROM SSISDB.[catalog].[executions]
WHERE execution_id = #executionID);
END
PRINT 'Exit Status: ' + CAST(#status AS VARCHAR(10))
This also times out. I did some digging and found that when the SP is executing from the web (but not management studio) there is a lock: LCK_M_S and the wait_resource is: KEY: 9:72057594041532416 (d28bcaafb8df). Again, this only occurs when executing the SP from the web app.
Some more digging unearthed the culprit for the lock. It is the PK_Operations key of the SSISDB.internal.operations table. This is the conundrum. Any ideas why this occurs only through the web and when synchronized it turned on??

Working on different database from stored procedure in SQL Server 2008 R2

Please do me a favor, I want to maintain all the stored procedures in one database, let's say SP_Dbase.
I plan this scheme, user application determine dbase_xyz and will go to SP_stored_procedure in SP_Dbase and then retrieve or edit data of dbase_xyz.
Since command Use dbasename does not work in a stored procedure, I have big difficulty.
I don't intend to write down all the procedure as an execute command as follows :
Set #cmd = 'select .... from ' + #DB + '.dbo.tablename'
EXEC(#cmd)
Anyone could help me ?
Note: All databases have same structure and exist in one SQL Server 2008 R2 instance.
Thanks

Creating a database in Firebird using FireDac (Delphi)

I recently changed from AnyDac to FireDac (8.0.5.3365). We're running Delphi 2006.
When I was using the AnyDac version of this component I could create a new database by doing the following..
Setup my connection
fConnection.LoginPrompt := false;
fConnection.ResourceOptions.SilentMode := true;
fConnection.Params.Clear;
fConnection.Params.Add(Format('DriverID=%s', ['IB']));
fConnection.Params.Add(Format('Database=%s', [fConnectionInfo.xDatabase]));
fConnection.Params.Add(Format('CharacterSet=%s', ['UTF8']));
fConnection.Params.Add(Format('user_name=%s', [fConnectionInfo.xUserName]));
fConnection.Params.Add(Format('password=%s', [fConnectionInfo.xPassword]));
fConnection.Params.Add(Format('ExtendedMetadata=%s', ['True']));
fConnection.Params.Add(Format('CreateDatabase=%s', ['Yes']));
fConnection.Params.Add(Format('Protocol=%s', ['Local']))
//database path = C:\Users\LoginName\AppData\Local\AppName\TestDB.FDB
Open and close the connection
fConnection.Open;
fConnection.Close;
And then I could run my create table sql scripts on the existing database.
But now when I do this with the FireDac version, the Open command raises the fbe_unavailable error as if I didn't specify the CreateDatabase parameter.
Should I be doing this a different way?
Thanks for your time.
Corey.
You have a full example here http://docwiki.embarcadero.com/RADStudio/Rio/en/Executing_SQL_Scripts_%28FireDAC%29
For example, the following Firebird script creates a database, and can be executed using TFDScript:
SET SQL DIALECT 3;
SET NAMES UTF8;
SET CLIENTLIB 'C:\fb25\bin\fbclient.dll';
CREATE DATABASE 'E:\Test2.ib'
USER 'sysdba' PASSWORD 'masterkey'
PAGE_SIZE 16384
DEFAULT CHARACTER SET NONE;
SET TERM ^ ;
CREATE PROCEDURE MY_PROC RETURNS (aParam INTEGER) AS
BEGIN
aParam = 10;
END^
You should use CreateDatabase=Yes connection definition parameter
additionally to other required parameters:
http://docwiki.embarcadero.com/RADStudio/Rio/en/Connect_to_Firebird_(FireDAC)

Resources