T SQL - null variable - sql-server-2008

I have a stored procedure being called from an .aspx.cs page. I have a parameter that sometimes cannot be sent when the sproc is called. Because of this I'm doing the following:
IF #variable is null
BEGIN
...do this...
END
Else
...do that...
My problem is in the IF statement. As far as I can tell when I use any of the following:
if #parameterVariable = null
if #parameterVariable = ''
if #parameterVariable <= 0
Nothing happens!? When I debug the sproc in SSMS I find that (even though the parameter is empty (no user selection)) that the cursor goes to and runs the code in the ELSE statement. Am I doing something wrong?
Thanks!

use optional parameter:
CREATE PROCEDURE uspTest
#param1 varchar(50) = null,
AS
BEGIN
SELECT col1, col2
FROM Table1
WHERE
((#Param1 IS NULL) OR (col1 = #Param1))
END

if #parameterVariable = null is wrong.
Change it to if #parameterVariable IS NULL.
Here is a SQL Fiddle demonstrating this: http://www.sqlfiddle.com/#!6/6cb42/1

when debugging in SMSS, you must check the box that says "Pass null value". otherwise your value is an empty string or somesuch.
I use the pattern you suggest all the time, and it works well for me.

i suggest you to read this page => ANSI NULLS
actually if #var = null is not wrong, everything depends on the value of ANSI_NULLS :)

Related

How can I pass a returned VARCHAR from a SELECT statement in a stored procedure?

Relevant code:
SELECT `startdate`, `problem`
INTO tktCompletedDate, problem
FROM servicerequest
WHERE (requestID = tktRequestID);
CASE problem
WHEN "Screen is broken" THEN
SET tktProbSpec = 'installed replacement screen';
ELSE SET tktProbSpec = 'Ran Diagnostics';
END CASE;
Trying to complete a class assignment (big surprise) and when I run this SELECT is returns the startdate and passes it into tktCompletedDate variable no problem, but the problem field does not get passed. When I check it later it says the value is NULL.

SQL 2008 Stored Procedure with Parameters from SQL Query

I have a stored procedure that has sever parameters that need declaring when its executed. I'm trying to establish a way of using the results of a query to assign a value to one of the parameters.
For example
EXEC #return_value = [dbo].[usp_Stored_Proc_demo]
#tableName = N'tbltesting',
#Col1 = N'c1',
#Col2 = N'c2',
#Col3 = N'c3',
#Col4 = (SELECT GETDATE()),
#Col5 = N'c5',
#Col6 = N'c6',
#Col7 = N'v7',
#Col8 = N'c8'
However this doesnt work, or anything else I have tried. Is there a way of doing it??
The options open to you really involve executing the pre-query first, assign the value to a variable and then use than in the sproc call:
SELECT #MyVar = Something
FROM dbo.Somewhere
WHERE WhatIAmLookingFor = 'AnAnswer'
EXECUTE dbo.MySproc #MyVar
You can't assign parameter values inline like you want to do based on the results of another query. You could do something using dynamic SQL I expect, but would advise against unless you have a really strong reason/need.

Mysql recursive procedure

I have a table name css_diectory with 3 columns directory_id, directory-name and root_directory. I am storing hierarchical directory structure in this table. I have written a procedure to retrieve all the descendants given the directory_id. But it doesn't work. Can anyone please help me.
Here is the procedure
CREATE PROCEDURE getDescendants
(IN rootId varchar(32), INOUT desecendantsFolderId varchar(3200))
BEGIN
DECLARE endOfRecord INTEGER DEFAULT 0;
DECLARE folderId varchar(32) DEFAULT "";
DECLARE folderName varchar(32) DEFAULT "";
DECLARE folderCursor CURSOR FOR
Select directory_id, directory_name from css_directory where root_directory=rootId;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET endOfRecord = 1;
OPEN folderCursor;
getFolderId: LOOP
FETCH folderCursor INTO folderId, folderName;
IF endOfRecord = 1 THEN
LEAVE getFolderId;
END IF;
Call getDescendants(folderId,desecendantsFolderId);
SET desecendantsFolderId = CONCAT(folderId,",",desecendantsFolderId);
call getDescendants(folderId,desecendantsFolderId);
END LOOP getFolderId;
END
Edit: The output of this procedure is always a null set. It does not produce any error
I'm not 100% sure I follow what you're doing, but it looks like there's a problem here:
SET desecendantsFolderId = CONCAT(folderId,",",desecendantsFolderId);
When any argument to CONCAT() is null, then the return value is null. Presumably descendantsFolderId is initially null, and if so, I don't see where that would change.
There are several ways to remedy this, but here is one of them:
SET desecendantsFolderId = NULLIF(CONCAT_WS(",",folderId,desecendantsFolderId),"");
CONCAT_WS() is like CONCAT(), except the first argument is used as a separator amd null arguments beyond the first one are disregarded, and empty string is returned if all subsequent arguments are null. NULLIF() is probably not technically needed, based on the rest of the code, but will ensure that the final result of CONCAT_WS() will be turned back to null if the input args are indeed all null.

How to increase the user' error times? unknown whether the original value is null

Before operate Access database,in the SQL express,I can just use this statement and it works very well:
UPDATE Person
SET FErrorTimes = IsNull(FErrorTimes, 0) + 1
WHERE (FUserName = #name)
by now, it reports as error syntax
can someone help me please,and thanks very much.
Access' IsNull is different than SQL Server's IsNull. In Access, IsNull accepts only one argument, and returns True or False to indicate whether that argument evaluates as Null.
Use one of these instead.
UPDATE Person
SET FErrorTimes = Nz(FErrorTimes, 0) + 1
WHERE FUserName = #name
UPDATE Person
SET FErrorTimes = IIf(FErrorTimes Is Null, 0, FErrorTimes) + 1
WHERE FUserName = #name
Note Nz() is only available for a query run within an Access session. If you're running a query from external code which connects to the Access db, use the second example.

Combobox null in if statement

I am trying to code an if statement where if a certain combobox is null, then it runs a certain part of code if it has data in it then it runs another. I wrote up this:
Private Sub ProjectAddSetDateAutoBtn_Click()
If ProjectAddAllDueDateAutoCmBx = Null Then
'Code1
Msgbox("ComboBox Is Null")
Else
'Code2
Msgbox("ComboBox Has Data")
End If
End Sub
I leave the combobox with no data, and then it doesn't run the code in the first part of the if or the code in the 2nd part of it either! If I enter data into the box, it runs the 2nd part of the if statement perfectly. There are no errors, I am quite stumped on this. Do ComboBoxes have their own "Null"? Is there a problem with this if statement?
Nothing is ever equal to Null, not even another Null.
Use IsNull() to check whether the combo box is Null.
'If ProjectAddAllDueDateAutoCmBx = Null Then
If IsNull(ProjectAddAllDueDateAutoCmBx) = True Then
I would suggest
If IsNull(ProjectAddAllDueDateAutoCmBx.Value) Then
It correctly checks for Null (IsNull instead of = Null), and it explicitly checks the value of the combo box.
(In most cases -- depending on the context -- just using the name of the control yields the value, but it doesn't hurt to be explicit.)
You cannot use a = Null comparison to get the results you want because Null propagates. To see this in action, try:
? Null = Null
in the Immediate Window and you'll see that Null is returned. Use the IsNull function, which will return true or false as you would expect.
Private Sub ProjectAddSetDateAutoBtn_Click()
If IsNull(ProjectAddAllDueDateAutoCmBx) Then
'Code1
Msgbox("ComboBox Is Null")
Else
'Code2
Msgbox("ComboBox Has Data")
End If
End Sub
While the accepted answer is totally correct, I use a different approach:
If HasValue(ProjectAddAllDueDateAutoCmBx) Then
where the HasValue function is:
Public Function HasValue(v As Variant) As Boolean
If Trim(v & "") <> "" Then
HasValue = True
Else
HasValue = False
End If
End Function
This has the advantage of treating NULL and "" (or any pure whitespace) values the same, which is many times what you want with MSAccess controls. For example entering a value in a null-valued textbox and removing it again with backspace will result in a ""-value, not NULL. From a user-perspective this is mostly meant to be the same.
[The (v & "")-part is just a trick to force conversion to a string.]
the equivalent of null in VB is Nothing so your check wants to be:
If ProjectAddAllDueDateAutoCmBx Is Nothing Then
....
it hope helps.