Inserting data into a table whose name was obtained in another query - sql

I'm trying to save a variable (aka parameter) to the database, which can be INTEGER, REAL or TEXT.
Depending on the type of variable that is stored in the "units" table, I want to get the name of the table where the data needs to be stored.
I built a query to get the table name and tested it. It works:
SELECT table_name FROM units WHERE id =
(SELECT id_unit FROM parameters WHERE name = 'ADC1')
The query returns the correct table name. In my case it is 'real_values'
Next I am trying to combine the above question and data insert
INSERT INTO
(SELECT table_name FROM units WHERE id =
(SELECT id_unit FROM parameters WHERE name = 'ADC1')
) (id, value) VALUES (1, 1.234)
And I get an error message
SQL Error [1]: [SQLITE_ERROR] SQL error or missing database (near "(": syntax error)

Related

'NULL as' returns error on creating table from query

I want to create a new table from a query with new column with a NULL value for every row.
For this purpose, I use the following query:
CREATE TABLE my_table AS SELECT _id, NULL as value, true as __deleted
FROM test_table
Which gives me the following error:
SYNTAX_ERROR: line 1:1: Column type is unknown: value. You may need to manually clean the data at location
Even though the query without the table creation SELECT _id, NULL as value, true as __deleted FROM test_table works well.
Is my query invalid for table creation? I did not find any info about such limitations.
If you check docs for CREATE TABLE you will see that column type is mandatory for every column declaration, and select typeof(null) will return unknown. A possible workaround is to cast NULL to some data type:
CREATE TABLE my_table AS
SELECT _id, cast(NULL as varchar) value, true __deleted
FROM test_table

How to retrieve ambiguous columns from a hive table using subquery?

Main table:
CREATE EXTERNAL TABLE user(language STRING,snapshot_time STRING,products STRUCT<id:STRING,name:STRING>,item STRUCT<quantity:ARRAY<STRUCT<name:STRING>>>)
ROW FORMAT SERDE 'org.openx.data.jsonserde.JsonSerDe'
STORED AS TEXTFILE
LOCATION '/user/input/sample';
I'm trying to insert ambiguous column names "product.name","A.name" into user_prod_info table. Since, the column names are same, I'm facing Ambiguous column reference text in q error.
Insert command:
INSERT OVERWRITE TABLE user_prod_info
SELECT q.* FROM (
SELECT row_number() OVER (PARTITION BY products.id ORDER BY snapshot_time DESC) AS temp_row_num,
language,
snapshot_time,
products.id,
products.name,
A.name
FROM user as raw
LATERAL VIEW EXPLODE(item.quantity) quantity as A
) q WHERE temp_row_num == 1;
This command is unable to retrieve the field from the specific table because we have two "name" fields. one is in "products" and the other is in "A".
I tried creating alias for "A.name as name1". I'm able to insert the data without errors. But, one record is storing in 3 rows with some nulls in it.
I got stuck over here. Can anyone please help me out regarding this...

Create a Table in Oracle, Update Data Type, Insert Into with Excel VBA

I am trying to create a new table in oracle but I am getting these errors with their respective attempts:
FAILED CREATE:
1)
CREATE TABLE quicktemp (LN_ID VARCHAR(255))
Err : "ORA-00907: missing right parenthesis"
2)
CREATE TABLE quicktemp VALUES (LN_ID VARCHAR(255))
Err : "ORA-00922: missing or invalid option"
SUCCESSFUL CREATE, FAILED DATA TYPE on INSERT INTO:
3)
CREATE TABLE quicktemp AS (SELECT LN_ID FROM dual WHERE 1 = 0)
INSERT INTO quicktemp (LN_ID) WITH temp AS (
SELECT 1 FROM dual UNION ALL
SELECT 2 FROM dual UNION ALL
SELECT 3 FROM dual
)
SELECT * FROM temp
Err : ORA-01790: expression must have same datatype as corresponding expression
My three options are to 1) create a table to allow any datatypes, 2) update a created table (with method 3) to allow for any data type or 3) change the datatypes of the values I am trying to insert. Any help on this would be much appreciated.
PROJECT SUMMARY:
The data is being queried from one database, exported to an Excel file, and then imported into a new table in a different database. I am picking up the field names and the values from the newly created Excel worksheet. I am not planning on using existing tables since they either don't exist, or I can't access them with the current schema.
NB: This project needs to be done entirely in VBA. I cannot accept answers relating to SSIS, SQL*Loader, etc.

Basic SELECT fails to find entry

I'm probably making a stupid beginner mistake here but SQL isn't one of my strong points.
I'm trying to modify a simple SQLite DB (in SQLite Manager) but can't seem to do it until I figure out why I can't get this select statement to return.
SELECT *
FROM facts
WHERE id = -9215979828305747000
threw no error and returned nothing regardless of the fact that its the first entry on the table. I also tried
SELECT *
FROM facts
WHERE CAST(facts.id AS INTEGER) = CAST('-9215979828305747000' AS INTEGER)
again nothing.
I see the value in the browse window but can't get it in a specific search. Is this happening because of value length?
-9215979828305747000 is bigger than the maximum value for an Int32 (2147483647), so I guess facts.id is not an Integer. Check your datatype and CAST accordingly.
Perhaps it is a string?
SELECT *
FROM facts
WHERE id = '-9215979828305747000'
I did this test :
CREATE TABLE "test" (
"id" INTEGER PRIMARY KEY NOT NULL,
"name" TEXT
)
insert into test values (1, "tttt")
insert into test values (-9215979828305747000, "OO")
select name from test where id = -9215979828305747000'
> "OO"
So it seems to work in a fresh new table. The problem is clearly not in the sql select or key or table definition.
Try to get the data in some temporary table :
insert into table_copy values (select * from facts)

Retrieve and insert into type objects in oracle

I have created an object type(address-city,state) in Oracle 10g .Then table cust_contact contains field of type address.Can anyone please provide SQL query to insert and retrieve values from this table including the type?
Selection is easy. Just include the type column in the query projection. Assuming that the ADDRESS column is called contact_address:
select id, contact_name, contact_address
from cust_contact
/
With inserts you need to specify the type in the statement:
insert into cust_contact values
(some_seq.nextval
, 'MR KNOX'
, address(34, 'Main Street', 'Whoville', 'SU')
)
/
You can also use the "." syntax when retrieving columns:
select c.contact_address.city from cust_contact c;
Please note that if cust_contact is a table of objects, then you must use the table alias "c".
for example :
first create type object say as address ,for this synatx or query is used:
create type address_ty as object(Street varchar2(50),City char(10),Zip number(6));
now use this address_ty as datatype in the time of table creation
for example:
create table Example(emp_name varchar2(10),emp_id number(10),address address_ty);
this will create table Example having
Address as address_ty as a datatype..
Now insert into Values In Example Table
Insert Into example Values('Sandeep Kumar',595,address_ty('Snap on sector 126','Noida',201301);
tHANX....