I have a SQL Server trigger that I have tried to convert for Firebird but seems like I have some trouble with it.
Here is my SQL trigger:
CREATE TRIGGER [dbo].[IDSOFT_INTERV_UI]
ON [MMAINT_SOCO_PROD].[dbo].[T_ZONES_LIBRES_UI]
AFTER UPDATE
AS
BEGIN
IF UPDATE(STR_ZONE_LIBRE)
BEGIN
DECLARE #ui_id int,
#num_interv int;
SELECT #ui_id = d.LIEN_UI,
#num_interv = e.NUM_INTERV
FROM T_LIGNE_BT a
inner join T_BT b on b.NUM_BT = a.CLE_BT
inner join T_UI c on c.NUM_UI = b.CLE_UI
inner join T_ZONES_LIBRES_UI d on d.LIEN_UI = c.NUM_UI
inner join T_INTERV e on e.NOM_INTERV = d.STR_ZONE_LIBRE
WHERE d.LIEN_UI in (select LIEN_UI from INSERTED) and d.INDICE_ZONE_LIBRE = 20;
Update a
set a.cle_element = e.NUM_INTERV
from t_ligne_bt a
inner join T_BT b on b.NUM_BT = a.CLE_BT
inner join T_UI c on NUM_UI = b.CLE_UI
inner join T_ZONES_LIBRES_UI d on d.LIEN_UI = c.NUM_UI
inner join T_INTERV e on e.NOM_INTERV = d.STR_ZONE_LIBRE
where d.LIEN_UI = #ui_id
and b.cle_ip is not null
and b.cle_ip <> 100000
and a.type_ligne_bt like 'I'
END
END
I'm really having problems to replicate the same for Firebird.
Can anyone help me please.
Related
I've got a problem with the syntax of a SQL query. It works sometimes and sometimes not. It's defenitly a problem with the query.
An error at or near "INTERSECTSELECT".
SQL Error: 0, SQLState: 42601
SELECT DISTINCT s.id,
s.title,
s.question,
s.comments,
s.start_date,
s.end_date,
s.motivation,
s.goals,
s.method,
s.type,
s.channel,
s.rhythm,
s.sample
FROM study s
LEFT OUTER JOIN study_customer_information sci
ON sci.study_id = s.id
LEFT OUTER JOIN customer_information ci
ON ci.id = sci.customer_information_id
WHERE s.is_active = :isActive
AND
s.language = :language AND s.id IN (SELECT sci.study_id
FROM study_customer_information sci
INNER JOIN customer_information ci
ON ci.id = sci.customer_information_id
WHERE ci.name_de in (:customer_informations)
GROUP BY sci.study_id
HAVING COUNT(sci.study_id) = :number_customer_informations INTERSECT SELECT st.study_id
FROM study_touchpoint st
INNER JOIN touchpoint tp ON tp.id = st.touchpoint_id
INNER JOIN subject_area sa ON sa.id = tp.subject_area_id
WHERE sa.name_DE = :subject_areas_0 INTERSECT SELECT st.study_id
FROM study_touchpoint st
INNER JOIN touchpoint tp ON tp.id = st.touchpoint_id
INNER JOIN touch_point_type tpt ON tpt.id = tp.touch_point_type_id
WHERE tpt.name_DE = :touchpoint_types_0 INTERSECT SELECT sth.study_id
FROM study_theme sth
INNER JOIN theme th
ON th.id = sth.theme_id
WHERE th.name_de in (:themes)
GROUP BY sth.study_id
HAVING COUNT(sth.study_id) = :number_themes) ORDER BY s.title
In my below T-SQL Query I need to use EFP_MessageCenter.MessageSender from my main SELECT in the SELECT in my LEFT OUTER JOIN as the value where I have placed <MessageSenderInitials>.
When I set (EFP_MessageCenter_1.MessageSender = EFP_MessageCenter.MessageSender) or (EFP_MessageCenter_1.MessageSender = MessageSenderInitials) I get the error The multi-part identifier "EFP_MessageCenter.MessageSender" could not be bound.
How can I get this to work?
SELECT LOWER(EFP_MessageCenter.MessageSender) AS MessageSenderInitials
, MAX(SenderInfo.FullName) AS SenderFullName
, MAX(SenderInfo.ProfilePicture) AS SenderProfilePicture
, MAX(EFP_MessageCenter_Receiver.UserID) AS ReceiverID
, MAX(EFP_MessageCenter.MessageTimestamp) AS ChangeDate
, COUNT(DisplayCountSelect.Displayed) AS CountNonReadMessages
FROM EFP_MessageCenter_Receiver
INNER JOIN EFP_MessageCenter ON EFP_MessageCenter_Receiver.MessageID = EFP_MessageCenter.id
INNER JOIN EFP_EmploymentUser AS SenderInfo ON EFP_MessageCenter.MessageSender = SenderInfo.Initials
LEFT OUTER JOIN
(SELECT EFP_MessageCenter_Receiver_1.Displayed, EFP_MessageCenter_Receiver_1.UserID, EFP_MessageCenter_1.MessageSender
FROM EFP_MessageCenter AS EFP_MessageCenter_1
INNER JOIN EFP_MessageCenter_Receiver AS EFP_MessageCenter_Receiver_1 ON EFP_MessageCenter_1.id = EFP_MessageCenter_Receiver_1.MessageID
WHERE (EFP_MessageCenter_Receiver_1.Displayed = 0) AND (EFP_MessageCenter_Receiver_1.UserID = 65) AND (EFP_MessageCenter_1.MessageSender = '<MessageSenderInitials>'))
AS DisplayCountSelect
ON DisplayCountSelect.UserID = EFP_MessageCenter_Receiver.UserID
WHERE (EFP_MessageCenter_Receiver.UserID = 65) AND (EFP_MessageCenter.MessageType = 'SPECIFIC')
GROUP BY EFP_MessageCenter.MessageSender
ORDER BY ChangeDate DESC
I've made a slight refactor of your query and changed the outer join to an an outer apply
It's not going to be 100% working I'm sure but should allow you to tweak it and include the correlation you need to.
I suspect you could move the CountNonReadMessages to a count(*) in the apply and possibly remove the aggregation, but that's just a guess.
select Lower(mc.MessageSender) as MessageSenderInitials
, Max(s.FullName) as SenderFullName
, Max(s.ProfilePicture) as SenderProfilePicture
, Max(mr.UserID) as ReceiverID
, Max(mc.MessageTimestamp) as ChangeDate
, Count(s.Displayed) as CountNonReadMessages
from EFP_MessageCenter_Receiver mr
join EFP_MessageCenter mc on mr.MessageID = mc.id
join EFP_EmploymentUser eu on mc.MessageSender = eu.Initials
outer apply (
select mr.Displayed
from EFP_MessageCenter mcx
join EFP_MessageCenter_Receiver mrx on mcx.id = mrx.MessageID
where mrx.Displayed = 0
and mrx.UserId=mr.UserId
and mcx.UserID = 65 /* this should probably be correlated */
and mcx.MessageSender = '<MessageSenderInitials>'
) s
where mr.UserID = 65 and mc.MessageType = 'SPECIFIC'
group by mc.MessageSender
order by ChangeDate desc
In MySQL it's possible to do something like this:
update
table_a A
inner join
table_b B
on
A.field_five = B.field_five
inner join
table_c C
on
B.field_one = C.field_one and A.field_two = C.field_two
set A.field_three = C.field_four
I have tried to construct the same query in PostgreSQL like this:
update table_a A
set A.field_three = C.field_four
from table_b B
inner join table_c C
on
B.agency_id = C.agency_id and A.field_two = C.field_two
where
A.field_five = B.field_five
I get the following error:
ERROR: invalid reference to FROM-clause entry for table "a"
I'm using PostgreSQL 11. What is the correct way to do this query in postgres?
don't specify what table to update in "set" and move "A.field_two = C.field_two" to the where clause
update table_a A
set field_three = C.field_four
from table_b B
inner join table_c C
on
B.agency_id = C.agency_id
where
A.field_five = B.field_five
and A.field_two = C.field_two
https://www.db-fiddle.com/f/mipu88sd4JDar25TtvQCQJ/1
Youl could rewrite it using CTE:
WITH cte AS (
SELECT c.*, b.field_five
FROM table_b B
JOIN table_c C
ON B.agency_id = C.agency_id
)
UPDATE table_a A
SET field_three = C.field_four
FROM cte c
WHERE A.field_five = c.field_five
AND A.field_two = c.field_two;
db<>fiddle demo
I would appreciate it if someone can explain me how the piece of code below works internally. For example, how the order of conditions is evaluated.
update br set ScannedId =
(
select top 1 Id from table1 sp (nolock)
inner loop join table2 cp (nolock) ON (sp.CPId = cp.CPID)
inner loop join table3 c (nolock) ON (cp.CID = c.CId and c.endDate >= sp.CreatedDate and c.startDate <= sp.CreatedDate ) `enter code here`
inner loop join table4 cc (nolock) ON (c.CChannelID =`enter code here` cc.CChannelID)
where (sp.UserId is null or sp.UserId = br.UserId)
and ((sp.Email = br.UserEmail)
or (sp.fName like br.UFName + '%' and sp.LName like br.ULName + '%' and sp.sHash = br.uHash)
or (sp.fName like br.UFName + '%' and sp.Addrs = br.UOAddrs and sp.ZC = br.UOZ and sp.sHash = br.uHash))
order by cc.Rank, c.Rank, cp.Rank, sp.EDate desc, sp.CreatedDate desc
)
from channelnewlogic br where userId = 3637217
Rows in channelnewlogic get their ScannedId column values updated according to the result of the inner select.
I receive an error
Coversion of varchar to float
when I write
set rowcount = 1
on first line of my code.
My script is:
select
vh.VchNum, ct.Val, r.RoutSheetNo, vi.FinalQty,
rh.RequestNo, vh.VchDate,
p.PartCode, d.Title, co.val
from
inv.InvVchHdr vh
join
acc.DL d on d.AccNum = vh.DLREF
join
inv.InvVchItm vi on vi.VchHdrRef = vh.VchHdrID
join
inv.InvVchItmCtrl ct on ct.VchItmRef = vi.VchItmID
join
QCS.QcsCertificateOfAnalysis q on q.Number = ct.Val
join
USR.kalaf_info_p kp on kp.Id = q.QcsCertificateOfAnalysisId
join
USR.coil_trace co on co.id = kp.coil_id
join
inv.Part p on p.Serial = vi.PartRef
join
inv.InvRqstItm rq on rq.RqstItmID = vi.RefNum
join
inv.InvRqstHdr rh on rh.RqstHdrID = rq.HdrRef
join
PRD.vwPrdOrderItemPlan pl on rh.OrdPlnBase = pl.OrdPlnId
join
prd.prdroutsheet r on r.OrdPlnRef = pl.OrdPlnId
where
pl.pPartRef not in (select pipe_code from usr.pipe_kalaf)
and pl.pPartRef not in (select Serial from inv.Part where PartName like '%لاف%')
and vi.VchType = 57
union
select vh.VchNum,ct.Val,pl.OrdPlnNo
,vi.FinalQty,
rh.RequestNo,vh.VchDate,
p.PartCode,d.Title,co.val from
inv.InvVchHdr vh
join acc.DL d
on d.AccNum=vh.DLREF
join
inv.InvVchItm vi
on vi.VchHdrRef=vh.VchHdrID
join inv.InvVchItmCtrl ct
on ct.VchItmRef=vi.VchItmID
join QCS.QcsCertificateOfAnalysis q
on q.Number=ct.Val
join USR.kalaf_info_p kp
on kp.Id=q.QcsCertificateOfAnalysisId
join USR.coil_trace co
on co.id=kp.coil_id
join
inv.Part p
on p.Serial=vi.PartRef
join inv.InvRqstItm rq
on rq.RqstItmID=vi.RefNum
join inv.InvRqstHdr rh
on rh.RqstHdrID=rq.HdrRef
join PRD.vwPrdOrderItemPlan pl
on rh.OrdPlnBase=pl.OrdPlnId
where pl.pPartRef in (select pipe_code from usr.pipe_kalaf) and vi.VchType=57
SET ROWCOUNT 0
i use sql server 2000 and when I remove setrowcount statement, problem resolve.
please help me
Remove the =(equal) sign
from
set rowcount = 1
To
SET ROWCOUNT 1