Error in Insert query in a table by executing one system proc in sybase - sybase-ase

I am getting error while inserting data inside table TableSizes with below query,
Could anyone please help me in this regard.
declare #name varchar(256)
select #name ='TestTable'
begin
insert into workdb..TableSizes
exec sp_spaceused #name
end
go
Msg 156, Level 15, State 2
Server '<ServerName>', Line 5
Incorrect syntax near the keyword 'exec'.
Thanks in advance.

So what you are trying to do here is to insert a proc result set into a table, which is not allowed unless the table is a proxy table. If not, then complete the insert statement.

Related

Getting all rows from a oracle table

Suppose a oracle table has many rows. How can i fetch specific column for all rows?
Let, in a table named teacher_description, there are 100 rows with columns teacher_id,teacher_name. How can i get the teacher_id for 100 rows?
Thank you.
As Ashish Pandya said, you can use just a simple select.
If you get PLS-00428: an INTO clause is expected in this SELECT statement error, I think you are calling this statement inside a code block; in this case you need to get the result in a cursor.
Take a look on the following code:
set serveroutput on;
declare
cursor v_cursor is select teacher_id from teacher_description;
begin
for idx in v_cursor loop
dbms_output.put_line(idx.teacher_id);
end loop;
end;
/
If you need to understand the cursors you can find here a fast link, http://www.tutorialspoint.com/plsql/plsql_cursors.htm, but I suggest you start reading here: http://www.oracle.com/technetwork/issue-archive/2013/13-mar/o23plsql-1906474.html.
select teacher_id from teacher_description

Drop DB2 table if exists

In my script I have to do a lot of selects to a joined table, so instead I decided to put this join into a temporal table.
First I thought:
1. Create table
2. Put the data from the join into a table
3. Drop the table
But then I thought, what if the script fails before I dropped the table?
So I decided to go with:
1. Drop the table
2. Create the table
3. Put the data from the join into a table
I don't really mind if the table is left there until the next time I run the script, so the second option works too.
But what if somebody had already dropped the table?
I saw some systems have a "drop if exists" but unfortunately not DB2. I would like to do something that won't make the script die when the drop table fails.
Ideas? On any of this? Thanks!
EDIT: I forgot to say this is in a PERL script!
The best way to do this is by using an annonymous block like in this code
You need to call the drop table in a dynamic sql, and catch the exception in the block.
--#SET TERMINATOR #
begin
declare statement varchar(128);
declare continue handle for sqlstate '42710' BEGIN END;
SET STATEMENT = 'DROP TABLE MYTABLE';
EXECUTE IMMEDIATE STATEMENT;
end #
This code will run normally in DB2. It does not need to be part of a procedure nor function.
Why not look for the table first? If you find it, it needs to be dropped; if you don't, it doesn't.
db2perf_quiet_drop that might works the way you want.. Its a free add-on :)
You can look into this post too..
http://www.dbforums.com/showthread.php?1609047-DB2-equivalent-for-mysql-s-DROP-TABLE-IF-EXISTS
If this doesn't work for you please let me know what error you are getting so I can try to help :)
Or this might work
if( NOT exists( create table detailval
(
id int,
detaildeptNo int,
info varchar(255)
);
insert into detailval(1,1, 'detail values A');
insert into detailval(2,1, 'detail values B');
insert into detailval(3,1, 'detail values C');
insert into detailval(4,2, 'detail values D');
)
)
then customStoredproc('droptable');
end if;
End
I think you should look into working with temporary tables (DECLARE GLOBAL TEMPORARY TABLE). They are stored in the temporary table space and are dropped automatically after commit.
You can easily also query syscat.tables like this:
select COUNT(*) from SYSCAT.TABLES where TRIM(TABNAME) = '<some_table_name>'
if this query returns 0 then the table does not exists.

Stored Procedure multi-issue

I have the following tables:
image_sources
character_trait_annotations
character_traits
characters
(and tables that are not pertinent to this problem). I am trying to use a stored procedure that looks for an ImageURL in image_sources where AnnotationID is equal to the one fetched from the code at random. That AnnotatonID belongs to character_trait_annotations table, which also has a CharacterID and a CharacterTraitID. The CharacterID is in characters table, which has CharacterName, and the CharacterTraitID is in character_traits, which has CharacterTraitName.
The code is to call the stored procedure with $character, $trait (which are CharacterName, CharacterTraitName, respectively), and attempt to fetch the ImageURL for it.
The stored procedure
The SQL code for the procedure is as follows:
CREATE PROCEDURE getImageURL(IN charName VARCHAR(35), IN traitName VARCHAR(100))
BEGIN
SELECT ImageSourceURL FROM image_sources WHERE
AnnotationID = (SELECT AnnotationID FROM character_trait_annotations WHERE CharacterID =
(SELECT CharacterID FROM characters WHERE CharacterName = charName) AND CharacterTraitName =
(SELECT CharacterTraitID FROM character_traits WHERE CharacterTraitName = traitName)
);
END;
However, when I call the procedure from PHP, I get this error: Unable to call stored procedure: Unknown column 'CharacterTraitName' in 'where clause'. I was able to create the stored procedure without complaint on the active server.
I check this procedure locally, on my own machine('s version of the database, in phpMyAdmin), by trying to create the stored procedure there to see what is going on, and I get this error: #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 7
I even tried to enclose the column names and the table names with backticks, and it is telling me the same thing. What is wrong with this?
I figured it out; /* by "I", I mean a friend of mine */
OK, so what did you do?
I didn't have access to phpMyAdmin, MySQLConnect, or any other of those database editors, so I had to attempt making one of my own: http://dinotator.biokdd.org/ResearchProject/tableViewer.php . Right now, the only thing that works is the dropdown menu to select the tables.
I didn't have access to this up until today, and when I asked my friend, and he asked for a way to see the database, I gave him that. The problem he pointed out (and I should have seen) was the second condition in the subquery: CharacterTraitName =
(SELECT CharacterTraitID FROM character_traits WHERE CharacterTraitName = traitName) The stupid mistake that was made was trying to check for equality between CharacterTraitName and CharacterTraitID. Furthermore, CharacterTraitName column didn't exist in character_trait_annotations.
echo 'I feel dumb.';

oci_execute(): OCI_SUCCESS_WITH_INFO: ORA-24344 using a trigger/sequence to auto increment an oracle table error

I'm using a sequence and trigger to essentially auto increment a column in a table, however I'm getting an error - ORA-24344: success with compilation error.
I was using this post: How to create id with AUTO_INCREMENT on Oracle? and it worked successfully for two other tables w/ auto increment I made, but there must be something in here I'm not familiar with causing an error.
More edits: Thanks to Polppan we've established that this likely isn't an Oracle issue, rather an OCI with PHP issue. I'm using:
oci_execute($sql);
And as mentioned here (again, thanks Polppan for that link), there's a bit of an issue between EOL characters and oci_execute. It was 11 years ago, so I don't know if that's been patched or not, and I did try his solution but it didn't help. Does anyone know if there are other issues with oci_execute and creating triggers?
Creating the table: (works)
CREATE TABLE RT_documents (
documentID INT NOT NULL,
reviewID varchar2(20) NOT NULL,
file_location CLOB NOT NULL,
version NUMBER(*,3) NOT NULL,
CONSTRAINT RT_documents_pk PRIMARY KEY (documentID)
)
Creating the sequence: (works)
CREATE SEQUENCE rt_documents_seq
Creating/replacing trigger: (doesn't work)
CREATE OR REPLACE TRIGGER rt_documents_bir
BEFORE INSERT ON RT_documents
FOR EACH ROW
BEGIN
SELECT RT_documents_seq.NEXTVAL
INTO :new.documentID
FROM dual;
END;
EDIT: Exact error message as requested - (Note, I'm executing these query-by-query using OCI/Oracle in PHP. PHP tag added just in case, but pretty sure this is an oracle syntax error or something).
Error:
Notice: oci_execute(): OCI_SUCCESS_WITH_INFO: ORA-24344: success with
compilation error in (...)
-I can successfully execute the first two queries, and double checked and the table is there so it worked properly.
Trigger doesn't understand new.id as id doesn't exist in RT_documents table.
Your trigger should be
CREATE OR REPLACE TRIGGER rt_documents_bir
BEFORE INSERT
ON RT_documents
FOR EACH ROW
BEGIN
SELECT RT_documents_seq.NEXTVAL INTO :new.documentID FROM DUAL;
END;
Update
SELECT * FROM v$version;
Oracle Database 10g Enterprise Edition
CREATE TABLE RT_documents
(
documentID INT NOT NULL,
reviewID VARCHAR2 (20) NOT NULL,
file_location CLOB NOT NULL,
version NUMBER (*, 3) NOT NULL,
CONSTRAINT RT_documents_pk PRIMARY KEY (documentID)
);
Table created.
CREATE SEQUENCE rt_documents_seq;
Sequence created.
CREATE OR REPLACE TRIGGER rt_documents_bir
BEFORE INSERT
ON RT_documents
FOR EACH ROW
BEGIN
SELECT RT_documents_seq.NEXTVAL INTO :new.documentID FROM DUAL;
END;
/
Trigger created.
INSERT INTO RT_documents (reviewID, file_location, version)
VALUES ('test', 'test', 1);
1 row created.
SELECT * FROM RT_documents;
DOCUMENTID REVIEWID FILE_LOCATION
VERSION
---------- -------------------- -------------------------------------------
-------------------------------- ----------
1 test test
1
Thanks to Polppan.
The solution was removing the EOL characters. (I did try this but had, without realising, removed the semi-colons, which caused the same error code)
This was a PHP error after all. Using oci_execute, you must remove EOL characters in triggers:
$sql = "CREATE OR REPLACE TRIGGER ......."; //shortened for easy reading
$sql = str_replace(chr(13),'',$sql);
$sql = str_replace(chr(10),'',$sql);
oci_execute($sql);

PHP/MySQLi What happens if a crash occurs during execution of a complex procedure?

I'm developing a php/MySQLi based site. I have a question about what might happen if the page for some reason crashes in the middle of executing a complex procedure.
Example:
if (get_message_call)
{
add something to table 1
add something to table 2
add something to table 3 ( based on some values inserted into table2 )
add something to table 4
etc...
}
I've built the database using PHPmyAdmin (InnoDB). I haven't assigned any relations though since primary/foreign keys didn't work out as in theory (perhaps i dit it wrong).
My main concern is that if the statement for some reason gets aborted (page crash, loose connection etc) in the middle of lets say executing the "add something to table3". I guess that the first and second statement will get executed, and the rest wont?
What is the best way to ensure that this wont be possible to occur since it might mess up the keys etc for the other tables.. i might be a bit fuzzy with the explenation but i hope someone understands my point.
I've read about "Rollbacks and Commits in Stored Procedures and Triggers" but i'm not sure if i understood it right..
Thanks in advance.
This is a case where you should use a database transaction. When you have to update multiple tables, and the updates depend on previous updates, it is safest to put the code into a stored procedure on the database. The stored procedure can start a transaction, which will commit the data to the database only if it finishes correctly (it means that if you write to one table, write to a second table, but the third write fails, the first and second writes are also rolled back).
Here is an example - the stored procedure takes 3 input arguments, returns 2 output values and updates 3 tables. If there is any error, the transaction rolls back. (Note this is just an example, there may be minor syntax errors)
CREATE DEFINER=`root`#`localhost` PROCEDURE `doSomething`(
IN input1 INT,
IN input2 VARCHAR(255),
IN input3 VARCHAR(100),
OUT output1 INT,
OUT output2 INT
)
BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION BEGIN ROLLBACK; END;
DECLARE EXIT HANDLER FOR SQLWARNING BEGIN ROLLBACK; END;
DECLARE EXIT HANDLER FOR NOT FOUND BEGIN ROLLBACK; END;
START TRANSACTION;
INSERT INTO table1 (column1, column2, column3) VALUES (input1, input2, NOW());
SET #new_row_id = LAST_INSERT_ID();
INSERT INTO table2 (column1, colum2) VALUES (#new_row_id, input3);
SET #other_new_row_id = LAST_INSERT_ID();
UPDATE table3 SET mycolumn = #other_new_row_id WHERE id = #new_row_id;
COMMIT;
SET output1 = #new_row_id;
SET output2 = #other_new_row_id;
END
If it's InnoDB, then you should be using transactions for this kind of inserts. For more info see:
http://dev.mysql.com/doc/refman/5.0/en/ansi-diff-transactions.html
http://php.net/manual/en/book.mysqli.php

Resources