Update using CTE - Syntax error? - sql

I'm trying to update a table using data contained in a CTE. Unfortunately I'm receiving a syntax error and I'm not quite sure why. The code currently is:
declare #period_id integer =
(
select period_id
from property.period
where getdate() between period_start and period_end
)
;with cte_reclassified as
(
select building_id ,
lease_id ,
scca_broad_category_code ,
scca_fine_categories_code ,
scca_notes_code ,
scca_sales_group_code ,
scca_uplift
from property.lease_period
where period_id = #period_id
)
update property.lease_period lp
from cte_reclassified r
set lp.scca_broad_category_code = r.scca_broad_category_code
where lp.lease_id = r.lease_id
and lp.building_id = r.building_id
The syntax error I'm receiving is:
Msg 102, Level 15, State 1, Line 21 Incorrect syntax near 'lp'.
Is there a way to do what i'm trying to attempt here? I've tried googling the subject but hit dead ends - any advice would be appreciated!

I think you want to take the "property" out of the UPDATE part of the statement (since you are updating through the CTE) and put the SET clause before the FROM:
update lease_period lp
set lp.scca_broad_category_code = r.scca_broad_category_code
from cte_reclassified r
where lp.lease_id = r.lease_id

You do not need to create alias on your update statement
On its syntax : Update [TableName] SET [ColumnName]='New Value' WHERE ColumnName='Filter'
have a look on this SO post on how it is done by #Robin Day:
SQL Server UPDATE from SELECT
Best Regards

Related

Select top n after data operations SQL

I want to
Filter a data set
Take distinct of a column
take out top 10 rows of that data set with the distinct column
The code I am using is
SELECT TOP (10) *
FROM (
SELECT DISTINCT(business_id) FROM businessdata
WHERE businessdata.city = 'Phoenix'
)
;
and the error I am getting is
Msg 102, Level 15, State 1, Line 7
Incorrect syntax near ';'.
Where am I going wrong?
You have to give a name to the subquery:
SELECT TOP (10) *
FROM (
SELECT DISTINCT(business_id) FROM businessdata
WHERE businessdata.city = 'Phoenix'
) AS my_subquery
ORDER BY businessdata
;
make sure you also an ORDER BY to correctly set the order and thus make TOP meaningful

sql server error msg 116, how to bypass

I have a problem with a subquery in my query.
In this query :
SELECT *
FROM Statistic_RecordedConversations A
JOIN (SELECT DID, max(DateTime) MaDate
FROM Statistic_RecordedConversations
where DID IN (
Select OpenData as DID, QuestionID, InterviewID
from Surveys.dbo.Askia2363Data
where QuestionID = 895
and InterviewID in (
select Surveys.dbo.Askia2363Data.InterviewID
from Surveys.dbo.Askia2363Interview,
Surveys.dbo.Askia2363Data
where Surveys.dbo.askia2363Interview.InterviewID =
Surveys.dbo.Askia2363Data.InterviewID
and completed = 1
and QuestionID = 891
and ClosedData = 4685
and CAST(EndInterview as Date) =
CAST (Current_TIMESTAMP as Date)))
GROUP BY DID ) B
ON A.DID = B.DID
AND A.DateTime = B.MaDate
I got an error with msg 116. I think its due to the ligne 6 where I have not only 1 column. I don't know how to get this query working. I tried EXISTS insteand of IN but got too many results and not the results that are interesting to me.
If someone got another solution, many thanks! :)
You can only return one field to correlate inside of an IN statement. You need to only return DID.
where DID IN ( Select OpenData as DID from Surveys.dbo.Askia2363Data...

Invalid SQL Syntax CLI0118E

I am trying to execute the below query and getting Invalid SQL Syntax error
. [IBM][CLI Driver] CLI0118E Invalid SQL syntax. SQLSTATE=37000(37000,-99999). Is it anything to do with driver upgrade? It was working fine a while ago. Please advice. Thanks in advance.
select a.name_task as nameTask, a.cd_sts as cdSts, a.desc_err_msg as
statusDesc
from task_log a,(
select id_bus_procss, name_task , id_run,
max(dt_lst_updt) as dt_lst_updt from task_log
where id_run = '1'
and id_bus_procss = '14'
and name_task in ({0})
and dt_lst_updt >= (
select dt_evnt_sts from
sf_evntflow_sts
where id_run = '1' and
id_evntflow ='15'
and cd_evnt_sts in (''CLN'',''RTY'' )
)
group by id_bus_procss, name_task, id_run)
X
where a.dt_lst_updt = X.dt_lst_updt
if you are executing this on stored procedure , Try this may help, Remove the new line character and separate the parameters by
spaces .
Or otherwise try upgrade.......
if you try this query its better?
select a.name_task as nameTask, a.cd_sts as cdSts, a.desc_err_msg as statusDesc
from task_log a,
( select b.id_bus_procss, b.name_task , b.id_run, max(b.dt_lst_updt) as dt_lst_updt
from task_log b inner join sf_evntflow_sts c on b.id_run=c.id_run and c.id_evntflow ='15'
and c.cd_evnt_sts in (''CLN'',''RTY'' ) and b.dt_lst_updt >=c.dt_evnt_sts
where b.id_run = '1' and b.id_bus_procss = '14' and b.name_task in ({0})
group by b.id_bus_procss, b.name_task, b.id_run
) as X
where a.dt_lst_updt = X.dt_lst_updt

Incorrect Syntax Merge Join

Quite confused, SQL server 2012 identifies a syntax issue withing the MERGE subquery. However, when I run the subquery stand alone it works without any issues.
Have been staring at this for a while and need some fresh eyes.
Msg 102, Level 15, State 1, Line 14
Incorrect syntax near 'CM'.
Msg 102, Level 15, State 1, Line 25
Incorrect syntax near 'CS'.
USE [UDARepDBArchive]
INSERT INTO ZZZ_SCD
SELECT [Id]
,[ZZZ]
,[MMMMM]
,[VVVVVVV]
,Eff_Date
,End_Date = CONVERT(DATETIME2,END_DATE)
,Current_Flag
FROM
(MERGE ZZZ_SCD CM
USING (SELECT [ID] = GA.ACCOUNTID
,ZZZ = CASE
WHEN isnull(g.MMMMM,'0') = '0' OR ISNULL(g.VVVVVVV,'0') = '0'
THEN '0'
ELSE g.MMMMM /g.VVVVVVV
END
,MMMMM = G.MMMMM
,VVVVVVV = G.VVVVVVV
FROM UDAReporting.rbd.STRGroup G
LEFT OUTER JOIN UDAReporting.rbd.STRGroupAccount GA ON GA.GROUPID = G.GROUPID
) CS ON CM.ID = CS.ID
WHEN NOT MATCHED THEN
INSERT VALUES (cs.[Id]
,cs.[ZZZ]
,cs.[MMMMM]
,cs.[VVVVVVV]
,CONVERT(DATETIME2,GETDATE())
,'2199-10-01 00:00:00.000'
,'Y' )
WHEN MATCHED AND CM.Current_Flag = 'Y' AND (CM.ZZZ <> CS.ZZZ ) THEN
UPDATE SET CM.Current_Flag = 'N', CM.End_date = CONVERT(DATETIME2,GETDATE())
OUTPUT $Action Action_Out, cs.[Id]
,cs.[ZZZ]
,cs.[MMMMM]
,cs.[VVVVVVV]
,CONVERT(DATETIME2,GETDATE()) AS Eff_date
,'2199-10-01 00:00:00.000' End_Date
,'Y' Current_Flag
) AS MERGE_OUT
WHERE MERGE_OUT.Action_Out = 'UPDATE'
Appears the issue was caused by "SET COMPATIBILITY_LEVEL = 90" on the exsisting Databases, rather than upgrading an dealing with potential issues, I'll just be creating a new Database

HQL syntax problem

I have a problem with the following HQL query:
select sum(MYTABLE.COUNTER) from (
select count(DISTINCT bi.products.id) as COUNTER
from BusinessInformation as bi
where bi.informationOwners.id in (100)
and bi.products.id in (10)
and bi.valueAmount not in ('NA')
and ((bi.valueType = 'ACHIEVED' and bi.referenceYears.id = 1) or (bi.valueType = 'FINAL_BALANCE' and bi.referenceYears.id = 2))
group by bi.informationOwners.id
) MYTABLE
The compiler reports:
org.hibernate.hql.ast.QuerySyntaxException: unexpected token: ( near line 1, column 34
Do you have any idea what's wrong with the query? I tested the inner query and it works fine.
Thanks,
C
HQL subqueries can occur only in the select or where clauses, not FROM.