Error: "Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression."
INSERT INTO cid_unidade(co_cid, unid_codigo, tp_agravo)
select
(select cid_codigo from (values
('Y00'),('Y000'),('Y001'),('Y002' ),('Y003'),('Y004' ),('Y005'),('Y006'),('Y007'),
('Y008'),('Y009'),('Y01'),('Y010'),('Y011'),('Y012'),('Y013'),('Y014'),('Y015'),
('Y016'),('Y017'),('Y018'),('Y019'),('Y02'),('Y020'),('Y021'),('Y022'),('Y023'),
('Y024'),('Y025'),('Y026'),('Y027'),('Y028'),('Y029'),('Y03'),('Y030'),('Y031'),
('Y032'),('Y033'),('Y034'),('Y035'),('Y036'),('Y037'),('Y038'),('Y039'),('Y04'),
('Y040'),('Y041'),('Y042'),('Y043'),('Y044'),('Y045'),('Y046'),('Y047'),('Y048'),
('Y049'),('Y05'),('Y050'),('Y051'),('Y052'),('Y053'),('Y054'),('Y055'),('Y056'),
('Y057'),('Y058'),('Y059'),('Y06'),('Y060'),('Y061'),('Y062'),('Y063'),('Y064'),
('Y065'),('Y066'),('Y067'),( 'Y068'),( 'Y069'),( 'Y07'),( 'Y070'),( 'Y071'),( 'Y072'),
('Y073'),( 'Y074'),( 'Y075'),( 'Y076'),( 'Y077'),( 'Y078'),( 'Y079'),( 'Y08'),( 'Y080'),
('Y081'),( 'Y082'),( 'Y083'),( 'Y084'),( 'Y085'),( 'Y086'),( 'Y087'),( 'Y088'),( 'Y089'),
('R456'),( 'T742')) t(cid_codigo)
WHERE cid_codigo NOT IN (SELECT co_cid FROM cid_unidade)),
'0067', '1'
The error is pretty clear. The outer select returns too many rows. You can just remove it:
INSERT INTO cid_unidade(co_cid, unid_codigo, tp_agravo)
select cid_codigo, '0067', '1'
from (values
('Y00'),('Y000'),('Y001'),('Y002' ),('Y003'),('Y004' ),('Y005'),('Y006'),('Y007'),
('Y008'),('Y009'),('Y01'),('Y010'),('Y011'),('Y012'),('Y013'),('Y014'),('Y015'),
('Y016'),('Y017'),('Y018'),('Y019'),('Y02'),('Y020'),('Y021'),('Y022'),('Y023'),
('Y024'),('Y025'),('Y026'),('Y027'),('Y028'),('Y029'),('Y03'),('Y030'),('Y031'),
('Y032'),('Y033'),('Y034'),('Y035'),('Y036'),('Y037'),('Y038'),('Y039'),('Y04'),
('Y040'),('Y041'),('Y042'),('Y043'),('Y044'),('Y045'),('Y046'),('Y047'),('Y048'),
('Y049'),('Y05'),('Y050'),('Y051'),('Y052'),('Y053'),('Y054'),('Y055'),('Y056'),
('Y057'),('Y058'),('Y059'),('Y06'),('Y060'),('Y061'),('Y062'),('Y063'),('Y064'),
('Y065'),('Y066'),('Y067'),( 'Y068'),( 'Y069'),( 'Y07'),( 'Y070'),( 'Y071'),( 'Y072'),
('Y073'),( 'Y074'),( 'Y075'),( 'Y076'),( 'Y077'),( 'Y078'),( 'Y079'),( 'Y08'),( 'Y080'),
('Y081'),( 'Y082'),( 'Y083'),( 'Y084'),( 'Y085'),( 'Y086'),( 'Y087'),( 'Y088'),( 'Y089'),
('R456'),( 'T742')) t(cid_codigo)
WHERE cid_codigo NOT IN (SELECT co_cid FROM cid_unidade));
Note that the constant values are included in the select.
Related
I have an issue when I try to set the value of a variable with a subquery.
This is my SQL code:
SELECT #V_SOURCE = (SELECT ITEM_SOURCE
FROM TABLE1
WHERE OPP_CODE = #V_OPP_CODE
AND PDGROUPNO = #V_PRD_GROUP_NO
AND DELETE_FLAG IS NULL
AND CONTRACTOR = #V_CONTRACTOR
AND OPP_ITEM_NO = #_OPP_ITEM_NO)
When I run this code with an assumed variable that is used in WHERE condition, it returns only 1 row and 1 col that is correct but if I run this code with store procedure it will return the error:
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <=, >, >= or when the subquery is used as an expression
The sub-query can return multiple rows.
Even if it shouldn't, that won't do.
But you can change it to this.
SELECT #V_SOURCE = ITEM_SOURCE
FROM TABLE1
WHERE OPP_CODE = #V_OPP_CODE
AND PDGROUPNO = #V_PRD_GROUP_NO
AND DELETE_FLAG IS NULL
AND CONTRACTOR = #V_CONTRACTOR
AND OPP_ITEM_NO = #_OPP_ITEM_NO
GROUP BY ITEM_SOURCE;
It'll assign the last value of the resultset to the variable.
Which is fine, since you expect only one anyway.
Another way is to pick only the top 1
SET #V_SOURCE = (
SELECT TOP 1 ITEM_SOURCE
FROM TABLE1
WHERE OPP_CODE = #V_OPP_CODE
AND PDGROUPNO = #V_PRD_GROUP_NO
AND DELETE_FLAG IS NULL
AND CONTRACTOR = #V_CONTRACTOR
AND OPP_ITEM_NO = #_OPP_ITEM_NO
);
I find an issue in my query because of I query data to use for this query some CONTRACTOR is NULL then it makes an error when using this query
SELECT #V_SOURCE = (SELECT ITEM_SOURCE
FROM TABLE1
WHERE OPP_CODE = #V_OPP_CODE
AND PDGROUPNO = #V_PRD_GROUP_NO
AND DELETE_FLAG IS NULL
AND CONTRACTOR = #V_CONTRACTOR
AND OPP_ITEM_NO = #_OPP_ITEM_NO)
When I filter data that CONTRACTOR is NULL out, I don't get any error now.
I'm trying to run a query in Hive where I run a join based on a case statement. For some reason, I'm having problems on lines 7 and 8. I have not been able to resolve the error which is
line 7: Expected: AND, AS, BETWEEN, DIV, ILIKE, IN, IREGEXP, IS, LIKE, NOT, OR, REGEXP, RLIKE CAUSED BY: Exception: Syntax error
line 8: Encountered: AS Expected: AND, BETWEEN, DIV, ILIKE, IN, IREGEXP, IS, LIKE, NOT, OR, REGEXP, RLIKE CAUSED BY: Exception: Syntax error
select * from dra_record_set.mark_set inv
INNER JOIN innerdb.name_set roll_table
on inv.record_id = roll_table.ply_record_id AND
roll_table.date =
(CASE
WHEN inv.purchase_day>0 AND inv.purhcase_date BETWEEN roll_table.discount_start_dt AND roll_table.discount_end_dt) THEN inv.purchase_date
ELSE WHEN (CONCAT(inv.purchase_yr,inv.purchase_mo,(CAST("15")AS INT))) AS temp_var BETWEEN roll_table.discount_start_dt AND roll_table.discount_end_dt) THEN inv.purchase_date
END AS temp_pur_dt)
WHERE inv.inroll_discount_eligible_flag =1
limit 10
case is an expression. It returns a value. Your seems to be more like a macro substitution for SQL code.
Remove the case and just use boolean logic:
from dra_record_set.mark_set inv join
innerdb.name_set roll_table
on inv.record_id = roll_table.ply_record_id and
( (inv.purchase_day > 0 and
inv.purhcase_date between roll_table.discount_start_dt and roll_table.discount_end_dt
) or
(inv.purchase_day <= 0 and
CONCAT(inv.purchase_yr, inv.purchase_mo, 15) between roll_table.discount_start_dt AND roll_table.discount_end_dt
)
)
The nested query only show a single row. When I add more data, it throws an error.
I'm using SQL Server 2014.
SELECT
ListaMaestra.id_ListaMaestra, ListaMaestra.Clave,
ListaMaestra.Nombre_P, ListaMaestra.Modulo_P,
ListaMaestra.Caracteristicas, ListaMaestra.Tipo_Formato,
ListaMaestra.Fecha_Emision, ListaMaestra.Fecha_Revision,
ListaMaestra.Revision, ListaMaestra.Norma,
empleado.nombre, cargo.nombre_cargo,
(SELECT empleado.nombre FROM ListaMaestra, empleado
WHERE ListaMaestra.Nombre_Reviso = empleado.id_empleado) AS Nombre_Elaboro,
(SELECT cargo.nombre_cargo FROM ListaMaestra, cargo
WHERE ListaMaestra.Cargo_Reviso = cargo.id_cargo) AS Cargo_Elaboro,
ListaMaestra.Estatus, ListaMaestra.Ruta_PDF
FROM
ListaMaestra, empleado, cargo
WHERE
ListaMaestra.Nombre_Elaboro = empleado.id_empleado
AND ListaMaestra.Cargo_Elaboro = cargo.id_cargo
ERROR:
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
The problem should go away if you remove ListaMaestra from the from clause of each subselect, since you've already referenced ListaMaestra in the from clause of the main select.
Getting this error with the following query in SQL Server 2014.
SELECT
[Case]
,[Course]
,[Device]
,[IntegerValue]
,[Question]
,IFL.[QuestionSimplified]
,[Revision]
,[Script]
,[TextValue]
,[Timestamp]
,[Type]
,[Variable]
,[Wave]
FROM [dbo].[CosmosData] CD
Left Outer join [dbo].[ImportedFacilityList] IFL on CD.[Variable] = IFL.[Variablename]
where
CD.[Script] = 'CARD-F' and
(select * from [dbo].[CosmosData] where Variable = 'SURVEY_TYPE' and IntegerValue = '2')
When I run the above query I am getting the beloiw error,
Msg 4145, Level 15, State 1, Line 20
An expression of non-boolean type specified in a context where a condition is expected, near ')'.
Any help please?
You have this in the where clause:
and (select * from [dbo].[CosmosData] where Variable = 'SURVEY_TYPE' and IntegerValue = '2')
SQL needs a boolean expression. This is usually formed by using = or a similar comparison operator. In your case, I think you just exant exists:
exists (select * from [dbo].[CosmosData] where Variable = 'SURVEY_TYPE' and IntegerValue = 2)
That said, you might want a correlation clause as well.
Note: I removed the single quotes from the integer value. Only use single quotes for string and date constants.
based on the assumption that you are using case as a key in this table, you can use the follwing to return all rows from cosmos data where your conditions are applied and the select in your where clause has a match using the criteria within it.
SELECT
[Case]
,[Course]
,[Device]
,[IntegerValue]
,[Question]
,IFL.[QuestionSimplified]
,[Revision]
,[Script]
,[TextValue]
,[Timestamp]
,[Type]
,[Variable]
,[Wave]
FROM [dbo].[CosmosData] CD
Left Outer join [dbo].[ImportedFacilityList] IFL on CD.[Variable] = IFL. [Variablename]
where CD.[Script] = 'CARD-F'
and Case
IN
(select Case from [dbo].[CosmosData] where Variable = 'SURVEY_TYPE' and IntegerValue = '2')
Hope that helps any
please am trying to apply for a request from my website that am working on and l got this error:
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
The 'CalCulateClaimForDeath' procedure attempted to return a status of NULL, which is not allowed. A status of 0 will be returned instead.
The Source Error pointed me to this code:
Line 239:
Line 240: //finally, execute the command.
Line 241: int retval = cmd.ExecuteNonQuery();
Line 242:
Line 243: // detach the SqlParameters from the command object, so they can be used again.
Help is needed.thank you