Want to create a COMPUTED COLUMN in POSTGRESQL which has to be a migration of MSSQLCODE .The computed column is as a result of a function "FUN_GetExternalSystemNameFull"
MSSQL CODE FOR TABLE
CREATE TABLE ClientApplication(
ClientApplicationId bigint IDENTITY(1,1) NOT NULL,
ClientId bigint NULL,
ExternalSystemNameFull AS (dbo.FUN_GetExternalSystemNameFull(ClientApplicationId,' | ')),
Age bigint
)
function FUN_GetExternalSystemNameFull in POSTGRESQL :
CREATE OR REPLACE FUNCTION "FUN_GetExternalSystemNameFull"(v_subscriptionid bigint, v_separator character varying)
RETURNS character varying
LANGUAGE plpgsql
AS $function$
BEGIN
RETURN((SELECT STRING_AGG("c"."ExternalSystemName",coalesce(v_separator,'')) from(select "ExternalSystemName"
from "ClientApplication"
where "ApplicationId" = v_subscriptionId) "c"));
END; $function$
Iam not able to create a POSTGRESQL TABLE with computed column from the function.Please help.
Iam using Postgresql 11
Related
I'm doing something similar to this example from the Redshift documentation but when I insert into the table I get an error Cannot insert a NULL value into column id. The documentation explicitly says that this should auto increment automatically.
create table category_ident
(id bigint identity(0,1) not null,
catgroup varchar,
catname varchar,
catdesc varchar);
insert into category_ident(catgroup,catname,catdesc)
select catgroup,catname,catdesc from category;
I am new in Postgresql so this question may be dumb for you guys!
I am tring to use the referenced tables in a trigger function, but some how the function doesn't seem to have access on the referenced alias (new_table) :
CREATE FUNCTION Function_Name()
RETURNS TRIGGER AS
$$
BEGIN
SELECT tbl.some_field INTO new_table.other_field
from some_table as tbl;
return null;
END;
$$ LANGUAGE PLPGSQL;
I am having this error:
"new_table.other_field" is not a known variable
and here is the trigger code:
CREATE TRIGGER Trigger_Name
AFTER INSERT
ON Table_Name
REFERENCING NEW TABLE AS new_table
FOR EACH ROW
EXECUTE FUNCTION Function_Name();
The function code should be executed first and then the trigger's, so how could the function access the alias that is referenced later in the trigger defenition??
and how to access the referenced table aliases in the function?
Note: In my example I am tring to use the alias, so when I use NEW inplace of new_table the function is created successfully!
The problem was that i am tring to set data in the NEW table using the alias name, however to do that i should use the original name NEW and not the referenced alias new_table..
The referenced alias new_table could be used to only get data from, like in FROM CLAUSE, joins and WHERE CLAUSE where one doesn't change the data in the referenced table.
Update:
here is an example of what i did to test that:
create table test_table2(
id int primary key,
name varchar(255)
)
create table test_table(
id int primary key,
name varchar(255)
)
create or replace function F_test_table()
returns trigger as $$
begin
insert into test_table2(id, name)
select id, name from new_table;
return null;
end;
$$ LANGUAGE PLPGSQL;
drop trigger if exists tr_test_table ON test_table;
create trigger tr_test_table
AFTER INSERT
ON test_table
REFERENCING NEW TABLE AS new_table
for each row ------- row level trigger ---------
EXECUTE FUNCTION F_test_table();
insert into test_table
values(1, '11111')
select * from test_table2
notice that the trigger inserts the data into test_table2
I am using Azure SQL Data Warehouse Gen 1, and I create a partition table like this
CREATE TABLE [dbo].[StatsPerBin1](
[Bin1] [varchar](100) NOT NULL,
[TimeWindow] [datetime] NOT NULL,
[Count] [int] NOT NULL,
[Timestamp] [datetime] NOT NULL)
WITH
(
DISTRIBUTION = HASH ( [Bin1] ),
CLUSTERED INDEX([Bin1]),
PARTITION
(
[TimeWindow] RANGE RIGHT FOR VALUES ()
)
)
How should I split a partition only when there is no such boundary?
First I think if I can get partition boundaries by table name, then I can write a if statement to determine add partition boundary or not.
But I cannot find a way to associate a table with its corresponding partition values, the partition values of all partitions can be retrieved by
SELECT * FROM sys.partition_range_values
But it only contains function_id as identifier which I don't know how to join other tables so that I can get partition boundaries by table name.
Have you tried joining sys.partition_range_values with sys.partition_functions view?
Granted we cannot create partition functions in SQL DW, but the view seems to be still supported.
I know this is an out of date question, but I was having the same problem. Here is a query I ended up with that can get you started. It is modified slightly from a query for SQL Server documentation:
SELECT s.[name] AS [schema_name]
, t.[name] AS [table_name]
, p.[partition_number] AS [partition_number]
, rv.[value] AS [partition_boundary_value]
, p.[data_compression_desc] AS [partition_compression_desc]
FROM sys.schemas s
JOIN sys.tables t ON t.[schema_id] = s.[schema_id]
JOIN sys.partitions p ON p.[object_id] = t.[object_id]
JOIN sys.indexes i ON i.[object_id] = p.[object_id]
AND i.[index_id] = p.[index_id]
JOIN sys.data_spaces ds ON ds.[data_space_id] = i.[data_space_id]
LEFT JOIN sys.partition_schemes ps ON ps.[data_space_id] = ds.[data_space_id]
LEFT JOIN sys.partition_functions pf ON pf.[function_id] = ps.[function_id]
LEFT JOIN sys.partition_range_values rv ON rv.[function_id] = pf.[function_id]
AND rv.[boundary_id] = p.[partition_number]
I'm trying to create an index on the AlbumTokens column in my Google Cloud Spanner test database and I get a mysterious error referencing an index option that is not currently documented:
CREATE INDEX AlbumTokens
ON Albums (
AlbumTokens
)
>>> Index AlbumTokens references ARRAY AlbumTokens, but is not declared as DISTINCT_ARRAY_ELEMENT index.
Is it possible to do this? If so, how?
I'm using the sample schema with an ARRAY<STRING> column added on:
CREATE TABLE Singers (
SingerId INT64 NOT NULL,
FirstName STRING(1024),
LastName STRING(1024),
SingerInfo BYTES(MAX),
) PRIMARY KEY (SingerId)
CREATE TABLE Albums (
SingerId INT64 NOT NULL,
AlbumId INT64 NOT NULL,
AlbumTitle STRING(MAX),
AlbumTokens ARRAY<STRING(MAX)>,
) PRIMARY KEY (SingerId, AlbumId),
INTERLEAVE IN PARENT Singers ON DELETE CASCADE
You can't create an index using an Array as a key:
Disallowed types
These cannot be of type ARRAY:
A table's key columns.
An index's key columns.
You can include the Array in the index via the STORING keyword to return the array without joining to the primary table, but you can't scan on i
in sqlite it is possible to have string by which the table was created:
select sql from sqlite_master where type='table' and tbl_name='MyTable'
this could give:
CREATE TABLE "MyTable" (`id` PRIMARY KEY NOT NULL, [col1] NOT NULL,
"another_col" UNIQUE, '`and`,''another'',"one"' INTEGER, and_so_on);
Now I need to extract with this string any additional parameters that given column name has been set with.
But this is very difficult since the column name could be enclosed with special characters, or put plain, column name may have some special characters that are used as encapsulation etc.
I don't know how to approach it. The result should be having a column name the function should return anything that is after this name and before , so giving it id it should return PRIMARY KEY NOT NULL.
Use the pragma table_info:
http://www.sqlite.org/pragma.html#pragma_table_info
sqlite> pragma table_info(MyTable);
cid|name|type|notnull|dflt_value|pk
0|id||1||1
1|col1||1||0
2|another_col||0||0
3|`and`,'another',"one"|INTEGER|0||0
4|and_so_on||0||0