What i want is to store java statement with a semi colon in my embedded derby database. However i am not quite able to generate the simple SQL insert statement that would work. I believe that when the control encounters the semi colon statement in the string if considers it as the termination character and errors are thrown. Try the below:
create table dummy(keys int, vals varchar(255));
insert into dummy values (10,'System.out.println();');
The above statement fails with errors. However if i remove the semi colon it works
insert into dummy values (10,'System.out.println()');
Has anyone seen this before if so which escape character did you use.
Thanks.
It may help to see what context you are trying to execute the sql statement in, i.e. the surrounding code. If this is in a java program, something like this may work:
int n = stmt.executeUpdate("insert into dummy values (10, 'System.out.println()$;') {ESCAPE '$'}");
Your statements work fine when I run them using Derby's "ij" sql editor:
ij version 10.12
ij> connect 'jdbc:derby:tt;create=true';
ij> create table dummy(keys int, vals varchar(255));
0 rows inserted/updated/deleted
ij> insert into dummy values (10,'System.out.println();');
1 row inserted/updated/deleted
ij> select * from dummy;
KEYS |VALS
--------------------------------------------------------------------------------
------------------------------------------------------------
10 |System.out.println();
1 row selected
Perhaps the problem lies elsewhere. If you could include more details about the exact message you get, that might help.
Related
My goal is to insert a new row in a database table and immediately get back the ID of the inserted row.
I saw some other posts on stackoverflow that suggested to use this command:
INSERT INTO selecttable (name) OUTPUT INSERTED.ID VALUES ('aName')
Unfortunately, the execution of this command fails with the following error message:
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'OUTPUT INSERTED.ID VALUES ('aName')'
The execution of the command without the output statement worked well:
INSERT INTO selecttable (name) VALUES ('aName')
Do you have an idea, why the command above is not working?
In general, I think you use LAST_INSERT_ID():
INSERT INTO selecttable (name)
VALUES ('aName');
SELECT LAST_INSERT_ID();
This is an imperfect equivalent construct. For instance, it doesn't work so well with multiple rows. But it is the MariaDB/MySQL solution to this problem.
I'm trying to make a PROCEDURE that makes it easier to store changes users make in their settings (like a server log, you know right) into a table user_settings_changelog. I finally made the PROCEDURE acceptable so my sql program (HeidiSQL) would store it. Now the problem is this: how to properly EXECUTE it. I tried this multiple times with multiple lines of code, but it seemed nothing worked. Can anyone help me out here?
The PROCEDURE query:
DELIMITER $$
CREATE PROCEDURE setting_adjustment_log (name_setting VARCHAR(45),
changed_from VARCHAR(45), changed_to VARCHAR(45), username
VARCHAR(45))
BEGIN
INSERT INTO user_settings_changelog
VALUES (GETDATE(), name_setting, changed_from, changed_to,
username);
END$$
The table user_settings_changelog has 5 columns: date DATETIME, name_setting VARCHAR(45), changed_from VARCHAR(45), changed_to VARCHAR(45) and username VARCHAR(45).
The EXECUTE query:
EXECUTE setting_adjustment_log ('background','black','white','TheCoderNoob');
The error HeidiSQL gives me:
SQL 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 '('background','black','white','TheCoderNoob')' at line 1
Maybe useful to mention: I am using a version of USBwebserver from a few years ago.
EDIT: I've been looking at the EXECUTE/CALL query in MySQL Workbench for a while, it seems like the database expects something between the name of the procedure and the given data or something. When I hover over it, it reads:
Syntax error: 'background' (single quoted text) is not a valid input at this position
First, you should always include the column names in an insert statement. (There might be a few exceptions, but if you are learning SQL, then sticks with best practices.)
Second, distinguish the input parameters from possible column names.
Third, your code looks like MySQL, so use NOW() or a similar function:
DELIMITER $$
CREATE PROCEDURE setting_adjustment_log (
in_name_setting VARCHAR(45),
in_changed_from VARCHAR(45),
in_changed_to VARCHAR(45),
in_username VARCHAR(45)
)
BEGIN
INSERT INTO user_settings_changelog (date, name_setting, changed_from, changed_to, username)
VALUES (NOW(), in_name_setting, in_changed_from, in_changed_to,
in_username);
END$$
When you call the stored procedure, use call:
CALL setting_adjustment_log('background', 'black', 'white', 'TheCoderNoob');
Couldn't get the right approach to automatically set the id value in my table with Oracle. Here's my trigger:
create or replace trigger themes_on_insert
before insert on THEME
for each row
begin
:new.ID := THEMES_SEQ.nextval;
end;
I have two columns: id, description. But when i call insert
insert into theme values('Sport');
it gives me
Error report:
SQL Error: ORA-00947: not enough values
00947. 00000 - "not enough values"
You do not specify the columns you are supplying values for, so the database expects a value for every column in your table.
Assuming you have a column name in that table, you need to do this:
insert into theme (name) values('Sport');
Not listing the target columns in an INSERT statement is bad coding style.
Just make sure to name the columns you're specifying the values for.
insert into theme (description) values('Sport');
Well if you do not specify the colnames, in your insert statement, the databse will assume that you give the values for all the column.
So :
insert into theme (col_name) values ('Sport');
In case Trigger is not mandatory,You can simply use the following
insert into theme values(THEMES_SEQ.nextval,'Sport');
Check the columns count while inserting.
I am having trouble with the following problem, Just wondering if you could give me any guidance on how i would start this.
Write a trigger to capture an input before it's inserted into a table
so that the value of the insert will be modified to be appended with
the text -previous
Say I have a table called StaffDetails and i could insert info such as ID, How would i apply it to the above problem.
Thank You
Create Trigger trg_name
on StaffDetails
instead of insert
referencing new as new_row
as
begin
#new_row.ID = (#new_row.ID + '_previous')
insert into StaffDetails (.....) values (#new_row.ID, .....)
end
it would probably look something like this, maybe youll have to change some syntax because its not the same for mysql and sql server, for detailed information on syntax for sql server you can look at https://msdn.microsoft.com/en-us/library/ms189799.aspx
This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Get ID of last inserted record in oracle db
I am brand new to oracle, having used SQL Server in the past. I have a stored procedure, and I am trying to do two INSERTs one after the other. The second INSERT requires the ID of the first INSERT. Could someone explain how to do it?
I'm seeing stuff about SEQUENCEs and nextvalue/curval ?
I guess in SQL Server I'd just declare a variable, and use SCOPE_IDENTITY, so I'd be looking to do that.
There are two possibilities. If you are using sequences for number generation, you can get the value from the sequence, like this:
select SequenceName.currval into AVariable from dual
or this
AVariable := SequenceName.currval;
But it's probably better to use the returning clause like this:
declare
AVariable int;
begin
insert into yourtable(columns)
values (values)
returning id into AVariable;
insert into anotherTable(columns)
values(AVariable, othervalues);
end;
This way, it will work regardless of implementation details. You don't have to know the name of the sequence, and it will also work with the later introduced identity columns.
The only thing it won't work for is views with an instead of trigger, but that's a special case altogether.
There are a few approaches. One option is to use the RETURNING clause, i.e.
DECLARE
l_generated_id INTEGER;
BEGIN
INSERT INTO table_name( <<column list>> )
VALUES( <<values list>>
RETURNING <<name of primary key column>>
INTO l_generated_id;
INSERT INTO other_table( <<column list>> )
VALUES( l_generated_id, <<other values>> );
END;
If you know that the primary key is populated via a sequence (with or without a trigger) and you know the name of that sequence, you can use the sequence_name.currval in your second INSERT statement (the first INSERT statement would, either directly or via a trigger reference the sequence_name.nextval to generate the new primary key).