Update doesnt work with condition - sql-server

I am trying to update BudCustomers.ImportedRecord Column and BudCustomers.STATUSID Column. The data for these two columns are 1 and 1. so i am trying to update to 0 for both but that doesnt work with this below query
--Update into BudCustomers from Bulk
Update BudCustomers
set BudCustomers.ImportedRecord = 0
,BudCustomers.VersionID = 1
,BudCustomers.STATUSID = 0
,BudCustomers.LastModifiedUserID = 'Import'
,BudCustomers.LastModifiedDate = GETDATE()
FROM BudCustomers BCUST WITH(NOLOCK)
Where ((BCUST.STATUSID <> 1)
OR (BCUST.ImportedRecord <> 1)) AND BCUST.LegalName = 'Test, LTD. (1000)'

From your description this is probably what you want.
Update BudCustomers
set ImportedRecord = 0
, VersionID = 1
, STATUSID = 0
, LastModifiedUserID = 'Import'
, LastModifiedDate = GETDATE()
Where
(
STATUSID = 1
OR
ImportedRecord = 1
)
AND LegalName = 'Test, LTD. (1000)'

Related

Getting TFS work items on SSMS

I need some help mimicking a TFS query.
I have tried writing the following SQL query to do so but I am not able to get a matching population:
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
IF OBJECT_ID('tempdb.dbo.#LastRecord') IS NOT NULL DROP TABLE #LastRecord
SELECT System_Id
, LastUpdatedDateTime = MAX(LastUpdatedDateTime)
INTO #LastRecord
FROM [dimWorkItem]
GROUP BY System_Id
SELECT distinct ID = w.System_Id
--Cannot find Department
, Department = '?'
, w.TeamProjectSK
, RequestedByUser = w.Custom_RequestedBy
, Title = w.System_Title
, [Priority] = w.Microsoft_VSTS_Common_Priority
, Activity = w.Microsoft_VSTS_Common_Activity
, da.AreaPath
, [State] = w.System_State
, Reason = w.System_Reason
, ClosedDate = w.Microsoft_VSTS_Common_ClosedDate
, ActivatedDate = Microsoft_VSTS_Common_ActivatedDate
, DevHours = Custom_DevHours
, CreatedDate = w.System_CreatedDate
, AssignedTo = dp.Name
, [Week] = CASE WHEN (w.System_State = 'Active' OR Microsoft_VSTS_Common_ClosedDate >= dateadd(d,-7,CONVERT(Date,getdate(),1))) THEN 'Current'
WHEN (w.System_State = 'Active' OR Microsoft_VSTS_Common_ClosedDate >= dateadd(d,-14,CONVERT(Date,getdate(),1)) AND Microsoft_VSTS_Common_ClosedDate < dateadd(d,-7,CONVERT(Date,getdate(),1))) THEN 'Last'
ELSE 'Previous' END
, TSRNumber = Custom_TSRNumber
FROM
(Select * from (select *, rn = row_number() over(partition by w1.System_Id, w1.TeamProjectSK, w1.System_State order by w1.System_Rev desc)
from dbo.DimWorkItem w1) d where rn = 1) w
JOIN #LastRecord l ON w.System_Id = l.System_Id AND w.LastUpdatedDateTime = l.LastUpdatedDateTime
LEFT OUTER JOIN dbo.DimArea da ON w.AreaSK = da.AreaSK
LEFT OUTER JOIN DimPerson dp ON w.System_AssignedTo__PersonSK = dp.PersonSK
WHERE System_WorkItemType = 'Task'
AND da.AreaPath like '\LOS\Reporting%'
AND w.Microsoft_VSTS_Common_Priority < 4
Also if anyone knows where I could find the department field as I am not sure whether it is a custom field or not.
SQL Server 2014: 12.0
TFS: 12.0
Visual studio professional 2015: 14.0
Thank you in advance for your suggestions!
I found a solution although still not able to find the the Department field :
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
IF OBJECT_ID('tempdb.dbo.#LastRecord') IS NOT NULL DROP TABLE #LastRecord
SELECT System_Id, LastChangedDate = MAX(System_ChangedDate)
INTO #LastRecord
FROM [dimWorkItem]
GROUP BY System_Id
DECLARE #FromDateWeek DATE
DECLARE #FromDateTwoWeek DATE
SET #FromDateWeek = dateadd(d,-7,CONVERT(Date,getdate(),1))
SET #FromDateTwoWeek = dateadd(d,-14,CONVERT(Date,getdate(),1))
--SELECT #FromDate
SELECT ID = w.System_Id
--Cannot find Department
, Department = '?'
, w.TeamProjectSK
, RequestedByUser = w.Custom_RequestedBy
, Title = w.System_Title
, [Priority] = w.Microsoft_VSTS_Common_Priority
, Activity = w.Microsoft_VSTS_Common_Activity
, da.AreaPath
, [State] = w.System_State
, Reason = w.System_Reason
, ClosedDate = w.Microsoft_VSTS_Common_ClosedDate
, ActivatedDate = Microsoft_VSTS_Common_ActivatedDate
, DevHours = Custom_DevHours
, CreatedDate = w.System_CreatedDate
, AssignedTo = dp.Name
, [Week] = CASE WHEN w.Microsoft_VSTS_Common_ClosedDate >= #FromDateWeek OR w.System_State = 'Active' THEN 'Current'
WHEN (w.Microsoft_VSTS_Common_ClosedDate >= #FromDateTwoWeek AND w.Microsoft_VSTS_Common_ClosedDate < #FromDateWeek)
OR w.Microsoft_VSTS_Common_ClosedDate < #FromDateWeek THEN 'Last'
ELSE 'Previous' END
FROM [dimWorkItem] w
JOIN #LastRecord l ON w.System_Id = l.System_Id AND w.System_ChangedDate = l.LastChangedDate
LEFT OUTER JOIN DimPerson dp ON w.System_AssignedTo__PersonSK = dp.PersonSK
LEFT OUTER JOIN dbo.DimArea da ON w.AreaSK = da.AreaSK
WHERE
da.AreaPath LIKE '\LOS\Reporting%'
AND System_WorkItemType = 'Task'
AND (Microsoft_VSTS_Common_ClosedDate >= #FromDateTwoWeek
OR w.System_State = 'Active')
AND ISNULL(Microsoft_VSTS_Common_Priority,0) < 4
AND w.System_Reason <> 'Obsolete'

what should i use if i want to update my record if a certain condition its true

i want to update my records, if the day of the last payment of the client its greater than 100;
update clients set ind_mo = 2,
ind_pay=2
if its less than 100;
update clients set ind_mo = 1,
ind_pay = 1
if the the own of the client its = 0
update clients set ind_mo = 1,
ind_pay = 1
i already tried this but it does not update my record if the own its 0
if (datediff(day,((select top 1 fec_ven from
cxc_cuedoc cue inner join cxc_cliente cli
on cli.cod_cli = cue.cod_cli where sal_doc !=0 and cue.tip_doc = '010' and num_doc=cue.num_doc)),getdate())) > 110
update cxc_cliente
set ind_mora ='2',
IND_JURIDICO = '2';
else
update cxc_cliente
set ind_mora = '1',
ind_juridico ='1'
UPDATE Cli
SET Cli.ind_mo = CASE WHEN DATEDIFF(DAY , fec_ven , GETDATE()) > 100
THEN 2 ELSE 1 END
,Cli.ind_pay = CASE WHEN DATEDIFF(DAY , fec_ven , GETDATE()) > 100
THEN 2 ELSE 1 END
from cxc_cuedoc cue
inner join cxc_cliente cli on cli.cod_cli = cue.cod_cli
and cli.num_doc = cue.num_doc
where sal_doc != 0
and cue.tip_doc = '010'
Or if you are only interested in the last row, maybe something like this...
WITH X AS (
SELECT ind_mo , ind_pay , fec_ven
, ROW_NUMBER() OVER (PARTITION BY cli.cod_cli ORDER BY fec_ven DESC) rn
FROM cxc_cuedoc cue
inner join cxc_cliente cli on cli.cod_cli = cue.cod_cli
and cli.num_doc = cue.num_doc
where sal_doc !=0
and cue.tip_doc = '010'
)
UPDATE x
SET x.ind_mo = CASE WHEN DATEDIFF(DAY , x.fec_ven , GETDATE()) > 100
THEN 2 ELSE 1 END
,x.ind_pay = CASE WHEN DATEDIFF(DAY , x.fec_ven , GETDATE()) > 100
THEN 2 ELSE 1 END
WHERE x.rn = 1

how to check if password expiry column in a store procedure has only 14,7,3,2,1 days left?

i have an sp that i check if user's password change date has 14 days
left(flag=0) and i send email to that users and i that sp i check(by a
col.flag) if email already sent or not.after that i update the the
flag col.in an another SP(ExpiryNotificationFlag) to 1 of users list whose email has
already sent..now i don't know how to check for 7,3,2,1 days as flag
col value is already 1 ?
sp
SELECT TenantName
, TenantEmail
, GMAP_code
, ContactID FROM (
SELECT b_1.TenantName
, b_1.TenantEmail
, LastPDWChangeDate =(SELECT ISNULL(max (DateChanged),GETDATE()) FROM dbo.wsm_Contact_PwdHistory WHERE ContactID = b_1.TenantRegContactID)
, ExpiryNotificationFlag =(SELECT top 1 ExpiryNotificationFlag FROM dbo.wsm_Contact_PwdHistory WHERE ContactID = b_1.TenantRegContactID order by DateChanged desc)
, GMAP_code =(SELECT TOP 1 ISNULL(GMAP_CODE,'') from wsm_Ref_Buildings where BuildingId = b_1.BuildingID)
, b_1.TenantRegContactID as ContactID
FROM dbo.wsm_Contact AS a LEFT OUTER JOIN
(
SELECT C.Name AS TenantName
, A.SiteID
, A.BuildingID
, A.FloorID
, A.ContactID AS TenantRegContactID
, D.LEASID
, C.Phone AS TenantPhone
, C.Email AS TenantEmail
, B.UserID AS userid
, C.Mobile AS TenantMobile
, B.UserType
FROM dbo.wsm_Contact_Tenancy AS A
INNER JOIN dbo.wsm_Contact_User AS B ON A.ContactID = B.ContactID AND B.Active = 1
INNER JOIN dbo.wsm_Contact AS C ON B.ContactID = C.ContactID
INNER JOIN (
SELECT DISTINCT COALESCE (LEASID, ExtLeasNum) AS LEASID
, SiteID
, BuildingID
, FloorID
FROM dbo.wsm_Contact) AS D ON A.SiteID = D.SiteID AND A.BuildingID = D.BuildingID AND A.FloorID = D.FloorID) AS b_1 ON
COALESCE (a.LEASID, a.ExtLeasNum) = b_1.LEASID AND a.SiteID = b_1.SiteID AND a.BuildingID = b_1.BuildingID AND a.FloorID = b_1.FloorID
INNER JOIN dbo.wsm_Ref_Floors AS C ON a.FloorID = C.FloorId
WHERE (a.OCCPSTAT NOT IN ('I', 'P')) and C.Deleted = 0 and b_1.userid is not null ) AS A WHERE DATEDIFF(DAY,A.LastPDWChangeDate,getdate()) = 76 AND ISNULL(ExpiryNotificationFlag,0) <> 1
END
update Query
UPDATE dbo.wsm_Contact_PwdHistory set ExpiryNotificationFlag = 1 WHERE ContactID = #ContactID
you can do it like that
( (DATEDIFF(DAY,A.LastPDWChangeDate,getdate()) = 76 AND ISNULL(ExpiryNotificationFlag,0) = 0) --14 days
or (DATEDIFF(DAY,A.LastPDWChangeDate,getdate()) = 83 AND ISNULL(ExpiryNotificationFlag,0) = 1) --7days
or (DATEDIFF(DAY,A.LastPDWChangeDate,getdate()) = 87 AND ISNULL(ExpiryNotificationFlag,0) = 2) --3 days
or (DATEDIFF(DAY,A.LastPDWChangeDate,getdate()) = 88 AND ISNULL(ExpiryNotificationFlag,0) = 3) --2 days
or (DATEDIFF(DAY,A.LastPDWChangeDate,getdate()) = 89 AND ISNULL(ExpiryNotificationFlag,0) = 4) --1 day
)
for that u have to modify Expiry notification flag to int instead of a Boolean
after that make a SP where will u update the value from
0 to 1,1 to 2,2 to 3 etc.
Youre essentially making the following complaint:
"A boolean datatype can only store one of two values!"
Indeed, so swap out that flag for a "Number of Days Left" column, and compare it to 14, 7, whatever....
Note, ExpiryNotificationFlag column isn't a great name - how about "PasswordExpiryEmailSent"
Sometimes these problems get a lot easier to think about when the columns have better names

Join data from two tables

In my project, the customers of the company are calling to make requests about their equipment. Everytime a call is made, it's saved in database. Now I need to know how many calls did the company get every month of a specific year so I've written that request :
SELECT COUNT(EveLngId) FROM T_Evenement
where EveLngDosId = 1062
And EveLngBibId = 268
And EveTinSite = 1
And EveLngEleId <> 17432
And (EveTinParEmail is null OR EveTinParEmail = 0)
And (EveLngTicketTransfertId IS NULL OR EveLngTicketTransfertId = 0)
And (EveTinIncidentAnnulation is null or EveTinIncidentAnnulation=0)
And YEAR(EveDatRedaction) = 2013
group by MONTH(EveDatRedaction)
The request works but the thing is that when the value for a month is null, i doesn't display Null or 0 but it skips it. To manage that, I create a Calendar table as below:
So now I need to join these two tables to have the value for each month but I can figure out how to do that. Any advise or solution ?
;WITH ResultsCTE AS
(
SELECT MONTH(EveDatRedaction) AS ResultMonth,
COUNT(EveLngId) AS ResultCount
FROM T_Evenement
where EveLngDosId = 1062
And EveLngBibId = 268
And EveTinSite = 1
And EveLngEleId <> 17432
And (EveTinParEmail is null OR EveTinParEmail = 0)
And (EveLngTicketTransfertId IS NULL OR EveLngTicketTransfertId = 0)
And (EveTinIncidentAnnulation is null or EveTinIncidentAnnulation=0)
And YEAR(EveDatRedaction) = 2013
GROUP BY MONTH(EveDatRedaction)
)
SELECT CalendarStrLibelle,ISNULL(ResultCount,0) AS ResultCount
FROM Calendar
LEFT JOIN ResultsCTE
ON ResultMonth = CalendarLngId
Try this one -
SELECT CalendarStrLibelle
, ResultCount = ISNULL(cnt, 0)
FROM dbo.Calendar c
LEFT JOIN (
SELECT
[month] = MONTH(EveDatRedaction)
, cnt = COUNT(EveLngId)
FROM dbo.T_Evenement
WHERE EveLngDosId = 1062
AND EveLngBibId = 268
AND EveTinSite = 1
AND EveLngEleId <> 17432
AND ISNULL(EveTinParEmail, 0) = 0
AND ISNULL(EveLngTicketTransfertId, 0) = 0
AND ISNULL(EveTinIncidentAnnulation, 0) = 0
AND YEAR(EveDatRedaction) = 2013
GROUP BY MONTH(EveDatRedaction)
) t ON t.[month] = c.CalendarLngId

Update with inner join to same table optimizations

The query is:
CREATE TABLE logfile (
sequence_number BIGINT,
start_time DECIMAL,
finish_time DECIMAL,
irp_major VARCHAR(100),
offset BIGINT,
length BIGINT,
filename VARCHAR(2000),
is_seq BIT
);
//bulk insert 120 000 rows
CREATE CLUSTERED INDEX IX_logfile ON logfile(sequence_number);
UPDATE dbo.logfile SET is_seq = (
SELECT CASE WHEN a.offset = (b.offset + b.length) THEN 1 ELSE 0 END AS seq
FROM dbo.logfile a
LEFT JOIN dbo.logfile b
ON a.sequence_number = b.sequence_number + 1
WHERE a.sequence_number = logfile.sequence_number)
Is there anyway I can improve the performance of the update query?
MERGE
INTO logfile l
USING (
SELECT sequence_number + 1 AS pseq,
p.*
FROM logfile p
) pl
ON l.sequence_number = pseq
WHEN MATCHED THEN
UPDATE
SET is_seq = CASE WHEN l.offset = (pl.offset + pl.length) THEN 1 ELSE 0 END
Perhaps this update would be faster
UPDATE dbo.LogFile SET is_seq = 0
UPDATE dbo.LogFile SET is_seq = 1
FROM dbo.LogFile f
INNER JOIN (
SELECT f2.sequence_number
FROM dbo.LogFile f1
INNER JOIN dbo.LogFile f2 ON f2.sequence_number = f1.sequence_number + 1
WHERE f1.Offset <> f2.Offset + f2.Length
) f2 ON f2.sequence_number = f.sequence_number

Resources