CREATE PROCEDURE GET_DATE(START_DATE VARCHAR, IS_SET VARCHAR(1), TIME_ZONE VARCHAR)
...
if (START_DATE) {
}
CALL GET_DATE(NULL, '1', 'PST');
What is the proper way to pass the default NULL value to stored procedure?
(NULL, '1', 'PST') doesn't execute and it just returns a row that says "NULL".
When you create the procedure there are two optional parameters CALLED ON NULL INPUT and RETURNS NULL ON NULL INPUT | STRICT:
CALLED ON NULL INPUT means the procedure is executed when you pass a NULL value and the procedure has to handle the NULL values.
RETURNS NULL ON NULL INPUT | STRICT means the procedure won't execute when you pass a NULL value. Instead the procedure just returns NULL.
The first parameter is default - maybe you set it accidentally by copying the CREATE PROCEDURE-code from somewhere else. More info: https://docs.snowflake.com/en/sql-reference/sql/create-procedure.html
Another hint is about case-sensitivity: Maybe inside your procedure your parameter is referred to by its lowercase name, but Snowflake has automatically converted the name to uppercase. See here: https://docs.snowflake.com/en/sql-reference/stored-procedures-usage.html#stored-procedure-or-udf-unexpectedly-returns-null
And one last thing you may think about is the code of your procedure. Maybe passing NULL works fine but your code produces a NULL value and thus the returned NULL value is correct.
Please refer to https://community.snowflake.com/s/article/Store-Procedure-and-Nulls
In Javascript store-procedures nulls are handled in a special way, SQL Nulls automatically get converted into 'UNDEFINED' inside the store procedure. The end-user should always take care of this by replacing its value inside the procedure, please have a look at the documentation page for further details.
The following are the three important scenarios/use-cases reflecting this behavior.
Call of the procedure with null values: when we pass nulls, as soon
as it comes inside the procedure they get transformed by 'undefined'.
Shows "undefined" within the procedure: Inside the procedure, if the
same passed parameter with Null is used then it will show
'undefined'. if the same value is inserted into a table, the table
will contain 'undefined' instead of null. Use replace inside the
procedure to cope with this.
Should return null values as result: If procedure returns
'undefined' it will be automatically converted as NULL outside the
procedure.
so to handle null, use piece of code mentioned below
if(START_DATE === undefined){
do something to handle NULL
}
Related
How to stop showing messages from procedure?
Currently, the problem is I have created a stored procedure Procedure A. It executes another procedure B from its code, and the B procedure executes yet other procedures C,D,E from its code.
The problem is when the A procedure executes, it shows 4 result sets like 0,1 or another values. But I want to show only the result from procedure A. How can I achieve this? I can't change the other procedures B,C,D,E because they also perform their individual tasks.
If you need clarification please ask.
You can try the followings:
if any of the sub procedures is returning only one row set and it is static (same columns with the same type are always returned), you can materialized the result set in temporary tables (or table variables)
For example, let's say that procedure X returns a table with two int columns. You
materialized the result like this:
CREATE TABLE #X
(
A INT
,B INT
);
INSERT INTO #X
EXEC usp_X;
Add additional parameter to the sub procedures or use any of the existing ones to not return the row set(s) if certain option is passed.
For example, add #MiscSettings parameter to your existing procedure X:
ALTER PROCEDURE usp_X AS
(
#Param01 INT
,#Parame02 VARCHAR(12)
,...
,#MiscSettings NVARCHAR(MAX) = NULL
)
The parameter is not mandatory, so you are not going to break any existing reference. Then in the procedure you can check if [DoNotReturnResultSet] string is passed in the #MiscSettings to not return the result sets. Existing references will continue to work because by default the row sets are returned.
IF Option Is Not Passed
BEGIN;
SELECT ...
END;
In both ways you can suffer if someone change the code of the sub routines. For example, if a type of returned column is changed, or someone add additional row set without checking if your special option is passed.
Note, in the second technique, if you do not like to add additional parameter, you can use some of the existing strings (for example). Just check if the string contains your option and then replace it.
When I have an execute sql task where I have the
Select '' AS [CancelledDate] from TableX
I map this column to a data time variable called CancelledDate, however when I run the Execute Sql Task I get this Error:
Error: 0xC002F309 at Sp Get Parameter for SP2, Execute SQL Task: An error occurred while assigning a value to variable "CancelledDate": "String was not recognized as a valid DateTime.".
Task failed: Sp Get Parameter for SP2
If I replace the '' with '01-JAN-1978' it works. This must have a problem with Null and '' in the columns.
I tried to put NULL(DT_DATE) in the variable and set evaluate as expression to True but that did not work either!
Well, first of all - empty string is not the same as NULL. NULL (no value) can be "converted" to any type, 'cause each type may be in NULL state while empty string is not nothing and it may or may not be successfully converted to the type you need.
Second - column cannot have no type. Which type is your column CancelledDate? It is varchar. I guess, varchar(1). It contains empty string. Is this a valid datetime? No.
Always explicitly specify type for such a virtual column. And for logical NULL use NULL itself instead of empty string or zero or something else.
Here is sample code that returns datetime column with NULL:
select cast(NULL as datetime) AS [CancelledDate]
from TableX
I have a dataset that is driven by a stored procedure requiring a parameter. The parameter is DATETIME but I am unable to present in that way in the report because DateTime's show as a calendar and I need a dropdown of values.
Alas, I have a dataset that drives the choices for the parameter called BaselineDate. The issue comes down to the way that SSRS formats the DateTime value for the parameter then passes as VARCHAR to the stored procedure. The VARCHAR value is in the wrong format.
SSRS
Expected Value
2016-04-07 13:01:19.173
Update
Per #Marco Bong's suggestions I have converted the dataset that is driving the Parameter options to the proper format. Unfortunately, SSRS I passing the parameter value as null.
Below is what is a debug table I created. I am simply inserting the value of the parameter into this table. As you can see SSRS is passing null to the stored procedure as NULL which in theory should be impossible as the parameter is set to not allow nulls. Any ideas?
select CONVERT(NVARCHAR,getdate(),21) as dtValue
//output will be ===> 2016-04-13 08:32:16.697
Update
If you want to use that selected value in another dataset (which execute your store prod), you may need to do this:
Then you can use this #param1 in your query.
Either where something = #param or set #baselineDate = #param
I fetch a result set with an execute SQL task. It has only one column NullTime varchar. It has three rows, first one is NULL. I want to simply iterate and display the value of these rows. If I do it only by C# script, then there is no problem. The NULL is displayed as a blank. The same thing can also be done with a foreach loop.
How to do it with foreach - use that loop to read each row and set the value of each row to SSIS string User::STR_WORD. Then, simply display User::STR_WORD with a C# script task.
In the pure C# method, I can even assign the blank value (actually a NULL) to the SSIS string. But with foreach loop method, I get an error because of the NULL value.
The error is -
Error: The type of the value being assigned to variable "User::STR_WORD" differs from the current variable type. Variables may not change type during execution. Variable types are strict, except for variables of type Object.
How do I fix this error ? Is script the only alternative to what seems to be a flawed for
loop ?
A workaround for this is the Coalesce function which will convert NULLs to the value you specify.
SELECT
COALESCE([YourColumn], 'EnterValueHere') AS [YourColumn]
FROM YourTable
So, this will replace a null with the value you want to use instead. It will
prevent the weird FOR loop from suddenly crashing.
Create a temporary table to see this working -
create table ##tester
(names varchar(25))
insert into ##tester
values(Null)
insert into ##tester
values('Not a null value')
select names from ##tester
select coalesce(names, 'Null has been obliterated!') from ##tester
I have a SQL Server table with a few columns of type varchar which are nullable. When an aspx page posts data if the textbox is empty, the database table column is updated to an empty string.
To maintain the null value rather han have it replaced by an empty string, I can either have the logic to change the empty string to System.DBnull in the middle tier c# code or do so in the stored procedure.
Are there other better ways to handle this situation?
you can use a trigger or do it in the proc, you can use the NULLIF function
Example
DECLARE #d VARCHAR(20)
SELECT #d = ''
SELECT NULLIF(#d,'')
I would probably put it near the code that makes a decision based on the distinction between an empty string and a null string. If there is no difference between their meaning or the resulting behavior of the application, I would suggest making the field non-nullable and sticking with an empty string.