I'm trying to assign a NULL value when another field is NULL using a CASE statement.
So far this is what I got:
UPDATE PunchintervalFinal
SET INDTTIME_LUNCH = (SELECT CASE
WHEN PunchintervalFinal.INDTTIME2 IS NULL THEN
NULL
END
FROM PUNCHBRIDGE A
WHERE A.EMPLOYEE = PunchintervalFinal.EMPLOYEE
AND A.PUNCHDATE = PunchintervalFinal.PUNCHDATE
AND PunchintervalFinal.EMPLOYEE = '500018')
This should do what you are trying to do:
UPDATE PunchintervalFinal
SET INDTTIME_LUNCH = NULL
FROM PunchintervalFinal
INNER JOIN PUNCHBRIDGE A ON A.EMPLOYEE = PunchintervalFinal.EMPLOYEE
WHERE PunchintervalFinal.EMPLOYEE = '500018'
AND A.PUNCHDATE = PunchintervalFinal.PUNCHDATE
AND PunchintervalFinal.INDTTIME2 IS NULL
Related
I have this Merge statement
MERGE Destination d
USING #Source s
ON d.DestinationId = s.DestinationId
WHEN MATCHED AND (
ISNULL(d.DestinationFieldOne,0) != ISNULL(s.DestinationFieldOne,0) OR
ISNULL(d.DestinationFieldTwo,'') != ISNULL(s.DestinationFieldTwo,'') OR
ISNULL(d.DestinationFieldThree,'') != ISNULL(s.DestinationFieldThree,'') OR
ISNULL(d.DestinationFieldFour,'') != ISNULL(s.DestinationFieldFour,'')
THEN UPDATE SET
d.DestinationFieldOne = s.DestinationFieldOne,
d.DestinationFieldTwo = s.DestinationFieldTwo,
d.DestinationFieldThree = s.DestinationFieldThree,
d.DestinationFieldFour = s.DestinationFieldFour
WHEN MATCHED AND (
#Deleted = 1
)
THEN UPDATE SET
d.Deleted = 1
WHEN NOT MATCHED BY TARGET
THEN INSERT (DestinationFieldOne, DestinationFieldTwo, DestinationFieldThree, DestinationFieldFour) VALUES (s.DestinationFieldOne, s.DestinationFieldTwo, s.DestinationFieldThree, s.DestinationFieldFour)
It's giving me
An action of type 'WHEN MATCHED' cannot appear more than once in a
'UPDATE' clause of a MERGE statement.
Is there any other way to do it?
This is functionally the same as what you've written.
Which is: if #delete=1, only update the deleted column otherwise update all attributes
UPDATE Destination
SET
-- leave as old values if Deleted=1
d.DestinationFieldOne =
IIF(s.Deleted=1,d.DestinationFieldOne,s.DestinationFieldOne),
d.DestinationFieldTwo =
IIF(s.Deleted=1,d.DestinationFieldTwo,s.DestinationFieldTwo),
d.DestinationFieldThree =
IIF(s.Deleted=1,d.DestinationFieldThree,s.DestinationFieldThree),
d.Deleted = IIF(s.Deleted=1,1,d.Deleted)
FROM Destination d
INNER JOIN #Source s
ON d.DestinationId = s.DestinationId
But, if i make some bold assumptions about what's in #Source, I suspect this is fine:
UPDATE Destination
SET
d.DestinationFieldOne = s.DestinationFieldOne,
d.DestinationFieldTwo = s.DestinationFieldTwo,
d.DestinationFieldThree = s.DestinationFieldThree,
d.Deleted = s.Deleted
FROM Destination d
INNER JOIN #Source s
ON d.DestinationId = s.DestinationId
INSERT INTO Destination (
DestinationFieldOne, DestinationFieldTwo,
DestinationFieldThree, DestinationFieldFour)
SELECT s.DestinationFieldOne, s.DestinationFieldTwo,
s.DestinationFieldThree, s.DestinationFieldFour
FROM #Source S WHERE NOT EXISTS (
SELECT * FROM Destination D WHERE S.DestinationId = D.DestinationId
)
If either #Deleted or the other long condition, update all 5 columns:
UPDATE d set
DestinationFieldOne = case DestinationUpdateFlag when 1 then s.DestinationFieldOne else d.DestinationFieldOne end
,DestinationFieldTwo = case DestinationUpdateFlag when 1 then s.DestinationFieldTwo else d.DestinationFieldTwo end
,DestinationFieldThree = case DestinationUpdateFlag when 1 then s.DestinationFieldThree else d.DestinationFieldThree end
,DestinationFieldFour = case DestinationUpdateFlag when 1 then s.DestinationFieldFour else d.DestinationFieldFour end
,Deleted = case #Deleted when 1 then 1 else d.Deleted end
from
Destination d
INNER JOIN #Source s ON d.DestinationId = s.DestinationId
cross apply
(select case when
ISNULL(d.DestinationFieldOne,0) != ISNULL(s.DestinationFieldOne,0) OR
ISNULL(d.DestinationFieldTwo,'') != ISNULL(s.DestinationFieldTwo,'') OR
ISNULL(d.DestinationFieldThree,'') != ISNULL(s.DestinationFieldThree,'') OR
ISNULL(d.DestinationFieldFour,'') != ISNULL(s.DestinationFieldFour,'')
then 1 else 0 end as DestinationUpdateFlag
) as q1
where #Deleted = 1 or DestinationUpdateFlag=1
I have a query that is:
SELECT DISTINCT DepotIo.Depot2Guid AS Depot1Guid, Depot2.Title, NULL AS Depot2Guid
FROM DepotIo
JOIN DepotIoDetail ON DepotIo.Guid = DepotIoDetail.DepotIoGuid
JOIN dbo.GetUserDepot(#UserGuid) AS Depot2 ON DepotIo.Depot2Guid = Depot2.Guid
JOIN Item ON Item.Guid = DepotIoDetail.ItemGuid
WHERE DepotIo.Company = #Company AND (DepotIo.Branch = #Branch)
But I want to when #Branch is not null, comes to WHERE condintion part and when it's value is null, relinquish it.. Like this :
WHERE DepotIo.Company = #Company AND (CASE #Branch
WHEN IS NOT NULL THEN DepotIo.Branch = #Branch)
what's true command ??
This is usually handled using or:
WHERE DepotIo.Company = #Company AND
(DepotIo.Branch = #Branch OR #Branch IS NULL)
You can use CASE WHEN as follows:
(CASE WHEN #Branch IS NOT NULL
THEN CASE WHEN DepotIo.Branch = #Branch THEN 1 ELSE 0 END
ELSE 1 END = 1)
Cheers!!
If I understand the question, this should do it.
If DeDepotIo.Branch is not nullable. This will relinquish rows where #Branch<>DeDepotIo.Branch, but not when #Branch is NULL.
WHERE DepotIo.Company = #Company AND (ISNULL(#Branch, DeDepotIo.Branch) = DeDepotIo.Branch)
The parameter has two values direct reports and indirect report.
I have tried:
select distinct cch.emp_person_id,
cch.lvl_num Empl_Lvl,
cch_lv.lvl_num MGR_LVL
from cmp_cwb_hrchy cch, cmp_cwb_hrchy cch_lv
where cch.mgr_person_id = :Mgr_id and
cch_lv.emp_person_id = :Mgr_id and
cch.MGR_PERSON_EVENT_ID= cch_lv.MGR_PERSON_EVENT_ID and
cch.EMP_PERSON_EVENT_ID = cch_lv.EMP_PERSON_EVENT_ID and
cch.lvl_num = cch_lv.lvl_num and
( 1=1 and cch.lvl_num = case when :direct_ALL = 'Direct' then cch_lv.lvl_num+1 else NULL end) and
( 1=1 and cch.lvl_num > case when :direct_ALL = 'ALL' then cch_.lvl_num else NULL end)
I'm new in sql server and I have WHERE clause like this:
WHERE[D].[IsLocked] = 0
AND(#StartDate IS NULL OR ISNULL([TA].[ModifiedDate], [TA].[CreationDate]) >= #StartDate)
AND(#EndDate IS NULL OR ISNULL([TA].[ModifiedDate], [TA].[CreationDate]) <= #EndDate)
AND((CASE WHEN[T].[TaskStatusId] = '09E02513-00AD-49E3-B442-A9ED2833FB25'
THEN 1 ELSE 0 END) = #Completed)
AND((#FilterEmpKey IS NULL AND[TA].[EmpKey] = #CurrentEmpKey)
OR (ISNULL([TA].[ModifiedAssignedBy], [TA].[AssignatedBy]) = #FilterEmpKey
AND[TA].[EmpKey] = #CurrentEmpKey))
But now I want to add if conditional in order to add more filters at the end of query like:
IF(#FilterEmpGuid IS NOT NULL)
AND[TA].[EmpKey] = #CurrentEmpKey
AND[TA].[AssignatedBy] = #CurrentEmpKey
AND[TA].[EmpKey] = #FilterEmpKey
But I get:
The multi-part identifier [TA].[EmpKey] could not be bound
What am I doing wrong?
IF conditionals are only for use outside sql queries, such as in procedures etc.
In a query itself you are limited to AND, OR and CASE statements, so you will need to rewrite your IF conditional for this:
AND (#FilterEmpGuid IS NULL
OR (
[TA].[EmpKey] = #CurrentEmpKey
AND[TA].[AssignatedBy] = #CurrentEmpKey
AND[TA].[EmpKey] = #FilterEmpKey
))
You could move the additional filter options into a scalar function.
If you know the additional fields that may be filtered, you may be able to get away with something like:
CREATE FUNCTION dbo.ExtendFilter(
#column_value VARCHAR(50), #param_value VARCHAR(50)
)
RETURNS BIT
AS
BEGIN
DECLARE #return BIT = 1; -- default RETURN to 1 ( include ).
IF ( NULLIF( #param_value, '' ) IS NOT NULL )
BEGIN
-- compare the column's value to the param value
IF ( #column_value <> #param_value )
SET #return = 0; -- don't include this record.
END
RETURN #return;
END
GO
And then use it like:
WHERE
{ other WHERE stuff }
AND dbo.ExtendFilter( [TA].[EmpKey], #CurrentEmpKey ) = 1
AND dbo.ExtendFilter( [TA].[AssignatedBy], #CurrentEmpKey ) = 1
AND dbo.ExtendFilter( [TA].[EmpKey], #FilterEmpKey ) = 1
Mind you this is just an example. You'd want to check #pram_value for NULL, etc...
Here is my stored procedure for updating tables in SQL Server, but I can not seem to get it working what is wrong with the statement.
Update pc.PatientCopayId, pc.amount, pc.patientid, pc.apptid ,p.PaymentId,p.PaymentDate,p.PayorType,p.PaymentMethod,
p.RefNumber,p.PaymentAmount,p.PayorId,pt.LastFirstName As PatientName,
ISNULL((SELECT note FROM dbo.PatientNote WHERE NoteTypeId = 28 AND KeyValue = pc.PatientCopayId),'') AS Note
from [dbo].[PatientCopay] pc, dbo.pymt_Payment p, dbo.Patient pt
where ApptId = #ApptId
and p.PaymentId = pc.Paymentid
And pt.PatientId = p.PayorId
Values and meaning
pc.amount, = #PaymentAmount
pc.patientid, = #PatientId
pc.apptid , = #ApptId
p.PaymentId, = #PaymentId
p.PaymentDate, = #PaymentDate
p.PayorType, = #PayorType
p.PaymentMethod, = #PaymentMethod
p.RefNumber, = #RefNumber
p.PaymentAmount, = #PaymentAmount
p.PayorId, = #PayorId
The UPDATE statement shoudl have the following form
UPDATE TableName SET....
Your update statement doesn't have the tablename or the SET keyword.
More Info HERE