How can I use Firebird's Execute Block with Delphi TSQLQuery? - delphi

Using dbExpress TSQLQuery, I can't execute a query with execute block command because that command requires ? notation for parameters, and Delphi uses : for parameters, then, if in the body of that block creates variables and uses them as
select data from table where .... into :var;
that ":var" is interpreted as a parameter by TSQLQuery.
Which is the way for executing an execute block statement with Delphi?
If I write:
execute block(param1 char(1)=:param1)
I can load a value for :param1 from Delphi, but when I execute it with Query.Open or Query.ExecSQL an error returns indicating absence of parameter so ? because in Firebird execute block is written:
execute block(param1 char(1)=?param1)
Is there any way to resolve this with TSQLQuery?

first you can disable the TSQLQuery property for
ParamCheck := False;
Then at the beginning of execute block, remove the parameter path..
execute block (param1 char (1) = :param1)
and your query is passed (%s) instead of :param1.
I did so in my problem and solved it !

":var" is interpreted as a parameter by TSQLQuery
You can turn that off by setting the ParamCheck property to False.

the only way that worked for me was not putting ":" for internal vars in the block.
Ex. select data from table into var; and that's work!, then, as this is a block, evaluate the var with an if!
if (var = 1)
do something;
else
do anotherthing;
and resolved business!

Related

how to get the output of a stored procedure as task output in snowflake

I created a stored procedure where it returns the string with a successful message and the number of rows inserted or error messages like file not found or data did not load when executed manually. when I called the same stored procedure with task it shows(task_history) as succeed. and cant find if the data has been loaded or not. it has to be checked manually.
when I referred the following question Snowflake a working procedure is not being successfully executed when calling it within a scheduled task
the procedure and the task has the same owner(owner has global execute task privilege).
but data is being updated both the times during manual and task call of procedure.
how to make the return value appear in task and make the task not executing the successor task if the stored procedure return a error.
You can use SYSTEM$SET_RETURN_VALUE to set a return value in you first task.
In a tree of tasks, a task can call this function to set a return value. Another task that identifies this task as the predecessor task (using the AFTER keyword in the task definition) can retrieve the return value set by the predecessor task.
You can then use SYSTEM$GET_PREDECESSOR_RETURN_VALUE in your next task to condition your actions (for example doing nothing if the return_value contains an error).
The return value will appear for monitoring in TASK_HISTORY.
There are 2 parts to get the return value from stored procedure to save to task history.
The stored procedure should "EXECUTE AS CALLER"
You need to call system$set_return_value with the return value
Example
CREATE OR REPLACE PROCEDURE MySchema.MyStoredProcedure()
RETURNS VARIANT
LANGUAGE JAVASCRIPT
EXECUTE AS CALLER
AS $$
let command1 = = `DELETE FROM MySchema.MyTable WHERE rownum > 10;`;
let stmt1 = snowflake.createStatement({sqlText: command1});
let rs = stmt1.execute();
rs.next();
let deleted = rs.getColumnValue(1);
//This requires the SP to be run as CALLER - This return value is logged in Task History
stmt1 = snowflake.createStatement({sqlText:`call system$set_return_value(' { "RowsDeleted": ${deleted} }');`});
rs = stmt1.execute();
return { "RowsDeleted": deleted };
$$;

Exit and Return in Proc TCL

I want to know the difference between Exit and Return inside a proc.
I have a script that contains a lot of proc, one of them uses exit and some value ex:
proc someProc {code} {
exit $code
}
and another one is like:
proc multiply {value} {
set number [expr {$value * 5}]
return $number
}
Does the exit stop the running script or what is the difference?
The exit command makes the current process stop. The running program will be gone afterwards (though the file containing the code will still be there). Its optional argument is the error code to hand to the OS; the default is zero, for no-error-at-all.
The return command makes the current procedure call stop. The optional argument provides a way to say what the result of the call to the procedure is; the default is the empty string (given that Tcl doesn't have NULL/null/nil/etc. at all).
Internally, exit does a system call to the “stop running this program” OS API, and return throws an exception that the general procedure management code transforms into the result of the call.

Handling error from one stored procedure into another in Sybase ASE 15.0

I need to execute a password reset on a Sybase ASE dataserver based on certain conditions:
if validations_here
begin
exec sp_password 'sso_passw', 'new_passw', #userid
end
sp_password might raise some errors, e.g. 10316 - "New password supplied is the same as the previous password". Although I couldn't find any documentation, I think they shouldn't be fatal errors and it should be possible to emulate them with raiserror.
Since it would be easier for the caller to handle it that way, I would like to get the error code and return it as part of a resultset, so I thought about SELECTing ##error. My code is as follows (I transcribed only those parts I think are relevant to the problem):
create procedure sp_desbloc_blanqueo_usr
#userid sysname,
#sso_pass varchar(20),
#new_pass varchar(20)
as
begin
declare #ret_code int
declare #ret_msg varchar(100)
declare #myerror int
select #ret_code = 0, #ret_msg = 'OK'
exec sp_password #sso_pass, #new_pass, #userid
set #myerror = ##error
if #myerror <> 0
begin
select #ret_code = #myerror, #ret_msg = 'Error occurred changing password'
-- It would be nice to have the actual error message as well
goto fin
end
fin:
select #ret_code as ret_code, #ret_msg as ret_msg
end
However, whenever I execute the stored procedure, I get 0 as ret_code and OK as ret_msg (even if parameters to sp_password are wrong).
How can I "catch" the error code of sp_password from my stored procedure?
Many "sp_" stored procedures set a nonzero return code when something goes wrong. Usually it is better to handle this return code than trying to catch errors raised inside the stored procedure. IIRC, this catching would not be possible with Transact-SQL; a 3rd generation language such as C would be required.
To get the return code of myproc stored procedure into variable #myvar, the syntax is
exec #myvar = myproc [arguments]
A simple example with sp_password:
declare #spreturn int
exec #spreturn = sp_password 'notmyoldpw', 'notmynewpw'
print "Return from sp_password is %1!", #spreturn
go
Server Message: Number 10315, Severity 14
Server 'SDSTRHA01_SY01', Procedure 'sp_password', Line 148:
Invalid caller's password specified, password left unchanged.
Server Message: Number 17720, Severity 16
Server 'SDSTRHA01_SY01', Procedure 'sp_password', Line 158:
Error: Unable to set the Password.
(1 row affected)
Return from sp_password is 1
(return status = 1)
The int variable #spreturn defined in the first line got sp_password return code, whose value was one as shown by (return status = 1) in the last message line. The reason why it was not zero is clear: there were two errors inside sp_password, 10315 and 17720. The point is to focus in this nonzero return code and ignore 10315 and 17720. In your stored proc, #spreturn ought to be checked against zero. If zero it ran OK, else something failed.

How to exit a Lua script's execution?

I want to exit execution of Lua script on some condition .
Example :
content = get_content()
if not content then
-- ( Here i want some kind of exit function )
next_content = get_content()
--example there can lot of further checks
Here I want that if I am not getting content my script suppose to terminate is should not go to check to next.
Use os.exit() or just return from some "main" function if your script is embedded.
os.exit()
kill process by sending a signal
do return end
stop execution
The two methods are not equal if you want to write and execute some luacode in the interpreter after stopping the execution by launching your program using the -i flag.
th -i main.lua
extract from the lua api doc :
For syntactic reasons, a break or return can appear only as the last statement of a block (in other words, as the last statement in your chunk or just before an end, an else, or an until). For instance, in the next example, break is the last statement of the then block.
local i = 1
while a[i] do
if a[i] == v then break end
i = i + 1
end
Usually, these are the places where we use these statements, because any other statement following them is unreachable. Sometimes, however, it may be useful to write a return (or a break) in the middle of a block; for instance, if you are debugging a function and want to avoid its execution. In such cases, you can use an explicit do block around the statement:
function foo ()
return --<< SYNTAX ERROR
-- `return' is the last statement in the next block
do return end -- OK
... -- statements not reached
end
In lua 5.2.0-beta-rc1+, you can add a label at the end of your code called ::exit:: or something of the like, and then whenever you need to exit the program just call it like this:
goto exit

Why is 'name' nil for debug.getinfo(1)

I'm trying to put together a lua testing framework that lets you know the function that had the problem, but when I switched from loadstring to _G, (I switched so my test harness could see the results of the function call) my functions started using 'nil' for the function name
Why can _G not detect the name of the current function in the following code? Also, how can I get the return results from loadstring (ie the 'false' from the blah call) or set the function name when using _G (ie. Tell the lua interpreter what the function name should be)?
function run_test(one, two)
if one ~= two then
print(debug.getinfo(2).name..' Failed')
end
end
function blah()
run_test(false, true)
return false
end
local fname = 'blah'
local status, result = pcall(_G[fname]) -- Outputs 'nil'; result is 'false'
local status, result = pcall(loadstring(fname..'()')) -- Outputs 'blah', result is 'nil'
The main thing I need is a way to call a function using a string of the function name, be able to see the function name inside the call (for test failures to point to the function that failed, like fname = 'blah' in the code above) and be able to get the return value
local fname = 'blah'
status, result = pcall(??Call fname somehow??)
assert(status)
assert(not result)
--stdout should be "blah Failed"
This is a limitation of the heuristics used by Lua to provide names for functions.
In Lua, all functions are anonymous. A given function can be the value of several variables: global, local, and table fields. The Lua debug system tries to find a reasonable name for a value based on where it came from by looking into the bytecode being executed.
Consider the simpler example
blah()
pcall(blah)
In the first call, the debug system sees that the function being called comes from the global blah and debug.getinfo(1).name gives the expected result, blah.
In the second call, the debug system sees that the function being called comes from the first argument to pcall but it does not look further to see where that argument came from, and debug.getinfo(1).name gives nil.
The same thing happens when you call _G[name](). All the debug system sees is a field of a table and the name of the field is too far off.
Try adding print(debug.traceback()) as the first line of blah to see another take on this explanation.

Resources