Subquery returned more than 1 value causing query to fail - sql-server

I'm stuck with this query. i want to show a field called "Executive" that must say "sin asignar" if there's no match when joining on the table 'prospectousuario', and if it exist it must say the first and the last name of the executive. But it returns the following error:
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an exp
The query is:
select p.IDCANCAP,
p.CEDULA,
p.NOMBRES,
p.APELLIDOS,
p.CELULAR,
p.CASA,
p.CORREO,
p.ESTABLECIMIENTO,
c.DESCRIPCION,
(select case when
p.CEDULA = pu.IDPROSPECTO and pu.IDUSUARIO = u.CEDULA
then u.NOMBRES+' '+u.APELLIDOS else 'Sin asignar'
end from usuario as u, PROSPECTOUSUARIO as pu) as Ejecutivo,
p.FECHA_CREAC
from PROSPECTO p, CANALCAPTACION c
where p.IDCANCAP = c.IDCANAL

Boy this is a guess, based on your SQL and a lot of reading between the lines... but give it a try:
select p.IDCANCAP, p.CEDULA, p.NOMBRES,
p.APELLIDOS, p.CELULAR,p.CASA, p.CORREO,
p.ESTABLECIMIENTO, c.DESCRIPCION, p.FECHA_CREAC,
case when exists
(Select * from PROSPECTOUSUARIO
Where IDPROSPECTO = p.CEDULA)
then u.NOMBRES+' '+ u.APELLIDOS
else 'Sin asignar' end Ejecutivo
from PROSPECTO p
join CANALCAPTACION c on c.IDCANAL = p.IDCANCAP
join usuario u on u.CEDULA = p.CEDULA

Just as the error message says, your one and only subquery
(select case when
p.CEDULA = pu.IDPROSPECTO and pu.IDUSUARIO = u.CEDULA
then u.NOMBRES+' '+u.APELLIDOS else 'Sin asignar'
end from usuario as u, PROSPECTOUSUARIO as pu)
returns more than 1 row, which is not allowed when used as an expression inside the SELECT. In fact, it appears that the u and pu tables are not joined against any other tables in any meaningful way.
To fix, you must join the tables. In fact, you don't even need a subquery for what you're trying to do - move all tables to the FROM section and join them properly, whether inner or outer. Unfortunately, I don't know which fields they should be joined on, so I can't help there without more information or wild guessing.

Related

Executing part of stored procedure based on condition

I am trying to load to a table based on load types - Full or Incremental that is being passed as parameter in stored procedure. I was able to try with substitution variable with one line of code previously, but the below code doesn't seem to work -
Stored procedure possible arguments:
LOAD_TYPE=FULL
LOAD_TYPE=INCR
var incr_condition = (load_type=='INCR')?"INNER JOIN temp_table"
with temp_table(
select data
from table a
where dt between 01-01-2019 and 09-09-2020
)
select *
from table b
${incr_condition} -- execute only if load_type=INCR
INNER JOIN TABLE C ON B.ID = C.ID
Is there any way to restrict the with clause to execute only if the load_type==INCR? Please advice.
I think the conditional operator (question mark) must have a false part in addition to the true part. Otherwise, it generates a syntax error when there's a semicolon ending the line. This example obviously doesn't run anything, but it will return the values assigned to the "out" variable, which would be run.
Since you're using a replacement variable ${incr_condition} be sure to use backticks to open and close your SQL string.
create or replace procedure foo(LOAD_TYP string)
returns string
language javascript
as
$$
var load_type = LOAD_TYP;
var incr_condition = (load_type === 'INCR') ? "INNER JOIN temp_table" : "";
var out = `
with temp_table(
select data
from table a
where dt between 01-01-2019 and 09-09-2020
)
select *
from table b
${incr_condition} -- execute only if load_type=INCR
INNER JOIN TABLE C ON B.ID = C.ID
`;
return out;
$$;
call foo('INCR'); --Adds the inner join
call foo('FULL'); --Does not add the inner join
I also recommend changing your comparison on strings from == to ===. For details on why, reference What is the correct way to check for string equality in JavaScript?.

SQL query gives Incorrect syntax near ')' - use of CAST in INNER JOIN

I get the error Incorrect syntax near ')' from the following SQL query:
SELECT
"GLPOST"."ACCTID",
"GLPOST"."FISCALYR",
"GLPOST"."FISCALPERD",
"GLPOST"."SRCELEDGER",
"GLPOST"."JRNLDATE",
"GLPOST"."BATCHNBR",
"GLPOST"."ENTRYNBR",
"GLPOST"."JNLDTLDESC",
"GLPOST"."JNLDTLREF",
"GLPOST"."TRANSAMT",
"APIBC"."POSTSEQNBR",
"APIBC"."CNTBTCH"
FROM ("MHLDAT"."dbo"."GLPOST" "GLPOST"
INNER JOIN "MHLDAT"."dbo"."GLJEH" "GLJEH"
ON (("GLPOST"."DRILSRCTY"="GLJEH"."DRILSRCTY")
AND ("GLPOST"."DRILLDWNLK"="GLJEH"."DRILLDWNLK")
AND "GLPOST"."DRILAPP"="GLJEH"."DRILAPP")))
INNER JOIN "MHLDAT"."dbo"."APIBC" "APIBC"
ON "APIBC"."POSTSEQNBR" = (CAST ("SUBSTRING" (CAST ("GLPOST"."DRILLDWNLK" AS "CHAR"(18)),3,CAST ("LEFT" (CAST ("GLPOST"."DRILLDWNLK" AS "CHAR"(18)),1) AS "INT" )) AS "INT" ))
WHERE
"GLPOST"."SRCELEDGER"=N'AP' AND "GLPOST"."FISCALYR"=N'2021' AND "GLPOST"."FISCALPERD"=N'01' AND "GLJEH"."ERRBATCH"=0
Any suggestions to resolve please?
SELECT
GLPOST.ACCTID,
GLPOST.FISCALYR,
GLPOST.FISCALPERD,
GLPOST.SRCELEDGER,
GLPOST.JRNLDATE,
GLPOST.BATCHNBR,
GLPOST.ENTRYNBR,
GLPOST.JNLDTLDESC,
GLPOST.JNLDTLREF,
GLPOST.TRANSAMT,
APIBC.POSTSEQNBR,
APIBC.CNTBTCH
FROM MHLDAT.dbo.GLPOST
INNER JOIN MHLDAT.dbo.GLJEH
ON GLPOST.DRILSRCTY=GLJEH.DRILSRCTY
AND GLPOST.DRILLDWNLK=GLJEH.DRILLDWNLK
AND GLPOST.DRILAPP=GLJEH.DRILAPP
INNER JOIN MHLDAT.dbo.APIBC
ON APIBC.POSTSEQNBR = CAST(SUBSTRING(CAST(GLPOST.DRILLDWNLK AS CHAR(18)),
3,
CAST(LEFT(CAST(GLPOST.DRILLDWNLK AS CHAR(18)),
1) AS INT)
) AS INT)
WHERE
GLPOST.SRCELEDGER = N'AP'
AND GLPOST.FISCALYR = N'2021'
AND GLPOST.FISCALPERD = N'01'
AND GLJEH.ERRBATCH = 0
You are using a lot of unnecessary parenthesis and double quoting, remove them (specially on the names of the functions SUBSTRING and LEFT). Remove the aliases (they are unnecessary because you use the same name as the table).
I have aligned your query so you can visually see where starts and ends every subexpression.

An expression of non-boolean type specified in a context where a condition is expected, near 'end'. error

select *
from DBNAME.dbo.Shell_V36 V
inner join
DBNAME.DBO.Promo P
on
Case
when
(V.Product = P.From_Product_Code)
And (V.cur_date >= P.Promo_end_date)
or (V.cur_date <= P.Promo_start_date)
then (P.To_Product_Code)
else(P.From_Product_Code)
end
i am getting "An expression of non-boolean type specified in a context where a condition is expected, near 'end'." can any one help me out please.
You have to provide the field from Shell_V36 table used in the ON clause of the JOIN operation:
select *
from DBNAME.dbo.Shell_V36 V
inner join DBNAME.DBO.Promo P
on Case when
(V.Product = P.From_Product_Code) And
(V.cur_date >= P.Promo_end_date)
or
(V.cur_date <= P.Promo_start_date)
then (P.To_Product_Code)
else (P.From_Product_Code)
End = V.code -- presumably code field should be used

SQL Server LEFT JOIN

This query has been keeping me busy for the last couple of days. I tried to rewrite it with different ideas but I keep having the same problem. To simplify the problem I put part of my query in a view, this view returns 23 records. Using a left join I would like to add fields coming from the table tblDatPositionsCalc to these 23 records. As you can see I have an additional condition on the tblDatPositionsCalc in order to only consider the most recent records. With this condition it would return 21 records. The join should be on two fields together colAccount and colId.
I simply want the query to return the 23 records from the view and where possible have the information from tblDatPositionsCalc. There is actually only 2 records in the view without corresponding id and account in tblDatPositionsCalc, that means out of the 23 records only 2 will have missing values in the fields coming from the table tblDatPositionsCalc.
The problem with my query is that it only returns the 21 records from tblDatPositionsCalc. I don't understand why. I tried to move the condition on date in just after the JOIN condition but that did not help.
SELECT TOP (100) PERCENT
dbo.vwCurrPos.Account,
dbo.vwCurrPos.Id,
dbo.vwCurrPos.TickerBB,
dbo.vwCurrPos.colEquityCode,
dbo.vwCurrPos.colType,
dbo.vwCurrPos.colCcy,
dbo.vwCurrPos.colRegion,
dbo.vwCurrPos.colExchange,
dbo.vwCurrPos.[Instr Type],
dbo.vwCurrPos.colMinLastDay,
dbo.vwCurrPos.colTimeShift,
dbo.vwCurrPos.Strike,
dbo.vwCurrPos.colMultiplier,
dbo.vwCurrPos.colBetaVol,
dbo.vwCurrPos.colBetaEq,
dbo.vwCurrPos.colBetaFloor,
dbo.vwCurrPos.colBetaCurv,
dbo.vwCurrPos.colUndlVol,
dbo.vwCurrPos.colUndlEq,
dbo.vwCurrPos.colUndlFut,
tblDatPositionsCalc_1.colLots,
dbo.vwCurrPos.[Open Positions],
dbo.vwCurrPos.colListMatShift,
dbo.vwCurrPos.colStartTime,
tblDatPositionsCalc_1.colPrice,
tblDatPositionsCalc_1.colMktPrice,
dbo.vwCurrPos.colProduct,
dbo.vwCurrPos.colCalendar,
CAST(dbo.vwCurrPos.colExpiry AS DATETIME) AS colExpiry,
dbo.vwCurrPos.colEndTime,
CAST(tblDatPositionsCalc_1.colDate AS datetime) AS colDate,
dbo.vwCurrPos.colFund,
dbo.vwCurrPos.colExchangeTT,
dbo.vwCurrPos.colUserTag
FROM dbo.vwCurrPos
LEFT OUTER JOIN dbo.tblDatPositionsCalc AS tblDatPositionsCalc_1
ON tblDatPositionsCalc_1.colId = dbo.vwCurrPos.Id
AND tblDatPositionsCalc_1.colAccount = dbo.vwCurrPos.Account
WHERE (tblDatPositionsCalc_1.colDate =
(SELECT MAX(colDate) AS Expr1 FROM dbo.tblDatPositionsCalc))
ORDER BY
dbo.vwCurrPos.Account,
dbo.vwCurrPos.Id,
dbo.vwCurrPos.colEquityCode,
dbo.vwCurrPos.colRegion
Any idea what might cause the problem?
(Option 1) DrCopyPaste is right so your from clause would look like:
...
FROM dbo.vwCurrPos
LEFT OUTER JOIN dbo.tblDatPositionsCalc AS tblDatPositionsCalc_1
ON tblDatPositionsCalc_1.colId = dbo.vwCurrPos.Id
AND tblDatPositionsCalc_1.colAccount = dbo.vwCurrPos.Account
and (tblDatPositionsCalc_1.colDate =
(SELECT MAX(colDate) AS Expr1 FROM dbo.tblDatPositionsCalc))
...
reason: the where clause restriction of left joined to column = some expression with fail to return for "null = something" so the row will be removed.
(Option 2) As oppose to pushing code in to additional views where it is harder to maintain you can nest sql select statements;
select
X.x1,X.x2,
Y.*
from X
left join
(select Z.z1 as y1, Z.z2 as y2, Z.z3 as y3
from Z
where Z.z1 = (select max(Z.z1) from Z)
) as Y
on x.x1 = Y.y1 and X.x2 = Y.y2
The advantage here is you check each nested sub query a move out quickly. Although if you still building up more logic check out common table expressions (CTE's) http://msdn.microsoft.com/en-us/library/ms175972.aspx

Yet another subquery issue

Hello from an absolute beginner in SQL!
I have a field I want to populate based on another table. For this I have written this query, which fails with: Msg 512, Level 16, State 1, Line 1
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
The statement has been terminated.
oK, here goes:
Update kre.CustomerOrderLineCopy
SET DepNo = (SELECT customerordercopy.DepNo
FROM kre.CustomerOrderCopy , kre.CustomerOrderLineCopy
WHERE CustomerOrderLineCopy.OrderCopyNo =kre.CustomerOrderCopy.OrderCopyNo)
WHERE CustomerOrderLineCopy.OrderCopyNo = (SELECT CustomerOrderCopy.OrderCopyNo
FROM kre.CustomerOrderCopy, kre.CustomerOrderLineCopy
WHERE kre.CustomerOrderLineCopy.OrderCopyNo = kre.CustomerOrderCopy.OrderCopyNo)
What I'm trying to do is to change DepNo in CustomerOrderLineCopy, with the value in DepNo in CustomerOrderCopy - based on the same OrderCopyNo in both tables.
I'm open for all suggestion.
Thanks,
ohalvors
If you just join the tables together the update is easier:
UPDATE A SET A.DepNo = B.DepNo
FROM kre.CustomerOrderLineCopy A
INNER JOIN kre.CustomerOrderCopy B ON A.OrderCopyNo = B.OrderCopyNo
The problem is that at least one of your sub queries return more than one value. Think about this:
tablePerson(name, age)
Adam, 11
Eva, 11
Sven 22
update tablePerson
set name = (select name from tablePerson where age = 11)
where name = 'Sven'
Which is equivalent to: set Sven's name to Adam and Eva. Which is not possible.
If you want to use sub queries, either make sure your sub queries can only return one value or force one value by using:
select top 1 xxx from ...
This may be enough to quieten it down:
Update kre.CustomerOrderLineCopy
SET DepNo = (SELECT customerordercopy.DepNo
FROM kre.CustomerOrderCopy --, kre.CustomerOrderLineCopy
WHERE CustomerOrderLineCopy.OrderCopyNo =kre.CustomerOrderCopy.OrderCopyNo)
WHERE CustomerOrderLineCopy.OrderCopyNo = (SELECT CustomerOrderCopy.OrderCopyNo
FROM kre.CustomerOrderCopy --, kre.CustomerOrderLineCopy
WHERE kre.CustomerOrderLineCopy.OrderCopyNo = kre.CustomerOrderCopy.OrderCopyNo)
(Where I've commented out kre.CustomerOrderLineCopy in the subqueries) That is, you were hopefully trying to correlate these subqueries with the outer table - not introduce another instance of kre.CustomerOrderLineCopy.
If you still get an error, then you still have multiple rows in kre.CustomerOrderCopy which have the same OrderCopyNo. If that's so, you need to give us (and SQL Server) the rules that you want to apply for how to select which row you want to use.
The danger of switching to the FROM ... JOIN form shown in #Avitus's answer is that it will no longer report if there are multiple matching rows - it will just silently pick one of them - which one is never made clear.
Now I look at the query again, I'm not sure it even needs a WHERE clause now. I think this is the same:
Update kre.CustomerOrderLineCopy
SET DepNo = (
SELECT customerordercopy.DepNo
FROM kre.CustomerOrderCopy
WHERE CustomerOrderLineCopy.OrderCopyNo = kre.CustomerOrderCopy.OrderCopyNo)

Resources