Snowflake: Trying to make a column to use as default the value from a sequence - sequence

So, I have something like this:
CREATE OR REPLACE TABLE TABLE_NAME (
ID NUMBER(38, 0) NOT NULL,
/* OTher elements */
)
With some values already (manually) inserted. I need to update this table so, for future inserts, the value of ID is taken from a sequence I just created:
CREATE OR REPLACE SEQUENCE S_TABLE_NAME_ID
START WITH 451;
For what I've seen in the documentation and in several forums, the syntax should be like this:
ALTER TABLE TABLE_NAME ALTER ID SET DEFAULT S_TABLE_NAME_ID.NEXTVAL;
But when I try to execute it, I get the following error message:
SQL Error [2] [0A000]: Unsupported feature 'Alter Column Set Default'.
Am I missing here something?

from Snowflake Doc (https://docs.snowflake.com/en/sql-reference/sql/alter-table-column.html):
"To change the default sequence for a column, the column must already
have a default sequence. You cannot use the command ALTER TABLE ...
SET DEFAULT <seq_name> to add a sequence to a column that does not
already have a sequence."
So I guess you have to set the sequence as column default when creating the table.

Related

How to copy SQL column data to new column then drop the original column?

I am following the recommendation in this sql snowflake forum in order to transform an integer data column into a varchar by creating a new column. I want to drop the original integer column when I am done, but doing so always results in the new column no longer working and any future queries erroring out.
For instance, I have test_num is the integer and test_num_to_char is the varchar
alter table test_table
add test_num_to_char varchar as CAST(test_num as varchar)
then
alter table test_table
drop column test_num
select *
from test_table
results in an error message:
SQL execution internal error: Processing aborted due to error 300002:224117369
Is there a different transformation method that removes the dependency on the original integer column so I can drop it?
alter table test_table add test_num_to_char varchar(10);
go
update test_table set test_num_to_char = CAST(recno as varchar);
Try the TO_DECIMAL transformation method.
It's documentation is given here

How to create default constraint on the table of Firebird to assign auto-generated UUID value

I am trying to define a table with a column type of UUID (CHAR(16) CHARACTER SET OCTETS). After reading through the Firebird 3.0 Developer's Guide, I found only context-variables/simple expression/constant are supported. I would like to know if there is a way to define a default constraint on a table to call GEN_UUID() to assign UUID for insertion?
You cannot do this with a DEFAULT clause, as that only allows literals and a select number of what the Firebird documentation refers to as 'context variables'.
To do what you want, you need to create a before insert trigger to generate the value. Something like this:
create trigger bi_yourtable before insert on yourtable
as
begin
new.uuid_column = gen_uuid();
end
Or, if you don't want unconditional generation of the UUID:
create trigger bi_yourtable before insert on yourtable
as
begin
if (new.uuid_column is null) then
begin
new.uuid_column = gen_uuid();
end
end

ERROR: syntax error at or near "modify" - in postgres

I executed this SQL statement in Postgres
alter table user modify column 'distinguishedName1' text;
and
alter table user modify column distinguishedName1 text;
user is the table name
distinguishedName1 is the column name with integer data type.
I wanted to modify the data type to boolean or text or varchar(256) etc based on user's input. But when I run the query I get the error
ERROR: syntax error at or near "modify"
Not sure what is the problem. Help required on right query.
POSTGRES syntax for altering column type :
ALTER TABLE user ALTER COLUMN distinguishedName1 TYPE text;
Try this:
ALTER TABLE "user" ALTER COLUMN distinguishedName1 TYPE text USING code::text;
or
ALTER TABLE "user" ALTER COLUMN distinguishedName1 TYPE text
Also do note that the USING is optional. See the manual here:
The optional USING clause specifies how to compute the new column
value from the old; if omitted, the default conversion is the same as
an assignment cast from old data type to new. A USING clause must be
provided if there is no implicit or assignment cast from old to new
type.
On a side note try to avoid naming your tables as reserved keywords.
alter table user Alter column distinguishedName1 text;
Syntax mistake , for sql server you have to use alter to modify the column of table

Add a column SQL query in Oracle database

I am using Oracle Database (version is 9i) and I want to add a column to a current table in oracle database.
I want to add an integer column to keep track of invalid tries per user, so default value should be 5.
When I try to execute this query in Sql*Plus it gives an error table or view doesn't exist ( I have double checked table name is correct.
ALTER TABLE CustApps_user ADD VALID_TRIES INT DEFAULT 5 NOT NULL;
I guess the error you're getting is ORA-00942. This can mean a number of things, but basically it means the object does not exist in the current scope and context of what you're doing. So for instance it is the error thrown when we attempt to build a view on a table in another schema when we have been granted privileges through a role and not directly.
In your case it probably mean that the table is in another schema. You normally may be accessing it through a view or synonym. You can easily check this by querying the data dictionary:
select owner, object_type
from all_objects
where object_name = 'CUSTAPPS_USER'
alter table
table_name
add
(
column1_name column1_datatype column1_constraint,
column2_name column2_datatype column2_constraint,
column3_name column3_datatype column3_constraint
);
Here are some examples of Oracle "alter table" syntax to add data columns.
alter table
cust_table
add
cust_sex varchar2(1) NOT NULL;
Here is an example of Oracle "alter table" syntax to add multiple data columns.
ALTER TABLE
cust_table
ADD
(
cust_sex char(1) NOT NULL,
cust_credit_rating number
);
You have to add bracket in query:
ALTER TABLE CustApps_user ADD (VALID_TRIES INT DEFAULT 5 NOT NULL);
INT is legal, but it will be converted to NUMBER, so you can also use:
ALTER TABLE CustApps_user ADD (VALID_TRIES NUMBER(38,0) DEFAULT 5 NOT NULL);
or change (decrease) NUMBER precision.

Creating a sequence on an existing table

How can I create a sequence on a table so that it goes from 0 -> Max value?
I've tried using the following SQL code, but it does not insert any values into the table that I am using:
CREATE SEQUENCE rid_seq;
ALTER TABLE test ADD COLUMN rid INTEGER;
ALTER TABLE test ALTER COLUMN rid SET DEFAULT nextval('rid_seq');
The table I am trying to insert the sequence in is the output from another query. I can't figure out if it makes more sense to add the sequence during this initial query, or to add the sequence to the table after the query is performed.
Set the default value when you add the new column:
create sequence rid_seq;
alter table test add column rid integer default nextval('rid_seq');
Altering the default value for existing columns does not change existing data because the database has no way of knowing which values should be changed; there is no "this column has the default value" flag on column values, there's just the default value (originally NULL since you didn't specify anything else) and the current value (also NULL) but way to tell the difference between "NULL because it is the default" and "NULL because it was explicitly set to NULL". So, when you do it in two steps:
Add column.
Change default value.
PostgreSQL won't apply the default value to the column you just added. However, if you add the column and supply the default value at the same time then PostgreSQL does know which rows have the default value (all of them) so it can supply values as the column is added.
By the way, you probably want a NOT NULL on that column too:
create sequence rid_seq;
alter table test add column rid integer not null default nextval('rid_seq');
And, as a_horse_with_no_name notes, if you only intend to use rid_seq for your test.rid column then you might want to set its owner column to test.rid so that the sequence will be dropped if the column is removed:
alter sequence rid_seq owned by test.rid;
In PostgreSQL:
UPDATE your_table SET your_column = nextval('your_sequence')
WHERE your_column IS NULL;
I'm not fluent in postgresql so I'm not familiar with the "CREATE SEQUENCE" statement. I would think, though, that you're adding the column definition correctly. However, adding the column doesn't automatically insert data for existing rows. A DEFAULT constraint is for new rows. Try adding something like this afterwards to populate data on the existing rows.
DECLARE #i Int
SET #i = 0
SET ROWCOUNT 1
WHILE EXISTS (SELECT 1 FROM test WHERE rid IS NULL) BEGIN
UPDATE test SET rid = #i WHERE rid IS NULL
END
SET ROWCOUNT 0