SQL Statement ignored - plsql

I am writing this code in SQL and getting the following error:
LINE/COL ERROR
-------- -----------------------------------------------------------------
7/2 PL/SQL: SQL Statement ignored
Statement:
CREATE OR REPLACE FUNCTION GET_C_O(vretail_outlet_id int,
vtran_year varchar)
RETURN number
AS
stock number;
BEGIN
select vretail_outlet_id,
SUM(GET_C_O_STOCKFN_TEST(61, a.BATCHID, 1,
to_date('204-04-01','yyyy-mm-dd'),
vretail_outlet_id, vtran_year, 1) * QP.PRODUCTRATE) AS OPENING
From Promas
inner join Product A
on Promas.Pcode = A.Pcode and
vretail_outlet_id = A.Retail_outlet_id and
vtran_year = a.tran_year
inner Join QryProductrate Qp
on Qp.batchid = a.batchid and
QP.Retail_outlet_id = a.REtail_outlet_id and
Qp.Tran_year = a.tran_year
where a.tran_year = '2014_2015' and
1=1
group by vretail_outlet_id)
into stock
from dual;
Return Stock;
END;

The SELECT statement is not correct. Try:
select SUM(GET_C_O_STOCKFN_TEST(61, a.BATCHID, 1,
to_date('2014-04-01','yyyy-mm-dd'),
vretail_outlet_id, vtran_year, 1) * QP.PRODUCTRATE) AS OPENING
into stock
From Promas
inner join Product A
on Promas.Pcode = A.Pcode and
vretail_outlet_id = A.Retail_outlet_id and
vtran_year = a.tran_year
inner Join QryProductrate Qp
on Qp.batchid = a.batchid and
QP.Retail_outlet_id = a.REtail_outlet_id and
Qp.Tran_year = a.tran_year
where a.tran_year = '2014_2015';
Share and enjoy.

Related

Why does this CosmosDB SubQuery fail?

I am trying to use a simple subquery to get a value but I get an error about cardinality
Query as follows:
SELECT va.variantId AS ItemNo,
c.Season,
SUBSTRING(va.variantId, 0, 7) AS ProductNo,
SUBSTRING(va.variantId, 0, 10) AS ArticleNo,
SUBSTRING(va.variantId, 0, 13) AS VariantNo,
(
SELECT VALUE p["value"]
FROM c
JOIN p IN c.OriginalData.presentation.productNameLong
WHERE c.ItemNo = '123456'
AND p.locale = 'en-GB'
AND c.Season = '201808'
AND c.brand = 'xxx'
) AS Title
FROM c
JOIN sm IN c.OriginalData.base.sales.summary.salesMarkets
JOIN ar IN sm.articles
JOIN va IN ar.variants
JOIN ch IN va.channels
WHERE c.ItemNo = '123456'
AND sm.salesMarket = 'SE'
AND ch.channelName = 'xxx'
The error is as follows:
Failed to query item for container rawdata:
Gateway Failed to Retrieve Query Plan: Message: {"errors":[{"severity":"Error","location":{"start":227,"end":498},"code":"SC2201","message":"The cardinality of a scalar subquery result set cannot be greater than one."}]}
ActivityId: 11ff3b08-d8a7-4737-9111-81f319cf1dc5, Microsoft.Azure.Documents.Common/2.11.0, Microsoft.Azure.Documents.Common/2.11.0
The result of that subquery can only ever be 1 single result so I am not sure what it is complaining about
You need to use ARRAY expression to construct an array from subquery's results.
Please try this SQL:
SELECT va.variantId AS ItemNo,
c.Season,
SUBSTRING(va.variantId, 0, 7) AS ProductNo,
SUBSTRING(va.variantId, 0, 10) AS ArticleNo,
SUBSTRING(va.variantId, 0, 13) AS VariantNo,
ARRAY(
SELECT VALUE p["value"]
FROM c
JOIN p IN c.OriginalData.presentation.productNameLong
WHERE c.ItemNo = '123456'
AND p.locale = 'en-GB'
AND c.Season = '201808'
AND c.brand = 'xxx'
) AS Title
FROM c
JOIN sm IN c.OriginalData.base.sales.summary.salesMarkets
JOIN ar IN sm.articles
JOIN va IN ar.variants
JOIN ch IN va.channels
WHERE c.ItemNo = '123456'
AND sm.salesMarket = 'SE'
AND ch.channelName = 'xxx'

i have several errors of PLS-00049

cursor is there to check for eligibility in student. however i cant even compile the script as i keep getting several of error:
[Error] PLS-00049 (395: 46): PLS-00049: bad bind variable 'SORLCUR_PIDM'.
CURSOR C_ENSP IS
SELECT SFRENSP_ESTS_CODE
,STVESTS_DESC
FROM STVESTS,SFRENSP
WHERE SFRENSP_PIDM = :Sorlcur_PIDM
AND SFRENSP_TERM_CODE = :sorlcur_TERM_CODE
AND SFRENSP_KEY_SEQNO = :SORLCUR_KEY_SEQNO
AND STVESTS_CODE = SFRENSP_ESTS_CODE;
CURSOR C_ENSP_SECOND IS
SELECT 'Y'
FROM SORLCUR, SFRENSP
WHERE SORLCUR_PIDM = :Sorlcur_PIDM
and SORLCUR_PIDM = SFRENSP_PIDM
AND sorlcur_TERM_CODE = :sorlcur_TERM_CODE
AND sorlcur_TERM_CODE = SFRENSP_TERM_CODE
AND SORLCUR_CURRENT_CDE = 'Y'
AND SORLCUR_CACT_CODE = 'ACTIVE'
AND SORLCUR_LMOD_CODE = 'LEARNER'
AND SORLCUR_PRIORITY_NO = 2
AND SFRENSP_KEY_SEQNO = SORLCUR_KEY_SEQNO
AND SFRENSP_ESTS_CODE NOT IN ( 'W', 'X' , 'Z');
v_ENSP_SECOND VARCHAR2(1);
CURSOR c_cohort IS
SELECT chrt.sgrchrt_chrt_code
FROM sgrchrt chrt
WHERE sgrchrt_pidm = :sorlcur_pidm
AND NVL(chrt.sgrchrt_stsp_key_sequence,99999999) = :sorlcur_key_seqno
AND NVL(sgrchrt_term_code_eff,'~') = :stvterm_term_code;
CURSOR c_resd IS
SELECT sgbstdn_resd_code,stvresd_desc
FROM sgbstdn,stvresd
WHERE SGBSTDN_PIDM = :KEY_BLOCK.PIDM
AND SGBSTDN_TERM_CODE_EFF = :KEY_BLOCK.STVTERM_TERM_CODE
AND stvresd_code = sgbstdn_resd_code;
cursor c1 is
select skricas_cas_number,
skricas_cas_status
from skricas a
where a.SKRICAS_PIDM = :key_block.PIDM
and exists (select 'x' from sorlcur
where sorlcur_pidm = skricas_pidm
and sorlcur_seqno = skricas_lcur_seqno
and sorlcur_term_code = :key_block.stvterm_term_code)
and skricas_cas_status = 'USED'
and skricas_cas_exp_date = (select max(a1.skricas_cas_exp_date)
from skricas a1
where a1.skricas_pidm = a.skricas_pidm
and a1.skricas_cas_status = a.skricas_cas_status
);
-- TT06987 start
CURSOR get_JSopt_in IS
SELECT szrasop_opt_in
FROM szrasop
WHERE szrasop_pidm = :sorlcur.Sorlcur_PIDM
AND szrasop_term_code = :sorlcur.Sorlcur_TERM_CODE;
--- v_elig VARCHAR2(1);
CURSOR get_max_szrolps IS
SELECT MAX( SZROLPS_SEQ_NO)
FROM SATURN.SZROLPS
WHERE SZROLPS_PIDM = :sorlcur.Sorlcur_PIDM
AND SZROLPS_TERM_CODE = :sorlcur.Sorlcur_TERM_CODE;
v_max_stage szrolps.szrolps_seq_no%TYPE;
-- TT06987 end
BEGIN
IF :sorlcur_program IS NOT NULL THEN
:SORLCUR_SMRPCMT_TEXT := F_UOG_GET_COMMENT(:sorlcur_program, :sorlcur_term_code, 'PRGTIT');
OPEN C_ENSP;
FETCH C_ENSP INTO :SFRENSP_ESTS_CODE, :ESTS_DESCRIPTION;
CLOSE C_ENSP;
OPEN C_ENSP_SECOND;
FETCH C_ENSP_SECOND INTO v_ENSP_SECOND ;
IF C_ENSP_SECOND%NOTFOUND THEN
:secondary_prog := 'N';
ELSE
:secondary_prog := 'Y';
END IF;
CLOSE C_ENSP_SECOND;
END IF;
OPEN c_cohort;
FETCH c_cohort
INTO :ENTRY_COHORT;
CLOSE c_cohort;
OPEN c_resd;
FETCH c_resd
INTO :sgbstdn_resd_code,:stvresd_desc;
CLOSE c_resd;
SELECT STVATTS_DESC
INTO :mode_desc
FROM STVATTS
WHERE :sorlcur_mode_code = STVATTS.STVATTS_CODE
AND STVATTS.STVATTS_CODE LIKE 'M%';
select stvstyp_desc
into :sorlcur.styp_desc
from stvstyp
where :SORLCUR_STYP_CODE = stvstyp_code;
:sorlcur.SITE_DESC := gb_stvsite.f_get_description(:SORLCUR_SITE_CODE);
get_last_section_completed;
-- added by Mary for 5095
open c1;
fetch c1 into :cas_number,
:cas_status;
close c1;
-- TT06987 start
-- Student eligible??
IF hwzkimag.F_eligible_js (:sorlcur.Sorlcur_PIDM, :sorlcur.sorlcur_TERM_CODE)
THEN
:optin_elig := 'Y';
END IF;
--message(:optin_elig);message(:optin_elig);
IF NVL(:optin_elig,'XXX') = 'Y' THEN
OPEN get_max_szrolps;
FETCH get_max_szrolps
INTO v_max_stage;
CLOSE get_max_szrolps;
-- ELigible and got passed the opt in / opt out stage 7.
IF v_max_stage >= 7 THEN
OPEN get_JSopt_in;
FETCH get_JSopt_in
INTO :optin_yes;
CLOSE get_JSopt_in;
set_item_property('jsopt_message',visible,property_false);
-- Eligible but has not passed opt in / opt out stage 7.
ELSE
:jsopt_message := 'OLR not complete';
set_item_property('jsopt_message',visible,property_true);
END IF;
END IF;
-- TT06987 end
i've solved the problem. There were two errors in the below code. 1st is bad bind variable and 2nd is invalid identifier.
CURSOR C_ENSP IS
SELECT SFRENSP_ESTS_CODE
,STVESTS_DESC
FROM STVESTS,SFRENSP
WHERE SFRENSP_PIDM = :Sorlcur_PIDM
AND SFRENSP_TERM_CODE = :sorlcur_TERM_CODE
AND SFRENSP_KEY_SEQNO = :SORLCUR_KEY_SEQNO
AND STVESTS_CODE = SFRENSP_ESTS_CODE;
1st - i took out the colons on the columns name (e.g. :sorlcur_pidm to just sorlcur_pidm)
2nd - the table name sorlcur wasnt defined as i was calling table columns from 3 different tables.
-- The correct Syntax is below --
CURSOR C_ENSP IS
SELECT SFRENSP_ESTS_CODE
,STVESTS_DESC
FROM STVESTS,SFRENSP,SORLCUR
WHERE SFRENSP_PIDM = Sorlcur_PIDM
AND SFRENSP_TERM_CODE = sorlcur_TERM_CODE
AND SFRENSP_KEY_SEQNO = SORLCUR_KEY_SEQNO
AND STVESTS_CODE = SFRENSP_ESTS_CODE;

Sql Query in Typed Dataset causing Parser Error

I have the this SQL query in a typed dataset in asp.net 2005 and SQL Server 2008 R2.
i.e I have used in Typed Dataset -- "Add Query" -- "Use Sql Statements" and used the below script, then I returned a Datatable. ("Fill a Datatable" checkbox was unchecked")
SELECT DISTINCT
ISNULL(Projects.ProjectID,'0') as
ProjectID,Messages.MessageID, Projects.ProjectName, Customers.CustomerName as
CustomerName, Regions.RegionName,
(select U.firstName + ' ' + U.LastName
from Users U inner join Projects P on P.ProjectManagerId = U.UserID inner join
Messages M on M.ProjectId = P.ProjectID
where M.MessageID = Messages.MessageID) as ProjectManagerName,
dbo.phases.TagName as Phase,
(select U.firstName + ' ' + U.LastName
from Users U inner join Messages M on M.CreatedBy = U.UserID Where M.MessageID =
Messages.MessageID) as CreatedBy,
Messages.DateCreated as EmailCreatedDate,
Messages.MessageSubject as MessageSubject,
Users.Email as [From],
**(stuff((select ','+U.EMAIL
FROM USERS U INNER JOIN Recipients R ON U.USERID = R.USERID INNER JOIN MESSAGES M
ON R.MESSAGEID = M.MESSAGEID
WHERE m.MessageID=Messages.MessageID AND R.RecipientTypeID=1
for xml path(''), type).value('.', 'nvarchar(max)'), 1, 1, '')) as [TO],**
**(stuff((select ','+U.EMAIL
FROM USERS U INNER JOIN Recipients R ON U.USERID = R.USERID INNER JOIN
MESSAGES M ON R.MESSAGEID = M.MESSAGEID
WHERE m.MessageID=Messages.MessageID AND R.RecipientTypeID=2
for xml path(''), type).value('.', 'nvarchar(max)'), 1, 1, '')) as CC**
FROM Messages INNER JOIN Users
ON Messages.Sender = Users.UserID
INNER JOIN Recipients
ON Messages.MessageID = Recipients.MessageID
LEFT OUTER JOIN dbo.Projects
ON dbo.Messages.ProjectID = dbo.Projects.ProjectID
Left OUTER JOIN dbo.Customers on dbo.Projects.CustomerId =
dbo.Customers.CustomerID
Left outer join dbo.regions on dbo.Customers.RegionID =
dbo.Regions.RegionID
left outer join dbo.Phases on messages.phaseid = dbo.phases.phaseid
where (Messages.MessageTypeID=1)
and Projects.ProjectID <> '0'
order by Projects.ProjectName
But on clicking next , it is causing an error like this:
Error in WHERE clause near '('.
Unable to parse query text.
However If I run the same above query in SQL Server 2008 R2 Management Studio, it runs fine and returns the results.
Please suggest what am I doing wrong.
Thanks
Hi, I have used one way out for this..ie used this Query(after slight modification) as inline query in .cs code behind file.. but now another problem is arising....ie.
Now it is taking too much of time to return results.. is it because of the functions I have used in the query ? please suggest some workaround for this..
SELECT DISTINCT
ISNULL(Projects.ProjectID,'0') as
ProjectID,Messages.MessageID, Projects.ProjectName, Customers.CustomerName as CustomerName, Regions.RegionName,
(select U.firstName + ' ' + U.LastName
from Users U inner join Projects P on P.ProjectManagerId = U.UserID inner join Messages M on M.ProjectId = P.ProjectID
where M.MessageID = Messages.MessageID) as ProjectManagerName,
dbo.phases.TagName as Phase,
(select U.firstName + ' ' + U.LastName
from Users U inner join Messages M on M.CreatedBy = U.UserID Where M.MessageID = Messages.MessageID) as CreatedBy,
Messages.DateCreated as EmailCreatedDate,
Messages.MessageSubject as MessageSubject,
Users.Email as [From],
(select [dbo].[fn_ForEmailReport](Messages.MessageID,1)) as [TO],
(select [dbo].[fn_ForEmailReport](Messages.MessageID,2)) as [CC]
FROM Messages INNER JOIN Users
ON Messages.Sender = Users.UserID
INNER JOIN Recipients
ON Messages.MessageID = Recipients.MessageID
LEFT OUTER JOIN dbo.Projects
ON dbo.Messages.ProjectID = dbo.Projects.ProjectID
Left OUTER JOIN dbo.Customers on dbo.Projects.CustomerId = dbo.Customers.CustomerID
Left outer join dbo.regions on dbo.Customers.RegionID = dbo.Regions.RegionID
left outer join dbo.Phases on messages.phaseid = dbo.phases.phaseid
where (Messages.MessageTypeID=1)
and Projects.ProjectID <> '0'
order by Projects.ProjectName
-- FUNCTION "fn_ForEmailReport" used in above query.
ALTER FUNCTION [dbo].[fn_ForEmailReport]
(
#MessageID int,
#RecipientTypeID int
)
RETURNS NVARCHAR(MAX)
AS
BEGIN
DECLARE #List VARCHAR(8000)
set #List = (stuff((select ','+U.EMAIL
FROM USERS U inner JOIN Recipients R ON R.USERID = U.USERID inner JOIN
MESSAGES M ON R.MESSAGEID = M.MESSAGEID
WHERE m.MessageID=#MessageID AND R.RecipientTypeID=#RecipientTypeID
for xml path(''), type).value('.', 'nvarchar(max)'), 1, 1, ''))
RETURN #List;
END
There are too many joins I believe. For example, the query below
(select U.firstName + ' ' + U.LastName
from Users U inner join Projects P
on P.ProjectManagerId = U.UserID
inner join Messages M on M.ProjectId = P.ProjectID
where M.MessageID = Messages.MessageID) as ProjectManagerName,
gets only a firstname and lastname, a cell, it doesn't need to be a join query. Plus, you have already joined Projects table at the end. You can give an alias to outer query and make inner queries simpler:
SELECT DISTINCT
ISNULL(Projects.ProjectID,'0') as
ProjectID,Messages.MessageID, Projects.ProjectName, Customers.CustomerName as CustomerName, Regions.RegionName,
(select U.firstName + ' ' + U.LastName
from Users WHERE P.ProjectManagerId = UserID) as ProjectManagerName,
dbo.phases.TagName as Phase,
(select U.firstName + ' ' + U.LastName
from Users WHERE M.CreatedBy = UserID) as CreatedBy,
Messages.DateCreated as EmailCreatedDate,
Messages.MessageSubject as MessageSubject,
Users.Email as [From],
(select [dbo].[fn_ForEmailReport](Messages.MessageID,1)) as [TO],
(select [dbo].[fn_ForEmailReport](Messages.MessageID,2)) as [CC]
FROM Messages M INNER JOIN Users
ON Messages.Sender = Users.UserID
INNER JOIN Recipients
ON Messages.MessageID = Recipients.MessageID
LEFT OUTER JOIN dbo.Projects P
ON dbo.Messages.ProjectID = dbo.Projects.ProjectID
Left OUTER JOIN dbo.Customers on dbo.Projects.CustomerId = dbo.Customers.CustomerID
Left outer join dbo.regions on dbo.Customers.RegionID = dbo.Regions.RegionID
left outer join dbo.Phases on messages.phaseid = dbo.phases.phaseid
where (Messages.MessageTypeID=1)
and Projects.ProjectID <> '0'
order by Projects.ProjectName
Finally, if you don't retrieve any information from Customers table, you can omit the following line:
Left OUTER JOIN dbo.Customers on dbo.Projects.CustomerId = dbo.Customers.CustomerID

Symfony2 doctrine DQL does not replace wildcards

I'm trying to query the model from my Symfony2 project and I am not able to replace wildcards into my DQL query. Look;
$q2 =
'SELECT
p.codigo,
p.descripcion,
SUM(l.cantidad) as cantidad,
SUM(l.cantidad*l.pvp) as euros
FROM
MGFAppBundle:LineaVenta l
JOIN
MGFAppBundle:Producto p
WITH
l.producto = p.id
JOIN
l.venta v
WITH
l.venta = v.id
WHERE
l.producto IN (:array)
AND
v.farmacia = :farmacia
GROUP BY
p.codigo';
$query2 = $this->em->createQuery($q2)
->setParameter('farmacia', $farmacia)->setParameter('array', $array);
$porFarmacia = $query2->getResult();
// This does not return a single value, when it should return 2 lines.
echo $query2->getSQL();
// Returns:
// SELECT p0_.codigo AS codigo0, p0_.descripcion AS descripcion1, SUM(l1_.cantidad) AS sclr2, SUM(l1_.cantidad * l1_.pvp) AS sclr3 FROM LineaVenta l1_ INNER JOIN Producto p0_ ON (l1_.lineaventas_id = p0_.id) INNER JOIN Venta v2_ ON l1_.venta_id = v2_.id AND (l1_.venta_id = v2_.id) WHERE l1_.lineaventas_id IN (?) AND v2_.farmacia_id = ? GROUP BY p0_.codigo
So, question marks where parameters should be. Any hint? Thanks in advance.

dbo.StoredProcedure SQL Syntax error near 'SELECT' and near 'AS'

Hey guys I hope you can help me I've tried every thing I can think of and it keeps telling me that my syntax near SELECT and that my syntax near AS is incorrect
CREATE PROCEDURE dbo.StoredProcedure2
SELECT
, Announcements.ID
, Announcement.CreateDate
, Announcements.Announcement
,aspnet_Users.UserName
,(SELECT Announcement_Read_State.Read_Date
FROM Announcement_Read_State
WHERE Announcement_Read_State.Announcement_ID = Announcements.ID
AND Announcement_Read_State.User_ID = 2) AS ReadState
FROM Announcements INNER JOIN aspnet_User ON Announcements .Sender_User_ID = aspnet_User.UserName
WHERE (Announcements.ID IN
( SELECT Max(Announcements.ID)
FROM Thread_Participant INNER JOIN Announcements ON
Thread_Participant.ThreadId = Announcements.Announcement_ThreadId
WHERE MessageThreadParticipant.UserID = 2
GROUP BY ThreadParticipant.AnnouncementThreadId
)
ORDER BY Message.CreateDate DESC;
It should be:
CREATE PROCEDURE dbo.StoredProcedure2
AS -- you were missing this
SELECT -- you had an extra comma here
Announcements.ID
, Announcement.CreateDate
, Announcements.Announcement
,aspnet_Users.UserName
,(SELECT Announcement_Read_State.Read_Date
FROM Announcement_Read_State
WHERE Announcement_Read_State.Announcement_ID = Announcements.ID
AND Announcement_Read_State.User_ID = 2) AS ReadState
FROM Announcements INNER JOIN aspnet_User ON Announcements .Sender_User_ID = aspnet_User.UserName
WHERE (Announcements.ID IN
( SELECT Max(Announcements.ID)
FROM Thread_Participant INNER JOIN Announcements ON
Thread_Participant.ThreadId = Announcements.Announcement_ThreadId
WHERE MessageThreadParticipant.UserID = 2
GROUP BY ThreadParticipant.AnnouncementThreadId
)
)-- you were missing this one
ORDER BY Message.CreateDate DESC;

Resources