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.
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.
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.
I have a problem with my code.
I use this code for create a view on sql server :
SELECT
(SELECT FirstNameD + ' ' + LastNameD AS Expr1
FROM dbo.UsersDatas) AS UsersFullName,
PhoneNumberD, EmailAddressD, UserNameD
FROM dbo.UsersDatas AS UsersDatas_1
and use this view in my c# application.
after I set two rows with my c# app, I get this error from Visual Studio :
System.Data.Entity.Core.EntityCommandExecutionException: 'An error
occurred while reading from the store provider's data reader. See the
inner exception for details.'
my c# code is:
private void SetDataGridViewDatasMethod()
{
var Query = from MU in DataBaseDataD.VW_UsersDatasView
select MU;
var UsersDataD = Query.ToList();
UsersInfoDataGridView.ItemsSource = UsersDataD;
}
and I search a lot about it on the internet but I can't find any solution could you help me fix this problem please?
If you want fullname you can do like this,
no need of inner query
SELECT
FirstNameD + ' ' + LastNameD AS UsersFullName,
PhoneNumberD, EmailAddressD, UserNameD
FROM dbo.UsersDatas
As Shantanu says - no need for the inner query.
And rather than using the "+" operator you might want to use CONCAT
SELECT CONCAT(FirstNameD, ' ', LastNameD) AS UsersFullName,
PhoneNumberD, EmailAddressD, UserNameD
FROM dbo.UsersDatas
Or, if you're using SQL Server 2017 you could also use CONCAT_WS
SELECT CONCAT_WS(' ', FirstNameD, LastNameD) AS UsersFullName,
PhoneNumberD, EmailAddressD, UserNameD
FROM dbo.UsersDatas
The key difference between + and CONCAT is how they behave when one of the inputs is NULL.