getting error The multi-part identifier "e.EpVisitCount" could not be bound. in sql server - sql

I am working on converting mysql query to sql server, but when i run the query it gives me error The multi-part identifier "e.EpVisitCount" could not be bound. , here i have posted my both query can you please look into it, why it gives me error in sql server ?
Mysql Query(Working Fine) :
UPDATE
tb_EpVisitRange as v left JOIN tb_Episode as e ON (v.company_id = e.CustID) AND (v.CMW = e.CMW)
SET
e.EpVisitCount = If(PayerType='NonEp',0,If(LUPA=1,0,v.High)),
e.VisitAlert = If( e.TotVisits > v.High,1,NULL)
where UploadID = '23'
SQl Query(Getting Error) :
UPDATE v
SET
e.EpVisitCount = IIF(PayerType='NonEp',0,IIF(LUPA=1,0,v.High)),
e.VisitAlert = IIF( e.TotVisits > v.High,1,NULL)
FROM tb_EpVisitRange v
JOIN tb_Episode as e ON (v.company_id = e.CustID) AND (v.CMW = e.CMW)
where UploadID = '613'

You seem to want to update e rather than v. So, you might try:
UPDATE e
SET EpVisitCount = (CASE WHEN PayerType = 'NonEp' THEN 0
WHEN LUPA = 1 THEN 0
ELSE v.High
END),
VisitAlert = (CASE WHEN e.TotVisits > v.High THEN 1 END)
FROM tb_Episode e JOIN
tb_EpVisitRang v
ON v.company_id = e.CustID AND v.CMW = e.CMW
WHERE UploadID = '613';
Notes:
The problem appears to be the table alias used for the update.
SQL Server only allows updating one table in a statement. There is no need to qualify the column names for the SET.
Use CASE for conditions. It is the ANSI standard and supported by almost all database.
This is especially true for nested expressions. CASE supports multiple conditions.
You are updating e, so I made that the first table in the FROM clause. I find that logic easier to follow.
You should qualify UploadId.

Related

Redshift - ERROR: Target table must be part of an equijoin predicate

I am trying to make an update on a temporal table I created in Redshift. The code I am trying to run goes like this:
UPDATE #new_emp
SET rpt_to_emp_id = CAST(ht.se_value AS INTEGER),
rpt_to_extrnl_email = ht.extrnl_email_addr,
rpt_to_fst_nm = ht.first_nm,
rpt_to_lst_nm = ht.last_nm,
rpt_to_mdl_init = ht.mdl_nm,
rpt_to_nm = ht.full_nm,
rpt_to_ssn = CAST(ht.ssn AS INTEGER),
FROM #new_emp,
(SELECT DISTINCT t.se_value,h.first_nm,h.last_nm,
h.mdl_nm,h.full_nm,h.ssn,h.extrnl_email_addr
FROM spec_hr.dtbl_translate_codes_dw t, spec_hr.emp_hron h
WHERE t.inf_name = 'system'
AND t.fld_name = 'HRONDirector'
AND h.foreign_emp_id = t.se_value
) ht
WHERE #new_emp.foreign_emp_id <> ht.se_value
AND (#new_emp.emp_status_cd <> 'T'
AND (#new_emp.ult_rpt_emp_id = #new_emp.foreign_emp_id
OR #new_emp.ult_rpt_emp_id = #new_emp.psoft_id
OR #new_emp.ult_rpt_emp_id IS NULL));
I've tried both with and without specyfing the updated table from the FROM command. But it keeps throwing me this error:
ERROR: Target table must be part of an equijoin predicate
Any ideas why is this failing? Thank you!
Redshift needs an equality join condition to know what value to update and with which values. Your join condition is "#new_emp.foreign_emp_id <> ht.se_value" which is an inequality or Redshift speak - this is not "an equijoin predicate". You have a SET of "rpt_to_lst_nm = ht.last_nm" but if the only join condition is an inequality then which value of last_nm is Redshift putting in the table?
To put it the other way around - you need to tell Redshift exactly which rows of the target table are receiving which values (equijoin). The join condition you have doesn't meet this requirement.

Postgres SQL state: 22P02 - invalid input syntax for integer

I'm using a sql query to export a database from my company's program.
Everything seems to be fine till I change the date on the "where" statement with a previous one.
Please find below the code:
SELECT p."Index", p."PSN" || CAST(p."PNR"as int) AS ID,
p."PSN" AS Serie, cast(p."PNR"as int) AS Numar,
pr."PINDate" AS r_gdate,
CASE WHEN pr."AsigEID"='10' THEN pr."PrimSUM" ELSE
pr."PrimSUM"*valuta1."EXCValue" END AS r_prima_lei,
CASE WHEN pr."AsigEID"='2'
THEN pr."PrimSUM"
ELSE CASE WHEN pr."AsigEID"='10' THEN pr."PrimSUM"/valuta2."EXCValue"
ELSE pr."PrimSUM"*valuta1."EXCValue"/valuta2."EXCValue"
END
END AS r_prima_eur,
CASE WHEN pr."AsigEID"='10' THEN pr."AsigSUM" ELSE
pr."AsigSUM"*valuta1."EXCValue" END as r_sa_lei,
CASE WHEN pr."AsigEID"='2'
THEN pr."AsigSUM"
ELSE CASE WHEN pr."AsigEID"='10' THEN pr."AsigSUM"/valuta2."EXCValue"
ELSE pr."AsigSUM"*valuta1."EXCValue"/valuta2."EXCValue"
END
END AS r_sa_eur,
pr."AsigStart", pr."AsigEnd", risc."Code", plink."Index"
FROM "PolsRisc" AS pr
LEFT JOIN "Pols" as p ON p."Index" = pr."PID"
LEFT JOIN "Riscs" as risc ON pr."RID" = risc."Index"
LEFT JOIN "PRLNK" plink ON plink."PTID" = p."PTID" AND plink."RID" = risc."Index"
LEFT JOIN "EXCValues" valuta1 ON valuta1."AtDate" = pr."AsigStart" AND valuta1."EID" = pr."AsigEID"
LEFT JOIN "EXCValues" valuta2 ON valuta2."AtDate" = pr."AsigStart" AND valuta2."EID"='2'
WHERE pr."PINDate" > '2020-08-01' AND pr."IsRezil" = 'false';
When I'm using '2020-08-01' the query works well. When I try to change it to a previous one eg. '2010-01-01' a get an error:
ERROR: invalid input syntax for integer: ""
SQL state: 22P02
I was looking for a solution on the previous posts but I didn't manage to solve this issue.
It looks like it is returning "" or a null value into one of the columns you are using integer logic for. The date change is just filtering out the data that would crash it.
You may need to use coalesce to reassign the nulls as 0 and then cast it back into being an int
select
cast(coalesce(table.column, 0) as int) as result
from table
I would advice to read the chapter http://www.postgresql.org/docs/current/interactive/sql-syntax-lexical.html#SQL-SYNTAX-CONSTANTS
. It's a brief and informative read.The cause for the error message is that '' is an empty string that has no representation in a numeric type like integer

Issue With SQL Pivot Function

I have a SQL query where I am trying to replace null results with zero. My code is producing an error
[1]: ORA-00923: FROM keyword not found where expected
I am using an Oracle Database.
Select service_sub_type_descr,
nvl('Single-occupancy',0) as 'Single-occupancy',
nvl('Multi-occupancy',0) as 'Multi-occupancy'
From
(select s.service_sub_type_descr as service_sub_type_descr, ch.claim_id,nvl(ci.item_paid_amt,0) as item_paid_amt
from table_1 ch, table_" ci, table_3 s, table_4 ppd
where ch.claim_id = ci.claim_id and ci.service_type_id = s.service_type_id
and ci.service_sub_type_id = s.service_sub_type_id and ch.policy_no = ppd.policy_no)
Pivot (
count(distinct claim_id), sum(item_paid_amt) as paid_amount For service_sub_type_descr IN ('Single-occupancy', 'Multi-occupancy')
)
This expression:
nvl('Single-occupancy',0) as 'Single-occupancy',
is using an Oracle bespoke function to say: If the value of the string Single-occupancy' is not null then return the number 0.
That logic doesn't really make sense. The string value is never null. And, the return value is sometimes a string and sometimes a number. This should generate a type-conversion error, because the first value cannot be converted to a number.
I think you intend:
coalesce("Single-occupancy", 0) as "Single-occupancy",
The double quotes are used to quote identifiers, so this refers to the column called Single-occupancy.
All that said, fix your data model. Don't have identifiers that need to be quoted. You might not have control in the source data but you definitely have control within your query:
coalesce("Single-occupancy", 0) as Single_occupancy,
EDIT:
Just write the query using conditional aggregation and proper JOINs:
select s.service_sub_type_descr, ch.claim_id,
sum(case when service_sub_type_descr = 'Single-occupancy' then item_paid_amt else 0 end) as single_occupancy,
sum(case when service_sub_type_descr = 'Multi-occupancy' then item_paid_amt else 0 end) as multi_occupancy
from table_1 ch join
table_" ci
on ch.claim_id = ci.claim_id join
table_3 s
on ci.service_type_id = s.service_type_id join
table_4 ppd
on ch.policy_no = ppd.policy_no
group by s.service_sub_type_descr, ch.claim_id;
Much simpler in my opinion.
for column aliases, you have to use double quotes !
don't use
as 'Single-occupancy'
but :
as "Single-occupancy",

How can be expressed this Access Query to SQL Server query

I have a database in access but recently moved to SQL server, and I have modified almost all queries but this one :
UPDATE Articulos_Auditoria
INNER JOIN Auditoria ON Articulos_Auditoria.CUD = Auditoria.CUD
SET Articulos_Auditoria.Cortado = 'True'
WHERE
(((CAST([Fecha] AS DATE)) = CAST(#Fecha AS DATE))
AND ((Auditoria.Terminal) = #term))
I am trying to convert it to SQL Server (since I changed DateValue to a CAST) but intellisense gives me a syntax error near 'INNER'
Can anyone give me some insight?
You have two errors:
You need to define the SET after the UPDATE TableName
You need to define a FROM clause
UPDATE Articulos_Auditoria
SET Cortado = 'True'
FROM Articulos_Auditoria
INNER JOIN Auditoria
ON Articulos_Auditoria.CUD = Auditoria.CUD
WHERE CAST([Fecha] as date)=CAST(#Fecha as date)
AND Auditoria.Terminal=#term
Use the from clause and table aliases:
UPDATE aa
SET Cortado = 'True'
FROM Articulos_Auditoria aa INNER JOIN
Auditoria a
ON aa.CUD = a.CUD
WHERE CAST([Fecha] as date) = CAST(#Fecha as date) AND (a.Terminal = #term)

Gsql not executing while constructing query

I want to fire an update query , normally using groovy we do something like :
sql.executeUpdate("update MYTABLE l set field1 where l.id = ${someobj.id}")
The above works perfectly but my problem is that i dont know ahead how many parameters i need to update. So i made a function which returns the
properttoudate1 = value1 , propertytoupdate2 = value2 .. and so on..
Then i modified the above query to
sql.executeUpdate("update MYTABLE l set ${makeQueryString(propertiesToUpdate)} where l.id = ${someobj.id}")
Now it gives me the error::
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''PROPERTY1 = 1' where l.id = 'PROPERTVALUE1'' at line 1
ANY IDEAS ??
You need to tell Groovy that the fieldname is a static, and shouldnt be replaced with ? by using Sql.expand:
sql.executeUpdate("update MYTABLE l set ${Sql.expand(makeQueryString(propertiesToUpdate))} where l.id = ${someobj.id}")