How do I set a default datetime parameter in a stored procedure? - stored-procedures

I have declared a stored procedure in Sybase, and one of the parameters is of type datetime. Now I want to assign this datetime a default value.
Here's the declaration:
create procedure Procedure
(
#fromDate datetime = getdate()
)
...
However Sybase is giving me an error
Number (102) Severity (15) State (1) Server (SERVER) Procedure (Procedure) Incorrect syntax near '('.
Is it possible to do this? If not, is there a workaround?

You can not use a function call in a default variable assignment (as you found out).
Set the default to Null, and put an assignment first thing in the stored procedure.
create procedure Procedure
(
#fromDate datetime = NULL
)
begin
set #fromDate = coalesce( #fromDate , getdate() )
end

Related

Assigning default value to snowflake stored procedure arguments

Is it possible to have default values in arguments of Stored procedures of Snowflake. For the below example, I am getting error. Please help
syntax error line 1 at position 53 unexpected ''test''.
create or replace procedure test(arg1 string default 'test')
returns string not null
language sql
as
$$
begin
return arg1;
end;
$$
;
Snowflake's procedures applies polymorphism instead of using default value. This solution is when you do not want to call sp like func1(Null)
For example (sql scripting):
create or replace procedure func1(arg1 varchar, arg2 varchar)
...
create or replace procedure func1(arg1 varchar)
...
call func1(arg1 , 'some-default-value')
...
One option could be providing NULL as value and handle it at the begining of the stored procedure with COALSESCE:
create or replace procedure test(arg1 string)
returns string not null
language sql
as
$$
begin
arg1 := COALESCE(arg1, 'test');
return arg1;
end;
$$;
CALL test(NULL);
-- test
Setting a default value/values as arguments directly in Stored procedures is not available in Snowflake currently
The below link can be referred for the allowed syntax in Stored Procedures
https://docs.snowflake.com/en/sql-reference/sql/create-procedure.html#syntax

Dynamic SQL calling another stored procedure in Teradata

I've seen in the help guides that you cannot use a call statement in a teradata dynamic sql statement without output parameter.
I assume this implies I can, If the proc has an output parameter.
has anyone done this?
Scenario -
I have a table that at some point I'll expand out in terms of fields for logic on when things should run, and this table is managed elsewhere -
CREATE TABLE DB.SP_Test
(
ProcName VARCHAR(250)
,ProcRun VARCHAR(1)
);
now I added chrTest as an output however, I am still getting an error on run (no compile error)
The error :-
SQL_State SQL_Exception
T7689 Invalid dynamic SQL statement.
REPLACE PROCEDURE DB.Test_Control (OUT chrTest VARCHAR(250) )
SQL SECURITY INVOKER
LMain:
BEGIN
DECLARE sqlProcRun VARCHAR(20000);
DECLARE CONTINUE HANDLER
FOR SqlException
BEGIN
-----------------------------------------------------------------------------
DECLARE strExceptionText VARCHAR(250);
GET DIAGNOSTICS EXCEPTION 1 strExceptionText = Message_Text;
INSERT INTO DB.PROC_ERROR VALUES
(
'Test_Control'
,:SqlState
,strExceptionText
,Current_Timestamp
)
;
END;
-----------------------------------------------------------------------------
SET sqlProcRun ='';
L0:
FOR procs_run_cursor AS select_list
CURSOR FOR
SELECT Trim(ProcName) AS ProcName
FROM DB.SP_Test
DO
/*creating a lost of call statements to run*/
SET sqlProcRun = sqlProcRun ||'CALL DB.'||procs_run_cursor.ProcName|| '();';
END FOR L0;
EXECUTE IMMEDIATE sqlProcRun;
END;

Error while connecting to stored procedure in Tableau

I'm getting this error while trying to connect to a stored procedure as data source:
Unable to complete action
The stored procedure returned no results. The fields don't have column names, or the data in the fields is not a supported data type
But the same stored procedure returns data in database. Any idea on this?
Please try adding the SET NOCOUNT ON code in the beginning of the stored procedure.
Something like
CREATE PROCEDURE [dbo].[ZZZ_THIS_IS_MY_SP]
#p INT
AS
BEGIN
SET NOCOUNT ON
DECLARE #Var1 NVARCHAR(20)
DECLARE #Var2 DATETIME
-- All other instructions...
END

HSQL problem when using TIMESTAMP value as IN parameter in stored procedure

Overview HSQL IN parameter of type TIMESTAMP doesn't work as expected for HSQL stored procedure.
Given the following DDL :
CREATE TABLE TS_STORE (
ID_COL VARCHAR(20) NOT NULL PRIMARY KEY,
TS TIMESTAMP
);
A DML statement such as :
INSERT INTO TS_STORE (ID_COL, TS) VALUES ('key1', '2020-02-19 12:17:53');
will successfully insert a row.
Then when I attempt to create a stored procedure to do the same as:
CREATE PROCEDURE TEST_PROC(IN IN_KEY VARCHAR(20), IN IN_TS TIMESTAMP)
MODIFIES SQL DATA
BEGIN ATOMIC
INSERT INTO TS_STORE(ID_COL, TS)
VALUES (IN_KEY, IN_TS);
END;
and attempt to call it as:
CALL TEST_PROC('key2', '2020-02-19 12:17:53');
Then I get an error: "incompatible data type in conversion".
This is a problem for me, since I am not allowed to change the signature of the the stored procedure to bypass the problem, since in my case HSQL is used as a mock for a production database (DB2) where the equivalent procedure works as expected.
It works if you call the procedure with a TIMESTAMP value, as opposed to the character string.
CALL TEST_PROC('key2', TIMESTAMP'2020-02-19 12:17:53');

Pass NULL parameter to TFDConnection.ExecSQLScalar

Is there a possibility to pass NULL value to some parameter of this FireDAC query?:
conn: TFDConnection;
fPar1, fPar2, fPar3: OleVariant;
cnt := conn.ExecSQLScalar(
'SELECT COUNT(*) FROM my_table WHERE par1=:p1 AND par2=:p2 AND par3=:p3',
[fPar1, fPar2, fPar3]
);
Is it possible without intermediate TFDQuery using TFDConnection object only?
Yes, you can do this, despite the fact that the parameters TFDConnection uses
for ExecSQLScalar are not directly accessible from your calling code, but it may not produce the result you are expecting unless you modify your SQL - see below.
Presumably, you have had an error message like "[FireDAC] parameter type [fPar2 ] is unknown ..." if you set fPar2 to Null beforehand.
You can avoid that by using the override of ExecSQLScalar that allows you to specify
the field types of the parameters in an open array following the parameter
which lists the variants, as in e.g.
cnt := conn.ExecSQLScalar(
'SELECT COUNT(*) FROM my_table WHERE par1=:p1 AND par2=:p2 AND par3=:p3',
[fPar1, fPar2, fPar3],
[ftString, ftString, ftString] // or whatever
);
See
function TFDCustomConnection.ExecSQLScalar(const ASQL: String;
const AParams: array of Variant; const ATypes: array of TFieldType): Variant;
in FireDAC.Comp.Client.Pas
BUT, on my data here, this does NOT produce the correct count value (using Seattle and SS2014) presumably because of Uwe Raabe's good point about par1 = Null versus par1 is Null. To get the correct answer, I had to modify the SQL as per Keith Miller's comment to include set ansi_nulls off before SELECT ...

Resources