Not able to insert data into Snowflake table with Specific data types
This may be very silly question but i just started trying out Snowflake and stuck at first step itself .
I need to look up XML element from the DETAILS column
select testId from app_event table ;
This is how i am trying to create a table in Snowflake
create or replace table app_event (
ID varchar(36) not null primary key,
VERSION number,
ACT_TYPE varchar(255),
EVE_TYPE varchar(255),
CLI_ID varchar(36),
DETAILS variant,
OBJ_TYPE varchar(255),
DATE_TIME timestamp,
AAPP_EVENT_TO_UTC_DT timestamp,
GRO_ID varchar(36),
OBJECT_NAME varchar(255),
OBJ_ID varchar(255),
USER_NAME varchar(255),
USER_ID varchar(255),
EVENT_ID varchar(255),
FINDINGS varchar(255),
SUMMARY variant
);
This is my Sample data
ID VERSION ACT_TYPE EVE_TYPE CLI_ID DETAILS OBJ_TYPE DATE_TIME AAPP_EVENT_TO_UTC_DT GRO_ID OBJECT_NAME OBJ_ID USER_NAME USER_ID EVENT_ID FINDINGS SUMMARY
003cce70-ffff-43bc-8905-5e1d64475aa1 0 SCREENED_CASE WORLDCHECK 0 <?xml version="1.0" encoding="UTF-8" standalone="yes"?><testPayload><testId>565656-21cf-4c7e-8071-574a1ef78981</testId><testCode>COMPLETED</testCode><testState>TEST</testState><testResults>1</testResults><testRequiredResults>0</testRequiredResults><testExcludedResults>0</testExcludedResults><testAutoResolvedResults>1</testAutoResolvedResults><testproviderTypes>WATCHLIST</testproviderTypes></testPayload> CASE 9/16/2020 9:45 9/16/2020 9:45 erutrt7-d726-4672-8599-83d21927bec5 0 5786765dfgdfgdfg System User USER_SYSTEM 0 0 <?xml version="1.0" encoding="UTF-8" standalone="yes"?><testCaseEventSummary><testTypes>WATCHLIST</testTypes><testResults>1</testResults></testCaseEventSummary>
I am to create table with above DDL but when i insert it throws error
But when i declare all data type as varchar i am able to insert rows into this table
Can you please suggest what i am missing .
This is how i am trying to load data into table
COPY INTO demo_db.public.app_event
FROM #my_s3_stage/
FILES = ('Event_data.csv')
file_format=(type='CSV');
I am to create table with above DDL but when i insert it throws error But when i declare all data type as varchar i am able to insert rows into this table
This kind of error message indicates that there is implicit conversion that fails.
Most likely for column AAPP_EVENT_TO_UTC_DT timestamp the value 9/16/2020 9:45 is not recoginized.
For providing date/timestamps: '2020-09-16 09:45:00'::timestamp could be used or TRY_TO_TIMESTAMP(string_expr, format) function providing specific format.
Related
I have a table that has three columns from which I need to retrieve the data. One of the columns OtherNames contain an array of Json Objects.
CREATE TABLE Persons (
NameID int,
CurrentName varchar(255),
OtherNames varchar(max),
);
I can query that column just fine, but how can I join it with the table it is in by ID so I can retrieve all the information on the table and what is related to this row.
DECLARE #test VARCHAR(MAX)
SELECT #test = '[{"Name":"Bob","DateTime":"03/03/2022"},{"Name":"Adam","DateTime":"04/05/2022"}]'
SELECT * FROM OPENJSON(#test)
WITH (
Name VARCHAR(MAX) '$.Name',
DateTime VARCHAR(MAX) '$.DateTime'
)
This above results in
Bob 03/03/2022
Adam 04/05/2022
How can I join to show the NameID and CurrentName along with it ?
NameID Name DateTime CurrentName
1 Bob 03/03/2022 Rob
1 Adam 04/05/2022 Rob
There could be multiple records and multiple Json data..
As I mentioned in the comments, use OPENJSON against the column, not a scalar variable which contains the value of just one of your rows, and none of the other row data.
SELECT P.NameID,
ONs.[Name],
ONs.[DateTime],
P.CurrentName
FROM dbo.Persons P
CROSS APPLY OPENJSON(P.OtherNames)
WITH ([Name] varchar(255),
[DateTime] date) ONs;
Note that as your value [DateTime] is culture dependant, you may need to define it as a varchar(10) in the WITH, and then CONVERT it to a date in the SELECT with a style code.
<Standard>
<Equip Cat="Hardware">10 TB Disk</Equip>
<Equip Cat="Hardware">USB 2.0</Equip>
</Standard>
I have a column called XML in a table called Product. The type is XML(.), and I am wondering how I can process the xml column.
The big issue is that I have no idea how to do a foreach in a XML column.
I want to add a row if my stored procedure detects the substring "10 TB" inside of the category "Hardware"inside the table called ProductFeature with the id from table Product in the column id and true inside of the column HighDiskSpace.
Here is a conceptual example for you.
It is using XQuery .exist() method. Its XPath predicate expression satisfies your question requirements. Both variables, #cat and #searchString, could be your stored procedure parameters.
SQL
-- DDL and sample data population, start
DECLARE #Product TABLE (ID INT IDENTITY PRIMARY KEY, xmldata XML);
INSERT INTO #Product (xmldata) VALUES
(N'<Standard>
<Equip Cat="Hardware">10 TB Disk</Equip>
<Equip Cat="Hardware">USB 2.0</Equip>
</Standard>');
-- DDL and sample data population, end
DECLARE #cat VARCHAR(30) = 'Hardware'
, #searchString VARCHAR(30) = '10 TB';
SELECT *
FROM #Product
WHERE xmldata.exist('(/Standard/Equip[#Cat=sql:variable("#cat")
and contains(./text()[1], sql:variable("#searchString"))])[1]') = 1;
When I am trying to insert data to H2 table, it is showing the below exception.
INSERT INTO DATA (IP,HEADERS,METHOD,BODY,ID,TIME) VALUES (?,?,?,?,?,?) -- (?1, ?2, ?3, ?4, ?5, ?6) [90004-175]
Caused by: org.h2.jdbc.JdbcSQLException: Hexadecimal string contains non-hex character: "[accept=application/json, activityid=caf59f7d-b2f1-4250-9e7a-61606ff5dace, Authorization=Bearer dc98a786-6dda-35e0-9086-7a3beba5edd5, Content-Length=19, Content-Type=application/json, Host=190.0.0.101:8248, User-Agent=curl/7.58.0]"; SQL statement:
INSERT INTO DATA (IP,HEADERS,METHOD,BODY,ID,TIME) VALUES (?,?,?,?,?,?) -- (?1, ?2, ?3, ?4, ?5, ?6) [90004-175]
Table :
CREATE TABLE IF NOT EXISTS DATA (
TIME BIGINT,
ID VARCHAR(255),
METHOD VARCHAR(255),
HEADERS BLOB,
BODY BLOB,
IP VARCHAR(255),
PRIMARY KEY(ID)
);
What can be the reason for this? How can I resolve it? Is this a problem of that type or value of inserting.
Appreciate your valuable input here.
I have four simple columns:
address, name, phone num, total price
and an array of "Foods" which have some columns like
Id, discount, description i.e
Please kindly suggest how many tables which relation should be used in it.
Note: it is for online food delivery app
Here is a screenshot of my table request in firebase :
As a head start:
CREATE TABLE Request (
RequestID INT PRIMARY KEY,
address VARCHAR(1000),
name VARCHAR(1000),
phone VARCHAR(100),
total DECIMAL(10,2))
CREATE TABLE RequestContent (
RequestContentID INT IDENTITY PRIMARY KEY, -- Generate a primary key for each record (identity)
RequestID INT,
productDiscount DECIMAL(10,2),
productId INT,
productName VARCHAR(100), -- You might want to leave the name out and have it on a "Product" table
productPrice DECIMAL(10,2),
productQuantity INT,
FOREIGN KEY (RequestID) REFERENCES Request (RequestID))
You might want to review the proper data type for each one.
This is the sample procedure I am using.
create procedure PRO_ProcName
#description varchar(max),
#txn_no varchar
as
begin
declare #txn table (
id bigint,
description varchar(max),
txn_no varchar
);
declare #txn_id bigint;
insert into transactions
(
description,
txn_no
)
output
inserted.description,
inserted.txn_no
into #txn
values
(
#description,
#txn_no
)
select #txn_id = id from #txn;
end
I am getting an error like:
Column name or number of supplied values does not match table definition.
I know that it is because I have id field in my temporary table and it is not getting inserted in the insert into statement. I cannot give value for id because it is the auto increment primary key.
How can I tackle this situation and get id of inserted record into a variable?
The inserted table represents the data that exists in the target table after the insert - so it also includes the auto-generated values, whether they where generated by a default value definition or by an identity definition on the columns - so you need to add inserted.id to the output clause.
However, there are two more things wrong in your procedure.
The first and most important is the fact that you didn't specify a length to the #txn_no varchar parameter. SQL Server will implicitly specify the length of 1 char in this case.
The second is the fact that you are not specifying the columns list of #txn in the output clause.
Here is a improved version of your code with all these issues fixed:
create procedure PRO_ProcName
#description varchar(max),
#txn_no varchar(255) -- Note: I don't know the actual length you need
as
begin
declare #txn table (
id bigint,
description varchar(max),
txn_no varchar
);
declare #txn_id bigint;
insert into transactions
(
description,
txn_no
)
output
inserted.id,
inserted.description,
inserted.txn_no
into #txn(id, description, txn_no)
values
(
#description,
#txn_no
)
select #txn_id = id from #txn;
end
I cannot give value for id because it is the auto increment primary key.
No it isn't. You haven't declared it to be anything of the sort. So we need to fix that first:
declare #txn table (
id bigint IDENTITY PRIMARY KEY,
description varchar(max),
txn_no varchar
);
And then we fix it by specifying a column list in your INTO clause:
output
inserted.description,
inserted.txn_no
into #txn (description, txn_no)
It's always a good habit to specify column lists anyway.
Or if I've misinterpreted your question, and the id should be coming from transactions, then you just add inserted.id as another column in your OUTPUT clause. inserted represents that state of the table after the insert. So you can include columns from it in your OUTPUT clause even if you didn't specify them in the INSERT.