derby sql insert - sql

I'm trying to insert values in my database
This is the statement I try to execute:
insert into leverancier (id,naam,straat,nr,postcode,plaats,telefoon)
values (1,"stef","bosstraat",88,9240,"Zele",null);
I get the following error:
ERROR 42X04: Column 'stef' is either not in any table in the FROM list or appears within a join specification and is outside the scope of the join specification or appears in a HAVING clause and is not in the GROUP BY list. If this is a CREATE or ALTER TABLE statement then 'stef' is not a column in the target table.
What is the problem?

To insert a string, like "stef", don't use double quotes but single quotes: 'stef'. Here's how the statement should be:
INSERT INTO leverancier
(id, naam, straat, nr, postcode, plaats, telefoon)
VALUES
(1,'stef', 'bosstraat', 88, 9240, 'Zele', NULL);
The error you get Column 'stef' is either not in any table ... is because double quotes are used for table and column names. So reading "stef", the parser assumes you are referring to a column named stef.

Related

Why I am getting Insert has more target columns than expressions

I have a simple table called source with 3 columns (source_id, name, tenant_id) and some data in it.
I a trying to insert a data into the table by checking its existence in the table. But I am getting this error ..any idea how to resolve
The Query -
INSERT INTO moa.source(source_id, name, tenant_id)
SELECT ((select max(source_id)+1 from moa.source), 'GE OWS - LHR', 1)
WHERE NOT EXISTS(SELECT 1 FROM moa.source where name = 'GE OWS - LHR');
The Error :
ERROR: INSERT has more target columns than expressions
LINE 1: INSERT INTO moa.source(source_id, name, tenant_id)
^
HINT: The insertion source is a row expression containing the same number of columns expected by the INSERT. Did you accidentally use extra parentheses?
SQL state: 42
The Table:
Sorry..figured it out..there should be no paranthesis after Select

Inserting a SQL statement as a record value into a PostgreSQL column

I work with an application that uses PostgreSQL 10.7. Part of the application allows you to bundle a group of of database objects and SQL statements into a package that you can later run when creating a Dev environment.
Each object and SQL statement has its own record in the database. I have to create over 1000 records so I am trying to create a script that will insert the SQL statements into the database for me.
I created my script but I am getting an error once Postgres sees the second "Value" command that is part of the record I am trying to insert.
Here is an example of what I am trying to do:
````insert into MY_TABLE
( NAME,
SQL_STMT,
ADDED_BY,
DATE_ADDED )
values
( 'package_1',
'INSERT INTO TABLE_1(NAME, OBJECT_ID, ORDER_NUMBER) VALUES
'LCMSMS','PEST',1);'
'CJONES',
'9/11/2019' );````
I am expecting it to be inserted but I am getting the following error. Can anyone guide me on how to "insert my insert statement"?
LINE 8: ...NAME,SQL_STMT,ADDED_BY,DATE_ADDED) VALUES ('LCMSMS...````
Your SQL statement contains emmbedded quotes, that clash with the surrounding quotes. You would need to double these quotes, like:
````insert into MY_TABLE
( NAME,
SQL_STMT,
ADDED_BY,
DATE_ADDED )
values
( 'package_1',
'INSERT INTO TABLE_1(NAME, OBJECT_ID, ORDER_NUMBER) VALUES (''LCMSMS'', ''PEST'', 1);'
'CJONES',
'9/11/2019' );````
As commented by mu is too short, another solution would be to use Postgres dollar quoting syntax. This saves you the effort of double quoting each and every embedded quote:
````insert into MY_TABLE
( NAME,
SQL_STMT,
ADDED_BY,
DATE_ADDED )
values
( 'package_1',
$$INSERT INTO TABLE_1(NAME, OBJECT_ID, ORDER_NUMBER) VALUES ('LCMSMS', 'PEST', 1);$$
'CJONES',
'9/11/2019' );````

Selecting rows where cells contain multiple values

Using Oracle SQL Developer 1.5.5.
I have a data table and I'm trying to select rows from the table (TABLENAME) where a column (COLUMN5) in the table contains multiple values. For example using the following command:
select * from TABLENAME where COLUMN5=''101','221','429''
I'm doing this because I want to select all rows in the table where the cell values in column 5 are '101','221',229'
An added complication is that in the table each of the values is within single quotation marks, separated by a comma. When run the above command I get an error message which says " SQL command not ended properly" I think this is something to do with the single quote marks and the multiple values because when i run the following command to select rows when column 5 is '443', I get a result:
select * from TABLENAME where COLUMN5='''443'''
Note that these values of '443' have single quotation marks around them in the table.
My question is what command will allow me to select rows with the multiple values in column 5? Help!
Let me know if you require any clarification
I think you can use FIND_IN_SET like below:
select * from accounts where FIND_IN_SET('101','101,102')
FIND_IN_SET ignore the single quotes
you just need to escape the quotes
create table so_col5 (a integer, col5 varchar2(25));
insert into so_col5 values (1, 'abcd');
insert into so_col5 values (2, 'abcd');
insert into so_col5 values (3, 'abcd');
Insert into SO_COL5 (A,COL5) values (4,'''101'',''221'',''229''');
commit;
select * from so_col5 where col5 = '''101'',''221'',''229''';
It took me a few tries, testing selecting the string from dual, but then I remembered I could just cheat - and export the result from the GRID as an INSERT Script and copy/paste the quoted string for that value and use that in my SELECT.
You're using v1.5, which is ANCIENT. Highly suggest you upgrade for an optimal experience.
You can also use the Q function (docs) which you can pass a string and Oracle handles the quote escaping for you. Here's a scenario you can play with on LiveSQL.

SQL How to insert null value

I want to insert data into one table from another table. In some of the columns I don't have data, so I want to set column to null. I don't know how I should do this?
This is the SQL:
INSERT INTO _21Appoint(
PCUCODE,PID,SEQ,
DATE_SERV,APDATE,
APTYPE,APDIAG,D_UPDATE,CID
) SELECT (
NULL,NULL,NULL,
treatment_date,appointment_date,
typeap_id,appointment_id,NULL,patient_id
) FROM cmu_treatment,cmu_appointment
WHERE cmu_treatment.treatment_id LIKE cmu_appointment.treatment_id;
Your insert is essentially correct. Just don't put the column list in parentheses:
INSERT INTO _21Appoint
(PCUCODE,PID,SEQ,DATE_SERV,APDATE,APTYPE,APDIAG,D_UPDATE,CID)
SELECT NULL,NULL,NULL,treatment_date,appointment_date,typeap_id,appointment_id,NULL,patient_id
FROM cmu_treatment,cmu_appointment
WHERE cmu_treatment.treatment_id LIKE cmu_appointment.treatment_id;
In Postgres (unlike other DBMS) putting a column list in parentheses makes the result a single "record", rather then individual columns. And therefore the select only returns a single column, not multiples and thus it doesn't match the column list for the insert
another option is to simply leave out the columns completely:
INSERT INTO _21Appoint
(DATE_SERV,APDATE,APTYPE,APDIAG,CID)
SELECT treatment_date,appointment_date,typeap_id,appointment_id,patient_id
FROM cmu_treatment,cmu_appointment
WHERE cmu_treatment.treatment_id LIKE cmu_appointment.treatment_id;

To insert single quote values into the table using "insert into"

I tried to insert some data from one table (tableA)
into another table (tableB) using bulk insert but cannot succeeded because the tableA data has got one value-> BERMUDA 23''-24''
when it tries to enter this value it raise error as
String or binary data would be truncated.
My query is
insert into tableA
select size from tableB
i try with replace the single quote with empty space but as its size we should enter the data with single quote only.
Please suggest the way out.
you can remove single quote with replace function
insert into tableA
select replace(size,'''','') from tableB