Running the same unmodified query in SQL Server and Oracle - sql

I want to run a query on both Oracle and SQL Server. The problem I have is that the query inserts into a column called PERCENT which I believe is a keyword in SQL Server.
A straight insert like this fails on SQL Server
INSERT INTO testtable
(PERCENT,VALUE)
VALUES
(50,'test');
To overcome the above SQL Server allows it if it is changed to one of the following
INSERT INTO testtable
([PERCENT],[VALUE])
VALUES
(50,'test');
INSERT INTO testtable
("PERCENT","VALUE")
VALUES
(50,'test');
The problem now is that Oracle does not support any of the above formats. Oracle only allows this format:
INSERT INTO testtable
(PERCENT,VALUE)
VALUES
(50,'test');
Is there a way I can run the above query in both Oracle and SQL Server without any problems?

Actually Oracle does support this format:
insert into testtable("PERCENT","VALUE") values(50,'test');
Here is a direct paste from my SQL Plus session:
SQL> create table testtable (percent number, value varchar2(20));
Table created.
SQL> insert into testtable ("PERCENT", "VALUE") values (50, 'test');
1 row created.

Related

Can SQL before insert triggers access the values of an INSERT statement? [duplicate]

I am trying to create a trigger in mysql that will insert in to another table when an insert on a table occurs.
CREATE TRIGGER addCardForNewUser AFTER INSERT ON swiped.Users
FOR EACH ROW
BEGIN
INSERT INTO swiped.Card (userid) VALUES (get value from original insert here);
END
In the values part of the insert statement how would i get a value from the original insert to use here?
Thanks
You can use those values with new.columnname. If the name of the column is userid too, then you can use:
CREATE TRIGGER addCardForNewUser AFTER INSERT ON swiped.Users
FOR EACH ROW
BEGIN
INSERT INTO swiped.Card (userid) VALUES (new.userid);
END
From the official documentation:
Within the trigger body, the OLD and NEW keywords enable you to access
columns in the rows affected by a trigger. OLD and NEW are MySQL
extensions to triggers; they are not case sensitive.
In an INSERT trigger, only NEW.col_name can be used

INSERT error in Oracle

I am working with oracle (with Toad) but when I try to execute this query
INSERT INTO KEYUSER(NAME) VALUES(UNAME) WHERE ID = 1;
I get this error:
SQL command not properly ended. Found 'WHERE' expecting ;-or- LOG -or- RETURN RETURNING
What is wrong? Regards.
You can never use where clause in insert statement. It does not make any sense to have it while you are simply trying to insert a row.
General Insert statement is like below
INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);
So in your case you have to remove where clause from your statement and query will be like below
INSERT INTO KEYUSER(NAME) VALUES('UNAME');
Presumably, you intend an UPDATE rather than INSERT:
UPDATE KEYUSER
SET NAME = UNAME
WHERE ID = 1;
Sometimes new users of SQL get confused between INSERT and UPDATE. INSERT inserts new rows into a table. UPDATE changes values in existing rows. The confusion is become some people interpret INSERT as "inserting new values into a row".

Advance Replace in insert scripts

I have below 2 insert statements which i took the export from sql developer from dev environment. I have delete those records from dev afterwards. Now i want to run this insert statement again in dev because those are my back up but i am getting error as virtual column which is ORD_DAYID cannot be used inside insert script. So i want to exclude this column and also the respective values using replace function or any tools which i dont know. I didnt know previously that i have virtual column for this table. I would like to know is there any tool or function where i can select ORD_DAYID and also the respective values get selected and then i can delete those and then i can be able to run this insert statement again in test enviornment.
P.S i have mentioned only 2 sample insert statements but there are 1000 insert statements. So its very difficult to manually delete this ORD_DAYID from this insert statements with respective values.
Insert into test_ord (IS_GRP,ORD_DAYID,REF_CAMPA_CODE) values (1,20150813,null);
Insert into test_ord (IS_GRP,ORD_DAYID,REF_CAMPA_CODE) values (1,20150828,null);
You can edit your INSERT statements using regular expressions, in an editor such as Notepad++.
So to change this ...
Insert into test_ord (IS_GRP,ORD_DAYID,REF_CAMPA_CODE) values (1,20150813,null);
... into this ...
Insert into test_ord (IS_GRP,REF_CAMPA_CODE) values (1,null);
You need a search pattern of:
Insert into test_ord \(IS_GRP,ORD_DAYID,REF_CAMPA_CODE\) values \(([0-9]+),([0-9]+),null\);
and a replace pattern of:
Insert into test_ord \(IS_GRP,REF_CAMPA_CODE\) values \(\1,null\);
Obviously you will need to refine the search pattern to cater for all the different values of IS_GRP, and REF_CAMPA_CODE in your 1000 statements.
" is there any way where we can count the place of column and value and replace it with null"
No. The snag with virtual columns is that they cannot be referenced in INSERT or UPDATE statements. So you need to totally exclude it from the projection.
"i am not able to find those option in notepad++"
Really? Search and replace is not an exotic option:
From the menu: Search > Find > Replace [tab] (or [ctrl]+h)
As the search mode select the regular expression radio button
create an auxiliary table without virtual columns.
Restore your data to this auxiliary table.
Transfer the data from the auxiliary table to the original table.
-- this is your table
create table mytab(A number, b number, s as (a+b));
--fill it with data
insert into mytab(a,b) values(1,1);
insert into mytab(a,b) values(1,2);
insert into mytab(a,b) values(2,1);
insert into mytab(a,b) values(2,2);
commit;
-- check its content
select * from mytab;
-- now delete the rows
delete from mytab;
commit;
-- restore your data
--------------------
-- create a table similar the table you want to restore
-- but the virtual colums as regular columns.
create table ctas as
select * from mytab where 1!=0;
-- insert your backup data
insert into ctas(a,b,s) values(1,1,2);
insert into ctas(a,b,s) values(1,2,3);
insert into ctas(a,b,s) values(2,1,3);
insert into ctas(a,b,s) values(2,2,4);
commit;
-- transfer the data to the table you want to restore
insert into mytab(a,b) select a,b from ctas;

ORACLE TRIGGER INSERT INTO ... (SELECT * ...)

Trigger with Insert into (select * ...)
I'm trying it.
INSERT INTO T_ USERS SELECT * FROM USERS WHERE ID = :new.ID;
not working...
this work.
INSERT INTO T_USERS(ID) VALUES(:new.ID);
Trigger
create or replace trigger "TRI_USER"
AFTER
insert on "USER"
for each row
begin
INSERT INTO T_USER SELECT * FROM USER WHERE ID = :new.ID;
end;​
this work.
INSERT INTO T_USERS(ID) VALUES(:new.ID);
So if it fits to you then try this:
INSERT INTO T_USER(ID) SELECT ID FROM USER WHERE ID = :new.ID;
If you want to select one or more rows from another table, you have to use this syntax:
insert into <table>(<col1>,<col2>,...,<coln>)
select <col1>,<col2>,...,<coln>
from ...;
Perhaps you could post the actual error you are experiencing?
Also, I suggest that you rethink your approach. Triggers that contain DML introduce all sorts of issues. Keep in mind that Oracle Database may need to restart a trigger, and could therefore execute your DML multiple times for a particular row.
Instead, put all your related DML statements together in a PL/SQL procedure and invoke that.
Its not about your trigger but because of INSERT statement
here insert statement works as below
INSERT INTO <TABLE>(COL1,COL2,COL3) VALUES (VAL1,VAL2,VAL3); --> If trying to populate value 1 by one.
INSERT INTO <TABLE>(COL1,COL2,COL3) --> If trying to insert mult vales at a time
SELECT VAL1,VAL2,VAL3 FROM <TABLE2>;
The number of values should match with number of columsn mentioned.
Hope this helps you to understand

How to insert chinese characters in sql server database using insert query in stored procedure?

Chinese characters are inserting as '??' in table.
I have tried like this but its not working.
CREATE PROCEDURE [Sp_InsertPersonalInformation]
(
#NameIn_Chinese NVARCHAR(100)=NULL
)
AS
BEGIN
INSERT INTO tbl_PersonalInformation(ChineseName) VALUES (N''+#NameIn_Chinese+'')
END
The below query is working. but through stored procedure i'm not able to insert in a correct format.
INSERT INTO TBL_PERSONALINFORMATION(ChineseName) VALUES (N'你好')