This question already has answers here:
Is it possible to insert multiple rows at a time in an SQLite database?
(25 answers)
Closed 4 years ago.
I searched the hinted posts, when posting this question but I foud none similar, so I apologize if I missed any.
Here's my problem. I created a table and it's fields, in SQLite Administrator, then I wrote the following query to populate the table:
USE Contact_Database;
INSERT into Names (Contact_ID, FirstName, LastName) VALUES
(1, 'Joana', 'Tavares'),
(2, 'Patrícia', 'Dias'),
(3, 'Paulo', 'Costa'),
(4, 'Marco', 'Almeida'),
(5, 'Bruno', 'Lindo'),
(6, 'Manuela', 'Lindo'),
(7, 'João', 'Lindo'),
(8, 'Rui', 'Trindade');
Result error: SQL Error: near "USE": syntax error
I've also tried with:
INSERT into Contact_Database.Names
Result error: SQL Error: near ",": syntax error <
What am I missing here?
Multiple inserts in one query are not supported.
See this question for more information.
I had same problem but I solved it while I ignore (delete) the insertion query during creating table.
I changed this
db.execSQL("INSERT INTO MessageTable VALUES(datetime(),'Pir Fahim',00923359110795','Hello,testing');");
to:
db.execSQL("INSERT INTO CallTable VALUES('"+call_time+"','"+name+"','"+phone_no+"','"+interface_style+"','"+tone+"','"+voice_path+"','"+vibration_mode+"');");
Related
I'm new at SQLite and i'm trying to add a column into a table which is exist or not exist with the statement below.
ALTER TABLE
table1
ADD
columnx TEXT NULL
WHERE
type = "table"
i've got the SQLiteException: near "WHERE": syntax error (code 1) error. What is wrong with that statement? What is the correct way of adding new column into a table with checking the existance of the table?
I have an oracle 19c ee database build via their docker image on Oracles github (https://github.com/oracle/docker-images/tree/master/OracleDatabase/SingleInstance). I am trying to follow their example as to how to create a function from here.
I have copied their example exactly. Setup table and data:
CREATE TABLE orders (
customer_id number(10),
order_total NUMBER(11,2)
);
INSERT INTO orders (customer_id, order_total) VALUES (1, 200.01)
The actual function:
CREATE FUNCTION get_bal(acc_no IN NUMBER)
RETURN NUMBER
IS acc_bal NUMBER(11,2);
BEGIN
SELECT order_total
INTO acc_bal
FROM orders
WHERE customer_id = acc_no;
RETURN(acc_bal);
END;
However I keep running into this error when I try and create the function
Query 2 ERROR: ORA-06550: line 5, column 27:
PL/SQL: ORA-00904: "ACC_NO": invalid identifier
ORA-06550: line 2, column 7:
PL/SQL: SQL Statement ignored
ORA-06550: line 6, column 7:
PLS-00372: In a procedure, RETURN statement cannot contain an expression
ORA-06550: line 6, column 7:
PL/SQL: Statement ignored
What am I doing wrong?
The example works for me. You must have mistyped something. Are you sure your function is exactly the same as the one in the manual?
ORA-00904: "ACC_NO": invalid identifier
suggests the declaration acc_bal NUMBER(11,2); is missing or different.
PLS-00372: In a procedure, RETURN statement cannot contain an expression
indicates that your code is a procedure, not a function.
SQL> CREATE TABLE orders (
2 customer_id number(10),
3 order_total NUMBER(11,2)
4 );
Table created
SQL> INSERT INTO orders (customer_id, order_total) VALUES (1, 200.01);
1 row inserted
SQL> CREATE FUNCTION get_bal(acc_no IN NUMBER)
2 RETURN NUMBER
3 IS acc_bal NUMBER(11,2);
4 BEGIN
5 SELECT order_total
6 INTO acc_bal
7 FROM orders
8 WHERE customer_id = acc_no;
9 RETURN(acc_bal);
10 END;
11 /
Function created
SQL> select get_bal(1) from dual;
GET_BAL(1)
----------
200.01
As an aside, while I'm a big fan of the Oracle documentation in general, and this example does neatly illustrate how to create a PL/SQL function, I think it could be improved:
For readability, it's better to give each declaration its own line, so line 3 would be better split into two with acc_bal NUMBER(11,2); on its own line.
The IS and AS keywords are interchangeable here, but surely create ... as (similar to what you might use to create a table or a view) reads better than create ... is.
Understandably, the author didn't want to complicate the example by introducing %type before it had been explained, but a more advanced version would use acc_bal orders.order_total%type; to make acc_bal inherit its datatype from the table column rather than hard-coding it. This goes for all three values used in the function.
The parameter and variable names are OK - they are at least clear - but there is a danger when using the same naming pattern for parameters and variables as for table columns. One day you will type WHERE c.customer_id = customer_id and wonder why you're getting more rows back than you expected. Again it's understandable that the author didn't want to get into that whole discussion in the first example, but it's something to think about. You might use get_bal.acc_no within the function, or use camelCase for parameters and variables, or prefix them with p_ for 'parameter' etc.
A basic rule of layout is that opening and closing keywords such as if/else and begin/end should be left-aligned. The END at line 10 is misaligned under its opening BEGIN. I suppose indenting the entire thing after the first line is a valid personal layout choice, but to me it doesn't add anything.
It's a good idea to leave blank lines around each SQL statement, to avoid a solid wall of text. Personally, I'd prefer a blank line before the RETURN at line 9.
A RETURN clause doesn't require any brackets. The compiler is ignoring the redundant brackets at line 9. I'd lose them.
It's good practice (though optional) to include the procedure/function name in the closing END, so line 10 would become END get_bal;
The COBOL-style uppercase habit is widespread in the industry, but there is no need for it. (PL/SQL's syntax is famously based on Ada, though some also point to ALGOL and PL/1 - nobody ever wrote those in uppercase.) I would improve readability by lowercasing the whole thing.
With these changes, I get this:
create or replace function get_bal
( inAccNo in orders.customer_id%type )
return orders.order_total%type
as
accBal orders.order_total%type;
begin
select order_total into accBal
from orders
where customer_id = inAccNo;
return accBal;
end;
I created a simple table yesterday using the code below:
create table DepartmentYT (
Department_ID int,
Department_Name varchar (255))
I then added the data below to it (which fits in the max character length for the Department name above):
insert into DepartmentYT
values
(1, 'IT'),
(2, 'Sales')
However, when I ran this query today, nothing was returned from the table
select * from DepartmentYT
So I tried to insert the data back in:
insert into DepartmentYT
values
(1, 'IT'),
(2, 'Sales')
But I got an error message that said "String or binary data would be truncated.
The statement has been terminated" which I understand occurs when you're trying to insert data that exceeds the max length specified for the data type.
I then ran this code to check the max length for the characters and noticed that the DepartmentName varachar data type now had a max character length of 1 instead of the 255 that I'd specified yesterday.
select * from information_schema.columns
WHERE TABLE_NAME = 'DepartmentYT'
COLUMN_NAME DATA_TYPE CHARACTER_MAXIMUM_LENGTH
Department_ID int NULL
Department_Name varchar 1
Does anyone know why this may be?
I could drop the table and re-create it, but I'd like to know why the problem may have occurred and if anyone has experienced something similar / may know why it occurred.
Thanks in advance!
Still not sure why the max character length changed, but I used this statement to increase the length
alter table DepartmentYT alter column Department_Name varchar (255) NULL;
then I re-inserted the data (using the statement below) into the table and it worked.
insert into DepartmentYT
values
(1, 'IT'),
(2, 'Sales')
This question already has answers here:
Does SQLite support "delete from from"
(2 answers)
Closed 8 years ago.
I am performing this SQLite command:
DELETE FROM t1027 INNER JOIN translationsmain ON t1027.textid=translationsmain.textid WHERE translationsmain.osb=0
The column "textid" exists both in the table "t1027" and in "translationsmain".
The column "osb" only exists in "translationsmain".
I am getting a syntax error, but I am not sure why.
I am getting a syntax error, but I am not sure why.
Simply, because such syntax is not allowed by SQLite.
Use this syntax, instead:
DELETE FROM t1027 WHERE textid IN (SELECT textid FROM translationsmain WHERE osb = 0)
Oracle 11g is giving me the following error while trying to convert a long datatype to a clob.
I try: select to_lob(long_col_name) from table1.
I get :
[Error] Execution (1: 39): ORA-00932: inconsistent datatypes: expected - got LONG
What am i doing wrong here?
Found the answer here with the help of a colleague:
http://docs.oracle.com/cd/B19306_01/server.102/b14200/functions185.htm
But no idea why this restriction is in place
You can apply this function only to a LONG or LONG RAW column, and only in the select list of a subquery in an INSERT statement.
I suggest a workaround like this, hope this helps to somebody.
SELECT substr(Y.longtoclob,
43 + length('ALIASLONG'),
DBMS_LOB.GETLENGTH(Y.longtoclob) -
2 * (32 + length('ALIASLONG'))) longtoclob
from dual,
(select (dbms_xmlgen.getxml('SELECT t.column_long ALIASLONG
FROM TABLE_LONG_CLOB t WHERE t.id = 2')) longtoclob
from dual) Y where DBMS_LOB.GETLENGTH(Y.longtoclob) > 0
You can't directly fetch LONG to LOB. You might want to convert it to VARCHAR2 first