pl sql oracle subquery - database

I have a procedure:
CREATE OR REPLACE PROCEDURE recommend_book(
in_ID_user IN number
)
IS
--zmienne
zmienna1 number(9,0);
BEGIN
SELECT COUNT(GENRE)
INTO zmienna1
FROM BOOKS
WHERE ID_BOOK IN(SELECT ID_BOOK
FROM SIGNATURES
WHERE SIGNATURE IN (SELECT SIGNATURE
FROM ORDERS
WHERE ID_READER=in_ID_user)
);
END;
/
I'm getting the error
PL-00428: An INTO clause is expected in this SELECT statement
Could you please help me identify what's missing/what's wrong here?

For other developers when see this question, it should be an answer:
As Frank says:
Are you 100% sure that the error you're seeing isn't from a previous compilation attempt? Your procedure looks perfectly fine.
Just restart your compiler.

Related

SQL Conversion failed when converting the varchar value '*' to data type int

Aside: Please note that this is not a duplicate of the countless other string-to-int issues in sql. This is a specific cryptic error message (with the asterisk) that hides another problem
Today I came across an issue in my SQL that took me all day to solve. I was passing a data table parameter into my stored procedure and then inserting part of it into another table. The code used was similar to the following:
INSERT INTO tblUsers
(UserId,ProjectId)
VALUES ((SELECT CAST(CL.UserId AS int) FROM #UserList AS UL),#ProjectId)
I didn't seem to get the error message when using test data, only when making the call from the dev system.
What is the issue and how should the code look?
I could bet that (SELECT CAST(CL.UserId AS int) FROM #UserList AS UL) returns more than 1 row and your test scenario had only 1 row. But that may be just me.
Anyway, the way the code should look is:
INSERT INTO tblUsers (UserId,ProjectId)
SELECT CAST(CL.UserId AS int),
#ProjectId
FROM #UserList AS UL
After some time of trawling through google and such places, I have determined that this SQL code is wrong. I believe the correct code is something more like this:
INSERT INTO tblUsers
(UserId,ProjectId)
SELECT CAST(CL.UserId AS int) ,#ProjectId
FROM #UserList AS UL
The issue is that the other way of doing it attempts to insert one record with the data of all of the rows in the table parameter. I needed to remove the VALUES statement. Also, in order to add other data, I can simply put that as part of the select as you would otherwise

My stored procedure in SQL Server 2008 when is execute with JOB its fails, but without its working great

I have this stored procedure wich its like this :
ALTER PROCEDURE [dbo].[P_ALIMENTATION_VolumeVentes]
AS
BEGIN
SELECT EDS, NomEDS,AgenceEDS, AgenceNomEDS,SecteurEDS, SecteurNomEDS,DirectionEDS,DirectionNomEDS,
(SELECT count(*) FROM CPListeVentesNonConformes WHERE CPListeVentesNonConformes.EDS = CPRT.EDS AND TypePart='PP') AS ListeVenteNC_PP,
(SELECT count(*) FROM CEListeVentesNonConformes WHERE CEListeVentesNonConformes.EDS = CPRT.EDS AND TypePart='ET') AS ListeVenteNC_ET,
(SELECT count(*) FROM CPListeVentesNonConformes WHERE CPListeVentesNonConformes.EDS = CPRT.EDS AND TypePart='PP' OR TypePart='ET') AS ListeVenteNC_PPET,
(SELECT count(*) FROM ListeVentes WHERE IDES01 = CPRT.EDS AND TypePart='PP') AS ListeVentes
INTO VolumeVentes
FROM CPR CPRT
GROUP BY EDS, NomEDS,AgenceEDS, AgenceNomEDS,SecteurEDS, SecteurNomEDS,DirectionEDS,DirectionNomEDS,TypePart
END
When i execute with the command line EXEC [dbo].[P_ALIMENTATION_VolumeVentes]
that work super great my table is create.
But when i use SQL Agent to schedule a job i have a nice surprise to have this error :
Executed as user: ZRES\CSAPREP10IUCRADM. The conversion of a varchar
data type to a datetime data type resulted in an out-of-range value.
[SQLSTATE 22007] (Error 242) The statement has been terminated.
[SQLSTATE 01000] (Error 3621). The step failed.
The structure table who will be create VolumetVentes have no fields with a type as datetime
Here is the structure of the tableVolumeVentes
I don't understand exactly where is the error ?
Thank you for help
Actually it should never work since you already have VolumeVente table.
SELECT INTO creates new table with columns described in select statement
https://msdn.microsoft.com/en-us/library/ms188029(v=sql.120).aspx
You should modify this code to become INSERT SELECT.
But you will probably still get the same conversion error because (I guess) column order is not correct in select statment and does not match column order in existing table. That is why you should always explicitly define column list in INSERT INTO clause, so the final script will look like:
INSERT INTO VolumeVentes(EDS, NomEDS,AgenceEDS, AgenceNomEDS,SecteurEDS, ...)
SELECT EDS, NomEDS,AgenceEDS, AgenceNomEDS,SecteurEDS
FROM ...
Use
INSERT INTO... Statement
instead of
SELECT INTO FROM... Statement

Pro*C returning Not a number in the IN clause

I am trying to run an SQL from my C code using Pro*c. This is my SQL
EXEC SQL select count(1) from MY_TABLE where id IN ( :format );
id is a NUMBER(10) and format is a char array containing value 1,2,3,4,5
This is returning error with "Not a Number"
However if the format array is just a single number, its running fine.
Please let me know if someone find the error.
Thx!
IN clause accept bind variables only as (:1,:2,:3) , so you have yo know the number of bind variables before hand. Which is not likely.
Simplest way is to form a dynamic query string with hard coded values in Pro*C.
There are alternative solutions from AsKTom and My SO answer
for(i=0;i<5;i++)
{
EXEC SQL select count(1) from MY_TABLE where id IN ( :format[i] );
}
I am telling to use above code as it is too lousy, just explaining how arrays works in Pro*C. You have to give the index of the array.
Edit: I learned a new thing for this problem:- We can also use
EXEC SQL FOR 5
SELECT COUNT(1) FROM MY_TABLE WHERE id IN (:format);
EXEC SQL SELECT COUNT(1) FROM MY_TABLE WHERE id IN
(:format[0],:format[1],:format[2],:format[3],:format[4])

PL/SQL: SQL Statement ignored?

Hi everyone getting this error message when trying to create a trigger and its got me a little stumped.
Here is my trigger code.
CREATE OR REPLACE TRIGGER CUSTOMER_AD
AFTER DELETE ON CUSTOMER
REFERENCING OLD AS OLD
FOR EACH ROW
DECLARE
nPlaced_order_count NUMBER;
BEGIN
SELECT COUNT(*)
INTO nPlaced_order_count
FROM PLACED_ORDERS p
WHERE p.FK1_CUSTOMER_ID = OLD.CUSTOMER_ID;
IF nPlaced_order_count > 0 THEN
INSERT into previous_customer
(customer_id,
first_name,
last_name,
address,
AUDIT_USER,
AUDIT_DATE)
VALUES
(:old.customer_id,
:old.first_name,
:old.last_name,
:old.address,
UPPER(v('APP_USER')),
SYSDATE);
END IF;
END CUSTOMER_AD;
And the error I'm getting 'Error at line 4: PL/SQL: SQL Statement ignored 0.10 seconds'
Anyone any guesses why?
thanks for the help
The error shown is only the highest level. Depending where you're running it you should be able to see the stack trace. The client will determine exactly how to do that; SQL*Plus or SQL Developer would show you more than this anyway, but I don't really know about other clients. If you can't see the details in your client then you can query for them with:
select * from user_errors where name = 'CUSTOMER_AD' and type = 'TRIGGER'
Assuming the tables all exist, it's probably this line:
WHERE p.FK1_CUSTOMER_ID = OLD.CUSTOMER_ID;
which should be:
WHERE p.FK1_CUSTOMER_ID = :OLD.CUSTOMER_ID;
When referencing the old (or new) value from the table, the name as specified in the referencing clause has be preceded by a colon, so :OLD in this case. As you're doing already in the insert ... values() clause.
(From comments, my assumption turned out to be wrong - as well as the missing colon problem, the table name is really placed_order, without an s).
Seems like you copied code from both answers to your previous question without really understanding what they were doing. You might want to look at the trigger design guidelines (particularly the one about not dupicating database functionality) and the syntax for create trigger which introduces the referencing clause.

How do you remotely execute a table valued function in Sql-Server 2000/5?

I have a stored procedure (sql2005) that needs to call a table valued function on a remote server (sql2000).
My query is as follows:
select
*
from
mytable mt
cross apply
opendatasource('sqloledb','Data Source=remoteserver;UID=user;Password=pass').mydatabase.dbo.mytvf
(cast(param1 as numeric(20,0)), #param2, mt.param3)
I'm getting an incorrect syntax error near 'cast'. Is it possible to execute a tvf with this notation? Should I somehow be using openrowset? Any help is appreciated.
Here's the official word. You cannot call a UDF in this way.
http://connect.microsoft.com/SQLServer/feedback/details/276758/remote-table-valued-function-calls-are-not-allowed
Looks like Mircosoft is working on the ability in a future version though.

Resources