I have MariaDB and Oracle databases. I have set up ODBC Connect between the two, so that I can access Oracle from MariaDB.
I can do the following from MariaDB:
CREATE TABLE oracopy ENGINE=connect TABLE_TYPE=ODBC tabname='testtab' CONNECTION='DSN=ORCL';
This creates a table locally.
What I really want to do however is run a Query on the remote Oracle and return the results to the MariaDB session.
The query would be Oracle speficic, i.e. might containing ORACLE functions like DECODE. Also the query could include a PLSQL function call which again would need to be run on Oracle. E.g:
SELECT t.id, DECODE( t.typ,'HH', 'Val 1', 'Val 2' ) tt,
my_package.fn_test ( t.dob ) dob
FROM testtab t;
Does MariaDB have a "run this query on XXX remote database".
Consider using the source definition argument, SRCDEF, as shown in docs.
CREATE TABLE oracopy ENGINE=connect TABLE_TYPE=ODBC CONNECTION="DSN=ORCL"
SRCDEF="SELECT t.id, DECODE( t.typ,'HH', 'Val 1', 'Val 2' ) tt,
my_package.fn_test ( t.dob ) dob
FROM testtab t;"
Related
Running Pro*C on Oracle 10g.
I am looking to do a subquery within an insert statement values clause. This sql query is fully valid and runs within TOAD with no problems, but Pro*C fails to parse the query.
EXEC SQL INSERT INTO TARGET_ATTACHMENT
(
TARGET_ID
FILENAME
)
VALUES (
:targetID,
( SELECT CREATED_FLAG from TARGET t where t.TARGET_ID = :targetID ) || '.tif'
)
If I remove:
( SELECT (CREATED_FLAG || DISPLAY_ID) from TARGET t where t.TARGET_ID = :targetID ) ||**".
The Pro*C compiler works and everything compiles and runs as expected.
If I DO NOT remove:
The Pro*C compiler throws a syntax error.
1>Syntax error at line 128, column 12, file d:\SVN\...\TA.pc:
1>Error at line 128, column 12 in file d:\SVN\...
1>...\TA.pc
1> ( select CREATED_FLAG from target t where t.TARGET_ID = :targetID )
1>...........1
1>PCC-S-02201, Encountered the symbol "CREATED_FLAG" when expecting one of the fol
1>lowing:
1> ( ) * + - / . # | at, day, hour, minute, month, second, year,
This is a problem, as I expect Pro*C to be able to compile subquerys within a values caluse:
ie.
INSERT into table1 (col1) values ( (select t2.singleCol from table2 t2 where t2.priKey = :priKey) )
Is this expected behaviour of Pro*C? or Should it support subqueries within the values clause?
Possibly change the subquery to:
( SELECT CREATED_FLAG || '.tif' from TARGET t where t.TARGET_ID = :targetID )
I dont think I have ever seen something appended to a subquery the way you were attempting.
The amount of SQL the Pro*C preprocessor is able to parse in static SQL statements is quite limited. For example it can't even parse explicit inner joiner/outer left join etc. notation.
As a workaround you can just prepare a dynamic SQL statement and execute it - even if your SQL statement is not really dynamic.
The code you have posted is logically identical to this:
INSERT INTO TARGET_ATTACHMENT
( TARGET_ID , FILENAME )
select :targetID, CREATED_FLAG|| '.tif'
from TARGET t
where t.TARGET_ID = :targetID )
Is there a particular reason why you need to use scalar cursors in a VALUES clause?
I am using following method for inserting multiple rows using a single INSERT statement, that is the ANSI style of inserting rows. It is available in SQL Server 2008 and 2012. I am not sure of SQL Server 2005/ 2000.
Create test table:
create table TestInsert (ID INT, Name NVARCHAR(50))
Single INSERT statement to insert 5 rows
INSERT INTO TestInsert
VALUES (1,'a'),
(2,'b'),
(3,'c'),
(4,'d'),
(5,'e')
Please let me know if there is any other best way to achieve this
Try this instead:
INSERT TestInsert
SELECT 1, 'a'
UNION ALL
SELECT 2, 'b'
UNION ALL
SELECT 3, 'c'
UNION ALL
SELECT 4, 'd'
UNION ALL
SELECT 5, 'e'
SQL Server - inserting multiple rows with single (ANSI style) statement
For SQL Server 2000+
According to SQL The Complete Reference, Third Edition (August 12, 2009):
1) The syntax for multirow INSERTs is
INSERT INTO table-name (columns not mandatory)
query
(page 236, Figure 10-3).
2) The SELECT statement has the FROM clause mandatory (page 87, Figure 6-1).
So, in this case, to insert multiple rows using just one INSERT statement we need an auxiliary table with just one row:
CREATE TABLE dual(value INT PRIMARY KEY CHECK(value = 1))
INSERT dual(value) VALUES(1)
and then
INSERT INTO table-name (columns) -- the columns are not mandatory
SELECT values FROM dual
UNION ALL
SELECT another-values FROM dual
UNION ALL
SELECT another-values FROM dual
Edit 2: For SQL Server 2008+
Starting with SQL Server 2008 we can use row constructors: (values for row 1), (values for row 2), (values for row 3), etc. (page 218).
So,
INSERT INTO TestInsert
VALUES (1,'a'), --The string delimiter is ' not ‘...’
(2,'b'),
(3,'c'),
(4,'d'),
(5,'e')
will work on SQL Server 2008+.
So I have some Oracle TIMESTAMP in a SQL dump from my Oracle database. And I want to import that into the Derby database. In Oracle, the SQL statement is looking like:
Insert into TABLE_NAME (COL1, COL2, COL3)
values (
'blah',
to_timestamp('17-MAR-11 15.52.25.000000000','DD-MON-RR HH24.MI.SS.FF'),
'blah'
);
COL2 has type TIMESTAMP here. When I run it in Derby, I predictably get "Error: 'TO_TIMESTAMP' is not recognized as a function or procedure."
So how do I insert a TIMESTAMP in Derby, and more specifically, how would I convert the above SQL statement to a SQL statement which is executable in Derby?
As Derby is a Java database you will be accessing it through JDBC, therefor you can use the JDBC escape format to specify a timestamp value:
INSERT INTO TABLE_NAME (COL1, COL2, COL3)
VALUES ('blah', {ts '2011-03-17 15:52:25'}, 'blah');
But it will require you to reformat the literal value into ISO format.
The above statement would work with Oracle as well, if run through a JDBC tool.
I have situations that I need to write multiple rows of the same value to setup some tables. Say I have to add 120 rows with two columns populated. I am looking for a shortcut, instead of having the Insert line repeated n times. How to do this?
In SQL Server Management Studio, you can use the "GO" keyword with a parameter:
INSERT INTO YourTable(col1, col2, ...., colN)
VALUES(1, 'test', ....., 25)
GO 120
But that works only in Mgmt Studio (it's not a proper T-SQL command - it's a Mgmt Studio command word).
Marc
How about
Insert Table( colsnames )
Select Top 120 #value1, #Value2, etc.
From AnyTableWithMoreThan120Rows
Just make sure the types of the values in the #Value list matches the colNames List
what about
insert into tbl1
(col1,col2)
(select top 120 #value1,#value2 from tbl2)
if in sql server 2008 . new in sql server 2008 to insert into a table multiple rows in a single query .
insert into tbl1
(col1,col2)
values
(#value1,#value2),(#value1,#value2),.....(#value1,#value2)
Put the values in an unused table for safe keeping. From there you can insert from this table to the tables you need to setup.
Create an Excel Spreadsheet with your data.
Import the speadsheet into Sql Server.
You can even try with something like this(just an example)
declare #tbl table(col1 varchar(20),col2 varchar(20))
; with generateRows_cte as
(
select
1 as MyRows
union all
select
MyRows+1
from generateRows_cte
where MyRows < 120
)
insert into #tbl(col1,col2)
select
'col1' + CAST(MyRows as varchar),'col2' + CAST(MyRows as varchar)
from generateRows_cte OPTION (MAXRECURSION 0)
select * from #tbl
Note:- Why not you are trying with Bulk insert into SqlServer from a dataset ? I didnot notice first that u have a front end too(VB)!
Using Informix, I've created a tempory table which I am trying to populate from a select statement. After this, I want to do an update, to populate more fields in the tempory table.
So I'm doing something like;
create temp table _results (group_ser int, item_ser int, restype char(4));
insert into _results (group_ser, item_ser)
select
group_ser, item_ser, null
from
sometable
But you can't select null.
For example;
select first 1 current from systables
works but
select first 1 null from systables
fails!
(Don't get me started on why I can't just do a SQL Server like "select current" with no table specified!)
You don't have to write a stored procedure; you simply have to tell IDS what type the NULL is. Assuming you are not using IDS 7.31 (which does not support any cast notation), you can write:
SELECT NULL::INTEGER FROM dual;
SELECT CAST(NULL AS INTEGER) FROM dual;
And, if you don't have dual as a table (you probably don't), you can do one of a few things:
CREATE SYNONYM dual FOR sysmaster:"informix".sysdual;
The 'sysdual' table was added relatively recently (IDS 11.10, IIRC), so if you are using an older version, it won't exist. The following works with any version of IDS - it's what I use.
-- #(#)$Id: dual.sql,v 2.1 2004/11/01 18:16:32 jleffler Exp $
-- Create table DUAL - structurally equivalent to Oracle's similarly named table.
-- It contains one row of data.
CREATE TABLE dual
(
dummy CHAR(1) DEFAULT 'x' NOT NULL CHECK (dummy = 'x') PRIMARY KEY
) EXTENT SIZE 8 NEXT SIZE 8;
INSERT INTO dual VALUES('x');
REVOKE ALL ON dual FROM PUBLIC;
GRANT SELECT ON dual TO PUBLIC;
Idiomatically, if you are going to SELECT from Systables to get a single row, you should include 'WHERE tabid = 1'; this is the entry for Systables itself, and if it is missing, the fact that your SELECT statement does return any data is the least of your troubles. (I've never seen that as an error, though.)
This page says the reason you can't do that is because "NULL" doesn't have a type. So, the workaround is to create a sproc that simply returns NULL in the type you want.
That sounds like a pretty bad solution to me though. Maybe you could create a variable in your script, set it to null, then select that variable instead? Something like this:
DEFINE dummy INT;
LET dummy = NULL;
SELECT group_ser, item_ser, dummy
FROM sometable
SELECT group_ser, item_ser, replace(null,null) as my_null_column
FROM sometable
or you can use nvl(null,null) to return a null for your select statement.
Is there any reason to go for an actual table? I have been using
select blah from table(set{1})
select blah from table(set{1})
is nice when you are using 10.x database. This statement doesn't touch database. The amount of read/write operations is equal to 0,
but
when you're using 11.x it will cost you at least 4500 buffer reads because this version of Informix creates this table in memory and executes query against it.
select to_date(null) from table;
This works when I want to get a date with null value
You can use this expression (''+1) on the SELECT list, instead of null keyword. It evaluates to NULL value of type DECIMAL(2,0).
This (''+1.0001) evaluates to DECIMAL(16,4). And so on.
If you want DATE type use DATE(''+1) to get null value of type DATE.
(''+1)||' ' evaluates to an empty string of type VARCHAR(1).
To obtain NULL value of type VARCHAR(1) use this expression:
DATE(''+1)||' '
Works in 9.x and 11.x.