Can I check if a variable is NULL within an Update's SET? - sql

I have a stored procedure that uses a simple UPDATE with some variables passed to it. But I don't want to update those fields when their variables aren't null. This is essentially what my statement looks like.
UPDATE myTable
SET myColumn = #myColumn,
mColumn1 = #myColumn1
WHERE myColumn2 = #myColumn2
Is there anyway to apply some conditional logic within the SET? I have around 10 fields that need to be checked, so I wouldn't want to do an update per field or something like that.
Any ideas?

COALESCE is your friend. It returns its first non-NULL argument. I'm not actually sure from your narrative which way around you want things, it's either:
UPDATE myTable
SET myColumn = COALESCE(myColumn,#myColumn),
mColumn1 = COALESCE(myColumn1,#myColumn1)
WHERE myColumn2 = #myColumn2
Which keeps the current column's value if the column's not null, or
UPDATE myTable
SET myColumn = COALESCE(#myColumn,myColumn),
mColumn1 = COALESCE(#myColumn1,myColumn1)
WHERE myColumn2 = #myColumn2
Which keeps the current column's value if the variable is null.

Try to use coalesce function as below
UPDATE myTable
SET myColumn = coalesce(myColumn,#myColumn),
mColumn1 = coalesce(mColumn1,#myColumn1)
WHERE myColumn2 = #myColumn2
Above code updates your columns only when they are null. If they are not null the code sets the same value stored in the columns.

ISNULL ( variable , in case of null default value)
INFO

Related

Putting variable table column into permanent table, but get error that it isn't a scalar variable

update autos
set autos.risico = #tbldeelnames.risico
Get an error on #tbldeelnames
Scalar value refers to a single value.
UPDATE a
SET a.riscio = t.risico
FROM #autos a
JOIN #tbldeelnames t on a.key = t.key
OR:
DECLARE #risico INT = 0;
[Do stuff to find value of risico and set it]
UPDATE Autos SET risico = #risico
Your update statement assumes there's only one autos.risico and only one #tbldeelname.risico, which means you don't need tables to hold the value(s), and your update statement makes no sense in that context.
If your #tbldeelnames contains more than one record, then you need to specify WHICH record to use from #tbldeelnames. And, which record from #autos to update, otherwise all records in #autos will be updated with the value from the chosen record in #tbldeellnames.

where condition have a declared variable on a wrong place do not know if its valid

I have a statement like this below in one of my big stored proc, and I am wondering if this is valid to write this way.
SELECT #PVDate = pv.Date,
#PVdMBeginDate = dbo.fPVoidDate(pv.myID)
FROM PVMeter pv (NOLOCK)
WHERE pv.PR_ID = #PR_ID
AND #VCommen BETWEEN pv.PVDMDate AND dbo.fPVoidDate(pv.myID)
Now, here, my question is, #VCommen is a declared date variable with a value set on it. It is not at all a column of PVMeter, while PVDMDate is a column in PVMeter and fpVoidDate returns datetime
While I debug SP, I do not see the value on #PVDate and #PVDMBeginDate
The original query is equivalent to:
SELECT
#PVDate = pv.Date,
#PVdMBeginDate = dbo.fPVoidDate(pv.myID)
FROM PVMeter pv (NOLOCK)
WHERE
pv.PR_ID = #PR_ID
AND pv.PVDMDate <= #VCommen
AND #VCommen <= dbo.fPVoidDate(pv.myID)
This style should be more familiar. In general, you can put any expression in the WHERE clause, it can be made of variables or constants without referring to table columns at all.
Examples
Classic example: WHERE 1=1 ... when the query text is generated dynamically. It is easy to add as many expressions as needed in no particular order and prepend all of them with AND.
DECLARE #VarSQL nvarchar(max);
SET #VarSQL = 'SELECT ... FROM ... WHERE 1=1 ';
IF ... THEN SET #VarSQL = #VarSQL + ' AND expression1';
IF ... THEN SET #VarSQL = #VarSQL + ' AND expression2';
IF ... THEN SET #VarSQL = #VarSQL + ' AND expression3';
EXEC #VarSQL;
Thus you don't need to have complex logic determining whether you need to add an AND before each expression or not.
Another example.
You have a stored procedure with parameter #ParamID int.
You have a complex query in the procedure that usually returns many rows and one column of the result set is some unique ID.
SELECT ID, ...
FROM ...
WHERE
expression1
AND expression2
AND expression3
...
You want to return all rows if #ParamID is NULL and only one row with the given ID if #ParamID is not NULL. I personally use this approach. When I open the screen with the results of a query for the first time I want to show all rows to the user, so I pass NULL as a parameter. Then user makes changes to a selected row, which is done through a separate UPDATE statement. Then I want to refresh results that user sees on the screen. I know ID of the row that was just changed, so I need to requery just this row, so I pass this ID to procedure and fetch only one row instead of the whole table again.
The final query would look like this:
SELECT ID, ...
FROM ...
WHERE
(#ParamID IS NULL OR ID = #ParamID)
AND expression1
AND expression2
AND expression3
...
OPTION (RECOMPILE);
Thus I don't have to repeat the complex code of the query twice.

Update or insert element into xml column dependent on existence

I'm trying to write an update statement that checks if an element exists in an XML column, and updating it's value if it exists. If not it will then insert the value as a new element.
Something like:
UPDATE Table
SET xmlCol =
case
when xmlCol.exist('element') = 1
then xmlCol.modify('replace value of blah')
else xmlCol.modify('insert blah')
end
where whatever
Am I going about this the wrong way?
The modify() method of the xml data type can only be used in the SET clause of an UPDATE statement.
It's probably simplest to do it in two statements.
UPDATE Table
SET xmlCol.modify('replace value of /blah')
WHERE xmlCol.exist('/blah') = 1;
UPDATE Table
SET xmlCol.modify('insert /blah')
WHERE xmlCol.exist('/blah') = 0;

update conditionally a field in case parameter is not null

I need a query to update one field. If the passed parameter is null the do NOT update it with the null value of the parameter
update myTable
set myField1 = :param1
environment: hibernate and oracle
Can't you just put it in there where clause?
update myTable
set myField1 = :param1
where :param1 is not null
That'll avoid extra DML. Alternatively you can do:
update myTable
set myField1 = decode(:param1, null, myField1, :param)
But that means you'll update a field to the same, which isn't really optimal when you don't have to.

Fill entire SQL table column

I just added a new column in my DB which I need to propagate with a specific text value (Confirmed). Is there a simple way to apply to all my data entries so I don't have to go through all my rows and type the value in?
Thanks
you run the statement:
UPDATE whateveryourtableis SET whateveryourcolumnis = 'whatever';
Yould could make the desired value the new column's DEFAULT e.g.
ALTER TABLE MyTable ADD
my_new_column VARCHAR(20) DEFAULT 'Confirmed' NOT NULL;
Yes there is:
UPDATE [table]
SET [column] = 'Confirmed'