I want to add records into an internal table. When I am trying to add, t is showing the error of
The field "ITAB_EMPLOYEE" is unknown, but there is a field with the similar name "ITAB_EMPLOYEE_I". .
REPORT zitab_siddhi.
TYPES: BEGIN OF EMPLOYEE,
EMPID TYPE C,
EMPNAME TYPE string,
EMPADDRESS TYPE c,
EMPEMAIL TYPE c,
EMPDEPT TYPE c,
EMPROLE TYPE string,
EMPCONT TYPE C,
END OF employee.
TYPES ITAB_EMPLOYEE TYPE STANDARD TABLE OF EMPLOYEE.
DATA: ITAB_EMPLOYEE_I TYPE EMPLOYEE.
ITAB_EMPLOYEE_I-EMPID = '123'.
ITAB_EMPLOYEE_I-EMPNAME = 'JOHN DOE'.
ITAB_EMPLOYEE_I-EMPADDRESS = 'BANGALORE'.
ITAB_EMPLOYEE_I-EMPEMAIL = 'JOHN#BANGALORE.COM'.
ITAB_EMPLOYEE_I-EMPDEPT = 'SALES'.
ITAB_EMPLOYEE_I-EMPROLE = 'MANAGER'.
ITAB_EMPLOYEE_I-EMPCONT = '1234567890'.
APPEND ITAB_EMPLOYEE_I TO ITAB_EMPLOYEE.
ITAB_EMPLOYEE is declared as table type and not as internal table in your code. Replace TYPES with DATA:
DATA itab_employee TYPE STANDARD TABLE OF employee.
Related
I ran below command to create a new table from an existing table. Problem is BQ creates this new table but change mode of REQUIRED columns to NULLABLE
create table `project_id.dataset.new_table_name` as
select * replace(
array(select as struct person.* except(add) from t.person) as person
)
from `project_id.dataset.table_name` t;
Expecting column mode unchanged.
The NOT NULL attribute of a table's column_schema does not propagate through queries over the table. If table T contains a column declared as x INT64 NOT NULL, for example, CREATE TABLE dataset.newtable AS SELECT x FROM T creates a table named dataset.newtable in which x is NULLABLE.
https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language#column_name_and_column_schema
So, you'd better consider explicit schema definition.
CREATE OR REPLACE TABLE `project_id.dataset.table_name` (
Id STRING,
Person ARRAY<STRUCT<
Name STRING NOT NULL,
Add STRUCT<line STRING>
>>
) AS
SELECT 'id', [('John', STRUCT('aaa'))];
CREATE OR REPLACE TABLE `project_id.dataset.new_table_name` (
Id STRING,
Person ARRAY<STRUCT<
Name STRING NOT NULL
>>
) AS
SELECT * REPLACE (ARRAY(SELECT AS STRUCT Person.* EXCEPT(add) FROM t.Person) AS Person)
FROM `project_id.dataset.table_name` t;
I have this query :
--What is the Specialization of employee ID 5?
SELECT EmpName
FROM Employee
WHERE Employee.ID IN(
SELECT Specialization
FROM Specialization
WHERE spe_mng_id = 5);
And when I run the command it returns me this error message:
"Conversion failed when converting the varchar value 'Assistant' to data type int."
My records are in varchar :
--Table Specialization
create table Specialization(
spe_id int,
Specialization varchar(50),
spe_mng_id int IDENTITY(1,1),
FOREIGN KEY(spe_mng_id) REFERENCES Employee(ID)
);
I don't understand what is hapenning, I am also quite new to SQL so it's dificult to find answers for my exact problem, thus asking the comunity!
I am expecting to get a return value of the Specialization of Employee with ID 5.
ID and spe_mng_id are linked.
When you run SELECT Specialization FROM Specialization WHERE spe_mng_id = 5, you already get a list of all specializations (possibly only one) of employee n5. If this is the only information needed (as in nothing from table Employee is required), you could even run this query directly.
However, if you also need to get the EmpName as an output (or any other info from table Employee), you will need to join the two tables using the JOIN command, using the info that spe_mng_id is a reference to ID in the table Employee (as seen through the FOREIGN KEY definition).
Something like:
SELECT EmpName, Specialization
FROM Employee
JOIN Specialization ON Employee.ID = Specialization.spe_mng_id
WHERE Employee.ID = 5
The conversion problem happened because you were trying to find rows whose Employee.ID (an INT) was in the list of Specialization of Employee n5 (a column of VARCHAR).
in my table I have a column name "Status" and a RECORD type column name "Payment", which has another column name "Status" inside it. So I basically have 2 columns: "Status" and "Payment.Status". When I want to UNNEST the STRUCT data("Payment" column) and use the data, I will get the error message Column name STATUS is ambiguous
How do I solve this problem?
Thanks in advance
When you are trying to use 'STATUS', BigQuery does not know to which column you are referring to.
You can overcome this with giving your UNNEST() an alias and using it to reference the specific column you are using.
Example:
with example_data as (
select true as status,
[STRUCT( 123 as id, false as status)] as payment
)
select e.status,
p.id,
p.status as payment_status
from example_data e, unnest(payment) as p
I have a table to store table names (lets call it "CUSTOM_TABLES").
I have a table to register data tracking (call it "CONSUMPTIONS").
I have tables that the user was created and I don't know its names (created at runtime) so, the system create the table ( execute the DDL) and store its name in "CUSTOM_TABLES". Lets call it "USER_TABLE" for now.
When a data is produced in a table "USER_TABLE", I register in the tracking table ("CONSUMPTIONS") the row ID of data and the ID of the "USER_TABLE" found in "CUSTOM_TABLES".
Now, I need to find, given a consumption, what table and what row the data is. Remember: in "CONSUMPTIONS" table I have only an ID (FK) pointing to "CUSTOM_TABLES".
CREATE TABLE consumptions
(
id_consumption serial NOT NULL,
id_row integer,
id_table integer
)
CREATE TABLE custom_tables
(
id_table serial NOT NULL,
description character varying(250),
name character varying(150)
)
The query I need is here:
select * from consumptions c
join insurance i on c.id_row = i.index_id
In this case, "insurance" is the "USER_TABLE".
But I don't know "insurance". I need to find it in "CUSTOM_TABLES" by using its ID.
select name from custom_tables where
id_table = consumption.id_table
The final query must be something like:
select * from consumptions c
join
(select name from custom_tables t where t.id_table = c.id_table) i
on c.id_row = i.index_id
I guarantee the "user_tables" have "index_id" attribute as its PK.
I prefer do not use functions.
i have 2 tables.
TABLE A COLUMNS - aid , aname
TABLE B COLUMNS - bid , bname
from table A, i will pick up data from column 'aid',
AND insert it in 'bid' column of table B , but in bname column of table B, i will insert a new value. how do i do this?
create table A(aid int,aname char)
insert into A values(111, 'e')
create table B(bid int, bname char)
insert into B (bid,bname)
bid will take value from the query : select aid from a
bname will get a new value -m
expected result should be : THE TABLE B WILL HAVE :
bid bname
--- -----
111 m
Try this:
insert into b (bid, bname) select aid, 'm' as bname_fixed_val from a
Two facts enabled the solution above:
The insert .. select clause allows you to insert the values returned with any select.
You can return constant values as fields with select, like for instance:
SELECT 0 as id, 'John' as name
Combining these two points together, I used an insert..select clause to select the field value from the first table (aid), along with a constant value for the second field (m). The AS bname_fixed_val clause is simply a field alias, and can be omitted.
For more information on SQL, here 's a link: http://www8.silversand.net/techdoc/teachsql/index.htm, although googling it wouldn't hurt also.