I created the following function upsert(insert or edit if it's already exist) in postgresql using pgadmin 4.
CREATE OR REPLACE FUNCTION upsert(
uname character varying(55),
fname character varying(55),
eml character varying(255),
psw character varying(265),
phonenbr character varying(55),
adrs character varying(300)
)
RETURNS table (j json) AS
$$
BEGIN
INSERT INTO users
VALUES (DEFAULT,uname, fname, eml, psw, phonenbr, adrs)
ON CONFLICT (username, firstname)
DO
UPDATE SET username = EXCLUDED.username, firstname = EXCLUDED.firstname,
email = EXCLUDED.email, password = EXCLUDED.password, phonenumber = EXCLUDED.phonenumber,
address = EXCLUDED.address, registrationdate=current_timestamp, subscriptionend =current_timestamp+ INTERVAL '1 month',stat='active';
END
$$
LANGUAGE 'plpgsql';
The issue is than when it's doing an insert, for 3 column the values are null but everything it's fine when it's doing an update.
He there a way to solved this issue without adding 3 more parameters to the function ?
the scrip for create the table:
CREATE TABLE users
(
id_user integer Generated Always as Identity,
username character varying(55) NOT NULL,
firstname character varying(55) NOT NULL,
email character varying(255) NOT NULL,
password character varying(255)NOT NULL,
phonenumber character varying(55)NOT NULL,
address character varying(300) NOT NULL,
subscriptionend timestamp without time zone ,
registrationdate timestamp without time zone,
stat status,
CONSTRAINT users_pkey PRIMARY KEY (username,firstname)
)
I find a solution by declaring and using variables inside the function:
CREATE OR REPLACE FUNCTION upsert(
uname character varying(55),
fname character varying(55),
eml character varying(255),
psw character varying(265),
phonenbr character varying(55),
adrs character varying(300)
)
RETURNS table (j json) AS
$$
DECLARE
regisdate timestamp without time zone;
subsend timestamp without time zone;
statu status;
BEGIN
regisdate:=current_timestamp;
subsend:=current_timestamp+ INTERVAL '1 month';
statu='active';
INSERT INTO users
VALUES (DEFAULT,uname, fname, eml, psw, phonenbr, adrs,regisdate,subsend,statu)
ON CONFLICT (username, firstname)
DO
UPDATE SET username = EXCLUDED.username, firstname = EXCLUDED.firstname,
email = EXCLUDED.email, password = EXCLUDED.password, phonenumber = EXCLUDED.phonenumber,
address = EXCLUDED.address, registrationdate = EXCLUDED.registrationdate, subscriptionend=EXCLUDED.subscriptionend,stat = EXCLUDED.stat;
END
$$
LANGUAGE 'plpgsql';
I have created one file format (CSV) and then one external table to load csv data from azure blob storage.
The external table is showing all columns as NULL except the "Value" column
File Format Code
COMPRESSION = 'NONE'
FIELD_DELIMITER = ','
RECORD_DELIMITER = '\n'
SKIP_HEADER = 0
FIELD_OPTIONALLY_ENCLOSED_BY = 'NONE'
EMPTY_FIELD_AS_NULL = FALSE
TRIM_SPACE = FALSE ERROR_ON_COLUMN_COUNT_MISMATCH = TRUE ESCAPE = 'NONE'
ESCAPE_UNENCLOSED_FIELD = '\134' DATE_FORMAT = 'AUTO'
TIMESTAMP_FORMAT = 'AUTO'
NULL_IF = ('NULL');
EXTERNAL TABLE CODE
CREATE OR REPLACE EXTERNAL TABLE EXT_DIM_TESTTABLE
(
COL1 VARCHAR (1000) AS (value:"COL1"::string),
COL2 VARCHAR (1000) AS (value:"COL2"::string),
COL3 VARCHAR (1000) AS (value:"COL3"::string),
COL4 VARCHAR (1000) AS (value:"COL4"::string),
COL5 VARCHAR (1000) AS (value:"COL5"::string),
COL6 VARCHAR (1000) AS (value:"COL6"::string)
)
WITH
LOCATION=#TESTSTAGE
AUTO_REFRESH = true
FILE_FORMAT = 'FILE_TESTFORMAT_CSV'
PATTERN='.*TEST_DATA.csv';
Now when I select * from EXT_DIM_TESTTABLE, all columns shows NULL except VALUE one,
VALUE column is coming as below, the column names are not taken as "Col1" / "Col2" etc. but the values are correct. Rest all columns are NULL
{
"c1": "TESTING",
"c2": "TESTING",
"c3": "TESTING",
"c4": "TESTING",
"c5": "TESTING",
"c6": "TESTING"
}
not sure what is missing here?
It seems you are using value:"COL1"::string incorrectly.
Can you try using below DDL for external table?
CREATE OR REPLACE EXTERNAL TABLE EXT_DIM_TESTTABLE
(
COL1 VARCHAR(1000) AS (value:c1::string),
COL2 VARCHAR(1000) AS (value:c2::string),
COL3 VARCHAR(1000) AS (value:C3::string),
COL4 VARCHAR(1000) AS (value:c4::string),
COL5 VARCHAR(1000) AS (value:c5::string),
COL6 VARCHAR(1000) AS (value:c6::string)
)
WITH
LOCATION=#TESTSTAGE
AUTO_REFRESH = true
FILE_FORMAT = 'FILE_TESTFORMAT_CSV'
PATTERN='.*TEST_DATA.csv'
;
How to write following query in snowflake
Declare firstName varchar;
Declare lastName varchar;
select firstName =FirstNameColumn,lastName =LastNameColumn from User;
You don't need to declare session variables, and you can use SET command to initialize/assign a value to a session variable:
https://docs.snowflake.com/en/sql-reference/session-variables.html#initializing-variables
create table names (fname varchar, lname varchar ) as select 'Gokhan','Atil';
set first_name = (select fname from names);
set last_name = (select lname from names);
select $first_name, $last_name;
+-------------+------------+
| $FIRST_NAME | $LAST_NAME |
+-------------+------------+
| Gokhan | Atil |
+-------------+------------+
Unfortunately, there is a bug when assigning multiple varchar variables like this:
set (first_name, last_name) = (select fname, lname from names);
Assignment to 'FIRST_NAME' not done because value exceeds size limit for variables. Its size is 16,777,216; the limit is 256 (internal storage size in bytes).
set my_variable2 = (select t2.v from t2 where t2.c = 'four');
select $my_variable2;
I have a table with 39 column and 30 rows in Sybase.I am trying to Concat all the 39 columns in a single column with 30 rows.
Tools used:
Winsql professional 4.5 connect to Sybase DB
table1 has actual data
Created a temp table2 of data type text. Create table #temp2 (Line text)
Insert and formatted using trim for space,null values and tried concat using + symbol into temp table2 from table1
Result: data gets truncated at 256 char
Findings: Sybase ASE text data type supports only 255 char
Can someone suggest on how to overcome with this issue!
Sybase ASE's text data type is not limited to 256 characters, but there are some tricks to using it successfully such as specifying textsize and being aware that these settings may be session and stored procedure specific.
Consider the following example Sybase ASE 16.0 GA on Linux:
Create then 39 column table.
create table table_1 (
col_1 Varchar(255) null,
col_2 Varchar(255) null,
col_3 Varchar(255) null,
col_4 Varchar(255) null,
col_5 Varchar(255) null,
col_6 Varchar(255) null,
col_7 Varchar(255) null,
col_8 Varchar(255) null,
col_9 Varchar(255) null,
col_10 Varchar(255) null,
col_11 Varchar(255) null,
col_12 Varchar(255) null,
col_13 Varchar(255) null,
col_14 Varchar(255) null,
col_15 Varchar(255) null,
col_16 Varchar(255) null,
col_17 Varchar(255) null,
col_18 Varchar(255) null,
col_19 Varchar(255) null,
col_20 Varchar(255) null,
col_21 Varchar(255) null,
col_22 Varchar(255) null,
col_23 Varchar(255) null,
col_24 Varchar(255) null,
col_25 Varchar(255) null,
col_26 Varchar(255) null,
col_27 Varchar(255) null,
col_28 Varchar(255) null,
col_29 Varchar(255) null,
col_30 Varchar(255) null,
col_31 Varchar(255) null,
col_32 Varchar(255) null,
col_33 Varchar(255) null,
col_34 Varchar(255) null,
col_35 Varchar(255) null,
col_36 Varchar(255) null,
col_37 Varchar(255) null,
col_38 Varchar(255) null,
col_39 Varchar(255) null)
go
I receive a warning about the potential row sizes not fitting on the page. My Sybase ASE instance is configured for 2K pages. 16K page size instances will not receive this warning. Truncation will only occur should the row size become larger than the page size:
Warning: Row size (10028 bytes) could exceed row size limit, which is 1962
bytes.
Insert the rows into table_1. Ideally, with 16K pages and columns with 255 characters, this insert statement could be used:
insert into table_1 values (
'0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789COL01',
'0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789COL02',
'0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789COL03',
'0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789COL04',
'0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789COL05',
'0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789COL06',
'0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789COL07',
'0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789COL08',
'0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789COL09',
'0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789COL10',
'0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789COL11',
'0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789COL12',
'0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789COL13',
'0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789COL14',
'0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789COL15',
'0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789COL16',
'0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789COL17',
'0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789COL18',
'0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789COL19',
'0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789COL20',
'0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789COL21',
'0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789COL22',
'0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789COL23',
'0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789COL24',
'0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789COL25',
'0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789COL26',
'0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789COL27',
'0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789COL28',
'0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789COL29',
'0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789COL30',
'0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789COL31',
'0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789COL32',
'0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789COL33',
'0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789COL34',
'0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789COL35',
'0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789COL36',
'0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789COL37',
'0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789COL38',
'0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789COL39')
go 30
The "go 30" at the end, submits the SQL batch 30 times, inserting 30 rows.
Since my own instance only has 2K pages, I'm limiting myself to 45 characters per column which is 1755 characters.
insert into table_1 values (
'0123456789012345678901234567890123456789COL01',
'0123456789012345678901234567890123456789COL02',
'0123456789012345678901234567890123456789COL03',
'0123456789012345678901234567890123456789COL04',
'0123456789012345678901234567890123456789COL05',
'0123456789012345678901234567890123456789COL06',
'0123456789012345678901234567890123456789COL07',
'0123456789012345678901234567890123456789COL08',
'0123456789012345678901234567890123456789COL09',
'0123456789012345678901234567890123456789COL10',
'0123456789012345678901234567890123456789COL11',
'0123456789012345678901234567890123456789COL12',
'0123456789012345678901234567890123456789COL13',
'0123456789012345678901234567890123456789COL14',
'0123456789012345678901234567890123456789COL15',
'0123456789012345678901234567890123456789COL16',
'0123456789012345678901234567890123456789COL17',
'0123456789012345678901234567890123456789COL18',
'0123456789012345678901234567890123456789COL19',
'0123456789012345678901234567890123456789COL20',
'0123456789012345678901234567890123456789COL21',
'0123456789012345678901234567890123456789COL22',
'0123456789012345678901234567890123456789COL23',
'0123456789012345678901234567890123456789COL24',
'0123456789012345678901234567890123456789COL25',
'0123456789012345678901234567890123456789COL26',
'0123456789012345678901234567890123456789COL27',
'0123456789012345678901234567890123456789COL28',
'0123456789012345678901234567890123456789COL29',
'0123456789012345678901234567890123456789COL30',
'0123456789012345678901234567890123456789COL31',
'0123456789012345678901234567890123456789COL32',
'0123456789012345678901234567890123456789COL33',
'0123456789012345678901234567890123456789COL34',
'0123456789012345678901234567890123456789COL35',
'0123456789012345678901234567890123456789COL36',
'0123456789012345678901234567890123456789COL37',
'0123456789012345678901234567890123456789COL38',
'0123456789012345678901234567890123456789COL39')
go 30
Check the correct number of characters have been entered, this should be the length of string in each column. In my own case, 45.
select
char_length(col_1),
char_length(col_2),
char_length(col_3),
char_length(col_4),
char_length(col_5),
char_length(col_6),
char_length(col_7),
char_length(col_8),
char_length(col_9),
char_length(col_10),
char_length(col_11),
char_length(col_12),
char_length(col_13),
char_length(col_14),
char_length(col_15),
char_length(col_16),
char_length(col_17),
char_length(col_18),
char_length(col_19),
char_length(col_20),
char_length(col_21),
char_length(col_22),
char_length(col_23),
char_length(col_24),
char_length(col_25),
char_length(col_26),
char_length(col_27),
char_length(col_28),
char_length(col_29),
char_length(col_30),
char_length(col_31),
char_length(col_32),
char_length(col_33),
char_length(col_34),
char_length(col_35),
char_length(col_36),
char_length(col_37),
char_length(col_38),
char_length(col_39)
from table_1
go
Now create table_2 with the text column.
create table table_2 (col_1 text null)
go
Insert the rows into table_2 from the concatenated values of the columns in table_1. There will be one row in table_2 for each row in table_1.
insert into table_2 select
col_1 +
col_2 +
col_3 +
col_4 +
col_5 +
col_6 +
col_7 +
col_8 +
col_9 +
col_10 +
col_11 +
col_12 +
col_13 +
col_14 +
col_15 +
col_16 +
col_17 +
col_18 +
col_19 +
col_20 +
col_21 +
col_22 +
col_23 +
col_24 +
col_25 +
col_26 +
col_27 +
col_28 +
col_29 +
col_30 +
col_31 +
col_32 +
col_33 +
col_34 +
col_35 +
col_36 +
col_37 +
col_38 +
col_39 as result
from table_1
go
Check the length of the column in table_2. If using 45 characters it should be 1755; if using 255 characters it should be 9945.
select char_length(col_1) from table_2
go
Confirm the last value of the last row ends in "COL39".
select col_1 from table_2
go
Something like...
0123456789012345678901234567890123456789COL39
Given the above test case was conducted using Sybase's isql utility we can show that Sybase ASE does correctly concatenate the values and store them in a text column. You are using "winsql", a tool that I am not familiar with nor do I have access to. I imagine this may be imposing some limits on what is being displayed. I suspect somewhere it maybe running:
set textsize 255
or simply truncating the data. The above test case should be able to confirm this. The values returned by char_length() will not be subjected to the truncation unless the input data is truncated.
After installing Sybase ASE 12.5.1 on Windows 2000 and configuring it for 16K pages, the commands above were executed in a database called "rwc". The commands worked as advertised.
I'm trying to use a concatenated value of two columns to display data. I'm first testing out ways to do this in a query before actually using it in a stored proc but I just don't get any results back.
So I have a table called Person:
CREATE TABLE Person
(
IDNumber char(13) NOT NULL,
Name varchar(MAX) NOT NULL,
Surname varchar(MAX) NOT NULL,
Age int NOT NULL,
PRIMARY KEY (IDNumber)
)
I want to display all the data available if the concatenated value of Name and Surname is equal to say 'Harriet West' but I'm not sure how to do this. Can anyone help by explaining how to do this?
You want a computed column such as:
CREATE TABLE Person (
IDNumber char(13) NOT NULL,
Name varchar(MAX) NOT NULL,
Surname varchar(MAX) NOT NULL,
Age int NOT NULL,
Fullname AS Name + ' ' + Surname,
PRIMARY KEY (IDNumber)
)
Example
INSERT INTO Person (IDNumber, Name, Surname, Age)
VALUES ('1', 'Bob', 'Smith', 10);
You can then query the computed column:
SELECT [column list] FROM Person WHERE Fullname = 'Bob Smith';
Result
| IDNUMBER | NAME | SURNAME | AGE | FULLNAME |
|---------------|------|---------|-----|-----------|
| 1 | Bob | Smith | 10 | Bob Smith |
Documentation
Some other thoughts:
It seems silly to give IDNumber a string data type; perhaps call it something else?
Do you really need varchar(max) for name columns?
Don't store a calculated value. You should store the date of birth of the person, then calculate the age.
I like the computed column approach, but if you don't want it, in your query you can do:
SELECT IDNumber, Name, Surname, Name + ' ' + Surname [Full Name], Age
FROM Person
If you want to query by the Full Name you could do:
SELECT IDNumber, Name, Surname, Age
FROM Person
WHERE Name + ' ' + Surname = 'Harriet West'
sqlfiddle demo
In the where clause...
Select * from Person
Where SurName + ' ' + Name = 'Harriet West'
Be sure to set concat_null_yields_null Off if there is a chance that SurName or Name could be Null.