Update statement not compiling using alias - sql

This is my update statement which is not being compiled:
Update Documents d
Set
DocumentDate = 1,
LastStatusChangedDateTime = (
Select Top 1 DATEADD(ss,1,StatusChangedDateTime)
From [dbo].[DocumentStatusesHistory] dsh
Where dsh.DocumentID = d.DocumentID
)
Where DocumentID In
(
Select Data From dbo.Split(#DocumentIDs,',')
)
I am unable to use d as an alias. How can I fix this?

How about this?
Update Documents
Set
DocumentDate = 1,
LastStatusChangedDateTime = (
Select Top 1 DATEADD(ss,1,StatusChangedDateTime)
From [dbo].[DocumentStatusesHistory] dsh
Where dsh.DocumentID = d.DocumentID
)
From Documents d
Where DocumentID In
(
Select Data From dbo.Split(#DocumentIDs,',')
)

Add FROM clause:
Update d
Set
DocumentDate = 1,
LastStatusChangedDateTime = (
Select Top 1 DATEADD(ss,1,StatusChangedDateTime)
From [dbo].[DocumentStatusesHistory] dsh
Where dsh.DocumentID = d.DocumentID
)
FROM Documents AS d
...

Try this one :
UPDATE d
SET d.DocumentDate = 1 ,
d.LastStatusChangedDateTime = ( SELECT TOP 1
DATEADD(ss, 1,
StatusChangedDateTime)
FROM [dbo].[DocumentStatusesHistory] dsh
WHERE dsh.DocumentID = d.DocumentID
)
FROM Documents d
WHERE d.DocumentID IN ( SELECT Data
FROM dbo.Split(#DocumentIDs, ',') )

Related

what is occurring in the SQL statement below and why it may be needed

WITH CTEDOC AS
(
SELECT
ROW_NUMBER() OVER (PARTITION BY d.CaseDocumentID ORDER BY PageSequence) [rowN],
d.DocumentID
FROM tblCaseDocument cd WITH(NOLOCK)
JOIN tblDocument d WITH(NOLOCK) ON d.CaseDocumentID = cd.CaseDocumentID
WHERE NOT EXISTS
(
SELECT * FROM tblDocument (NOLOCK)
WHERE PageSequence = 1
and SourceTableID = cd.CaseDocumentID
)
AND cd.DocumentCount > 0
)
UPDATE d SET PageSequence = cte.rowN
FROM CTEDOC CTE
JOIN tblDocument d (NOLOCK) ON d.DocumentID = cte.DocumentID
uodate Occured for both files?

How can I optimize this SQL code further to run faster

UPDATE N SET [actType] = 'X'
FROM tableA N
WHERE NOT EXISTS (SELECT 1
FROM tableA O
WHERE O.clientCode = N.clientCode AND
O.[userName] = N.[userName] AND
O.[profile] = N.[profile] AND
O.[rankID] = N.[rankID] - 1
) AND
N.[rankID] NOT IN (SELECT MIN(T.[rankID])
FROM tableA T
WHERE T.[userName] = N.[userName]
)
You can use row_number() with updatable cte :
with cte as (
select N.*, ROW_NUMBER() OVER (PARTITION BY [userName] ORDER BY rankID) AS Seq
from tableA N
)
update c
set c.[actType] = 'X'
from cte c
where seq > 1 and
not exists (SELECT 1
FROM tableA O
WHERE O.clientCode = c.clientCode AND
O.[userName] = c.[userName] AND
O.[profile] = c.[profile] AND
O.[rankID] = c.[rankID] - 1
);
Your syntax suggests SQL Server. So, you can use.

Get records based on latest date

In the below query I need to take only one record which is the latest phDate.
WITH TEMP_PAT_PIP_AddedBy
AS (
SELECT S.ppId
,(
SELECT PersonID
FROM [PFMADestination].[dbo].PERSON
WHERE SourceType = 'user_Users'
AND SourceId = S.uuId
) AS AddedByToStore
,phDate
FROM [PFMADestination].[dbo].PatientPIP D
INNER JOIN [PFMAOldProd].[dbo].pat_History S ON S.ppId = D.PatientId
AND phEvent = 'PIP-info uppdaterad'
)
--select * from TEMP_PAT_PIP_AddedBy order by ppId, phDate desc
UPDATE P
SET AddedBy = ISNULL(AddedByToStore, 1), Date=phDate
FROM TEMP_PAT_PIP_AddedBy T
INNER JOIN [PFMADestination].[dbo].[PatientPIP] P ON T.ppId = P.PatientId
Expected Output for CTE Take only latest phDate for each ppId and ignore other if its more than 1 record then update it.
Using partition by clause you can get expected output. Try following query :
WITH TEMP_PAT_PIP_AddedBy
AS (
SELECT S.ppId
,(
SELECT PersonID
FROM [PFMADestination].[dbo].PERSON
WHERE SourceType = 'user_Users'
AND SourceId = S.uuId
) AS AddedByToStore
,phDate , ROW_NUMBER() OVER(PARTITION BY S.ppId ORDER BY phDate DESC) AS NUM
FROM [PFMADestination].[dbo].PatientPIP D
INNER JOIN [PFMAOldProd].[dbo].pat_History S ON S.ppId = D.PatientId
AND phEvent = 'PIP-info uppdaterad'
)
UPDATE P
SET AddedBy = ISNULL(AddedByToStore, 1), Date=phDate
FROM TEMP_PAT_PIP_AddedBy T
INNER JOIN [PFMADestination].[dbo].[PatientPIP] P
ON T.ppId = P.PatientId AND T.NUM=1
Add phDate = max phDate condition to the where clause:
WITH TEMP_PAT_PIP_AddedBy
AS (
SELECT S.ppId
,(
SELECT PersonID
FROM [PFMADestination].[dbo].PERSON
WHERE SourceType = 'user_Users'
AND SourceId = S.uuId
) AS AddedByToStore
,phDate
FROM [PFMADestination].[dbo].PatientPIP D
INNER JOIN [PFMAOldProd].[dbo].pat_History S ON S.ppId = D.PatientId
AND phEvent = 'PIP-info uppdaterad'
)
SELECT *
FROM TEMP_PAT_PIP_AddedBy
where phDate = (select max(phDate) from TEMP_PAT_PIP_AddedBy
where ppId = TEMP_PAT_PIP_AddedBy.ppId)
ORDER BY ppId
,phDate DESC
Edited version, is phDate a P column? (If not, edit my answer.)
WITH TEMP_PAT_PIP_AddedBy
AS (
SELECT S.ppId
,(
SELECT PersonID
FROM [PFMADestination].[dbo].PERSON
WHERE SourceType = 'user_Users'
AND SourceId = S.uuId
) AS AddedByToStore
,phDate
FROM [PFMADestination].[dbo].PatientPIP D
INNER JOIN [PFMAOldProd].[dbo].pat_History S ON S.ppId = D.PatientId
AND phEvent = 'PIP-info uppdaterad'
AND phDate = (select max(phDate) from P
where ppId = P.ppId)
)
--select * from TEMP_PAT_PIP_AddedBy order by ppId, phDate desc
UPDATE P
SET AddedBy = ISNULL(AddedByToStore, 1), Date=phDate
FROM TEMP_PAT_PIP_AddedBy T
INNER JOIN [PFMADestination].[dbo].[PatientPIP] P ON T.ppId = P.PatientId
Svensk?

SQL Select Into #Temp

I have a complex SQL query that works. It's like
SELECT * FROM Site s
JOIN (
SELECT DISTINCT z.Value FROM Doc z
JOIN (
SELECT x.DocumentID FROM Doc x
JOIN (
SELECT DocumentID, MAX(VERSION) AS VERSION
FROM Doc GROUP BY DocumentID) y ON y.DocumentID = x.DocumentID
WHERE DocumentTypeID = 78 AND MetadataTypeID = 22 AND VALUE > GETDATE() AND y.Version = x.Version
) a ON z.DocumentID = a.DocumentID
WHERE MetadataTypeID = 2
) b ON b.Value = s.SiteID
WHERE SiteID > 0
It does what I want it to do. But when I wrap it in
SELECT * INTO #Temp FROM ()
I get an incorrect syntax near ')'.
I'm confused. I just want to put the results into a temp table so I can do further work on it. Why doesn't it work?
You're missing an alias for subquery.
SELECT * INTO #MyTempTable
FROM
(
SELECT * FROM Site s
JOIN (
SELECT DISTINCT z.Value FROM Doc z
JOIN (
SELECT x.DocumentID FROM Doc x
JOIN (
SELECT DocumentID, MAX(VERSION) AS VERSION
FROM Doc GROUP BY DocumentID) y ON y.DocumentID = x.DocumentID
WHERE DocumentTypeID = 78 AND MetadataTypeID = 22 AND VALUE > GETDATE() AND y.Version = x.Version
) a ON z.DocumentID = a.DocumentID
WHERE MetadataTypeID = 2
) b ON b.Value = s.SiteID
WHERE SiteID > 0
) AS DT
select * into TBL_name from
( SELECT * FROM Site s
JOIN (
SELECT DISTINCT z.Value FROM Doc z
JOIN (
SELECT x.DocumentID FROM Doc x
JOIN (
SELECT DocumentID, MAX(VERSION) AS VERSION
FROM Doc GROUP BY DocumentID) y ON y.DocumentID = x.DocumentID
WHERE DocumentTypeID = 78 AND MetadataTypeID = 22 AND VALUE > GETDATE() AND y.Version = x.Version
) a ON z.DocumentID = a.DocumentID
WHERE MetadataTypeID = 2
) b ON b.Value = s.SiteID
WHERE SiteID > 0)al

Call one CTE in another CTE

How could I call a CTE in another CTE ?
WITH cte1
AS (
SELECT City.*
FROM City
WHERE (City.CityName COLLATE SQL_Latin1_General_CP1_CI_AI) LIKE 'são paulo'
)
, cte2
AS (
SELECT Imovel.Imovel_Id
FROM Imovel
WHERE Imovel.Number = 311
AND Imovel.ZIPCode = '30280490'
AND Imovel.Complement = ''
AND Imovel.Street = 'Do furquim'
-- the line below has an error in cte.City_Id
AND Imovel.City_Id = cte1.City_Id
)
You have to join both like with a normal table:
WITH cte1
AS (SELECT city.*
FROM city
WHERE ( city.cityname COLLATE sql_latin1_general_cp1_ci_ai ) LIKE
'são paulo'),
cte2
AS (SELECT imovel.imovel_id
FROM imovel
INNER JOIN cte1
ON imovel.city_id = cte1.city_id
WHERE imovel.number = 311
AND imovel.zipcode = '30280490'
AND imovel.complement = ''
AND imovel.street = 'Do furquim')
SELECT * FROM cte2
Note that i have appended SELECT * FROM cte2 since CTE's cannot "stand" alone.