OpenQuery for SQL Query - oracle11g

Kindly help me writing below query in openquery.Thanks in advance
INSERT INTO Tablename
SELECT * FROM tablename1 WHERE insertionorderid IN (
SELECT orderid FROM temp_table2)

If you are trying to insert into SQL Server the syntax would be
INSERT INTO dbo.[YOURTABLE] SELECT * FROM [MATCHING_TABLE] WHERE CLAUSE
The caveat here is that the two tables must have identical schemas or you will have to explicitly define the columns. Also this will not work properly for tables with identity columns

Related

Does SQLScript for SAP HANA support the use of INSERT with CTEs (Common Table Expressions)?

I know this isn't a specific bit of code or problem, but I am having trouble with a very similar issue to the person asking this (except theirs is for SQL Server): Combining INSERT INTO and WITH/CTE ...and I can't seem to find it out there on any SAP HANA help forums etc. so thought there may be an expert on here who can just give me a simple yes or no answer.
The SQL statement I am using contains multiple CTEs, but when I try to insert it tells me there is a Syntax error around the word INSERT. It is definitely laid out exactly the same as in the question I've linked above (spent hours checking), and I can post code samples if necessary but I simply want to know whether it is supported first! Thanks
Short answer:
No, CTEs are not supported for INSERT/UPDATE statements.
Longer answer:
SQLScript's INSERT/UPDATE commands are actually "borrowed" SQL commands as the documentation explains.
Checking the documentation for SQL INSERT we find that it supports a subquery as a source of values.
The subquery term is defined as part of the SQL SELECT statement. Checking the documentation for SELECT shows that <subquery> and <with_clause> are different, non-overlapping terms.
This means, that CTEs cannot be used in subqueries and therefore not be part of the subqueries used in INSERT/UPDATE commands.
You can, however, use SQLScript table variables in INSERT statements in your SQLScript blocks, which is very similar to CTEs:
DO BEGIN
te_a := SELECT 10, 'xyz' as VAL from dummy;
te_b := SELECT 20, 'abc' as VAL from dummy;
te_all := SELECT * from :te_a
UNION ALL SELECT * from :te_b;
INSERT INTO VALS
(SELECT * from :te_all);
END;
You can convert the CTE into a Sub-Select statement in many cases
You can use following
insert into city (city, countryid, citycode)
select
city, countryid, citycode
from (
-- CTE Expression as subselect
select * from city
-- end (CTE)
) cte
Instead of using following valid CTE command combined with INSERT (on SQL Server)
with cte as (
select * from city
)
insert into city (city, countryid, citycode)
select
city, countryid, citycode
from cte
SAP HANA includes this posibility, the order of the code is different than SQL Server:
INSERT INTO EXAMPLE (ID)
WITH cte1 AS (SELECT 1 AS ID FROM DUMMY)
SELECT ID FROM cte1;

Return auto generated column values from sqlite database

How to return auto generated column values from sqlite database in c#. I'm using Sqlite.data.dll.
Tried by executing query : "select last_insert_rowid() but it doesn;t seem to work as it is returning 0s. I'm using SqliteDataReader object to get the result.
Thanks
I tried these commands, and it worked fine (even if the last_insert_rowid() call is from another transaction):
sqlite> create table mytable(pk integer primary key, other stuff);
sqlite> insert into mytable values(null, 42);
sqlite> select last_insert_rowid();
1
The only requirement of last_insert_rowid() is that it is called on the same database connection; please check that you didn't open another one.
is your id is incremented? then try this:
SELECT max(id)
or you can refer to this post
Click HERE

Converting SQL Server to Oracle

In my project, I have a database in SQL which was working fine. But now I have to make the application support oracle db too.
Some limitations I found out was that in Oracle, there is no bit field and the table name cannot be greater than 30 char. Is there any other limitation that I need to keep in mind.
Any suggestion from past experience will be helpful.
If I recall correctly from my earlier Oracle days:
there's no IDENTITY column specification in Oracle (you need to use sequences instead)
you cannot simply return a SELECT (columns) from a stored procedure (you need to use REF CURSOR)
of course, all stored procs/funcs are different (Oracle's PL/SQL is not the same as T-SQL)
The SQL ISNULL counterpart in Oracle is NVL
select ISNULL(col, 0)...
select NVL(col, 0)...
You will also struggle if you attempt to select without a from in Oracle. Use dual:
select 'Hello' from DUAL
Bear in mind also, that in Oracle there is the distinction between PL/SQL (Procedural SQL) and pure SQL. They are two distinct and separate languages, that are commonly combined.
Varchar in Oracle Databases called
varchar2 is limited to 4000
characters
Oracles concept of temporary tables is different, they have a global redefined structure
by default sort order and string compare is case-sensitive
When you add a column to a select *
Select * from table_1 order by id;
you must prefix the * by the table_name or an alias
Select
(row_number() over (order by id)) rn,
t.*
from table_1 t
order by id;
Oracle doesn't distinguish between null and '' (empty string). For insert and update you ca use '', but to query you must use null
create table t1 (
id NUMBER(10),
val varchar2(20)
);
Insert into t1 values (1, '');
Insert into t1 values (2, null);
Select * from t1 where stringval = 0; -- correct but empty
Select * from t1 where stringval is null; -- returns both rows
ORACLE do not support TOP clause. Instead of TOP you can use ROWNUM.
SQL Server: TOP (Transact-SQL)
SELECT TOP 3 * FROM CUSTOMERS
ORACLE: ROWNUM Pseudocolumn
SELECT * FROM CUSTOMERS WHERE ROWNUM <= 3

SELECT INTO statement in sqlite

Does sqlite support the SELECT INTO statement?
Actually I am trying to save the data in table1 into table2 as a backup of my database before modifying the data.
When I try using the SELECT INTO statement:
SELECT * INTO equipments_backup FROM equipments;
I get a syntax error:
"Last Error Message:near "INTO":syntax
error".
Instead of
SELECT * INTO equipments_backup FROM equipments
try
CREATE TABLE equipments_backup AS SELECT * FROM equipments
sqlite does not support SELECT INTO.
You can probably use this form instead:
INSERT INTO equipments_backup SELECT * FROM equipments;
SQlite didn't have INSERT INTO syntax.
In 2019, I use TEMPORARY keyword to create temporary table and INSERT data to temp table:
CREATE TEMPORARY TABLE equipments_backup(field1 TEXT, field2 REAL)
INSERT INTO equipments_backup SELECT field1, field2 FROM equipments

Query to get all table names

Can any one tell me that how to get names of all tables of a database using asp.net
A newer method on SQL Server is to use the INFORMATION_SCHEMA Views to get the information:
SELECT table_name FROM INFORMATION_SCHEMA.Tables WHERE table_type='BASE TABLE'
This particular view also includes Views in its list of tables, which is why you need the where clause.
You didn't mention which database engine you are using. On SQL Server, you can query the sysobjects table and filter for objects with type U:
SELECT name FROM sysobjects WHERE type = 'U'
In case you are interested in the MySQL way to achieve this, you can use
DESCRIBE tableName;

Resources