Issue with setting default value for a column (sql server 2005) - sql

The following syntax won't work:
ALTER TABLE MyCustomers
ALTER COLUMN CompanyName SET DEFAULT 'A. Datum Corporation'
per url, http://msdn.microsoft.com/en-us/library/ms174123.aspx
Error message:
Incorrect syntax near the keyword 'set'.
What gives?
Thanks.

Per this question:
How to set a default value for an existing column
The following will work on SQL Server, and has the added benefit of naming the constraint:
ALTER TABLE MyCustomers
ADD CONSTRAINT DF_SomeName DEFAULT 'A. Datum Corporation' FOR CompanyName;

Related

Alter column length in SQL

ALTER TABLE tableName
MODIFY COLUMN columnName VARCHAR (256);
Error :
Incorrect syntax near 'VARCHAR'
The syntax for modifying a column definition is depending on used database. For Microsoft SQL Server for example, you would need to replace MODIFY COLUMN with ALTER COLUMN.
For better help you should specify your database type.

Updating current time for a column in SQL

So i am trying to store the time at which any changes were made to any row.
I was using the following query:
ALTER TABLE SPRD_MGMT_INP_INDEX_CHANGE_RATE
CHANGE "UPLOAD TIME"
"UPLOAD TIME" TIMESTAMP NOT NULL
DEFAULT CURRENT_TIMESTAMP
ON UPDATE CURRENT_TIMESTAMP;
I am getting the following error:
SQL compilation error: syntax error line 2 at position 0 unexpected 'CHANGE'. Please help
The syntax uses alter column and set, but the bigger issue is that you can't change the default of a column in Snowflake with the exception noted here:
https://docs.snowflake.com/en/sql-reference/sql/alter-table-column.html
In that page, it states the following:
Change the default for a column, unless the default is a sequence.
In that table for the action quoted above it has a checkmark by Unsupported.
You will need to create a new table with this default. You can then copy your rows from the original table using an insert from select.
You can specify the default in your new table like this:
create or replace table T1
(COL1 int, COL2 string, UPLOAD_TIME timestamp_tz default current_timestamp());

Add generated column to an existing table Postgres

I am trying to add a generated column to an existing table with this script.
alter table Asset_Store add column
md5_hash VARCHAR(100) GENERATED ALWAYS AS
(CAST(UPPER(
case
when OR_ID is not null then MD5(cast(OR_ID as varchar(100)))
when Asset_ID is not null then MD5(Asset_ID)
else null
end
) as VARCHAR(100)))
STORED
;
but I am getting an error:
SQL Error [42601]: ERROR: syntax error at or near "("
Position: 88
ERROR: syntax error at or near "("
Position: 88
ERROR: syntax error at or near "("
Position: 88
What is the issue? I don't get it.
In the schema of my Asset_Store table the column
OR_ID is int and Asset_ID is varchar(100).
I guess it expects a slightly different syntax... but what is the right syntax?
Your syntax is correct. Your version of PostgreSQL apparently is not.
In version 12:
create table asset_store(or_id text, asset_id text);
alter table Asset_Store add column
md5_hash VARCHAR(100) GENERATED ALWAYS AS
(CAST(UPPER(
case
when OR_ID is not null then MD5(cast(OR_ID as varchar(100)))
when Asset_ID is not null then MD5(Asset_ID)
else null
end
) as VARCHAR(100)))
STORED
;
ALTER TABLE
Time: 17.678 ms
More general, simplified command
ALTER TABLE "items"
ADD COLUMN "revenue" numeric
GENERATED ALWAYS AS ("price" * (1-"discount")) STORED;

alter data type of existing column from bigint to varchar in Apache derby

I am using Apache derby database v 10.9.1.0. There is one existing table Country-having column LawID of type bigint. It contains records having integer data only. Due to some business reason, I need to alter its data type from 'bigint' to 'varchar' . I tried following two ways to alter existing table. But both ways did not work.
a. first way
ALTER TABLE Country ADD COLUMN LawID_NEW VARCHAR(50);
UPDATE Country SET LawID_NEW = LawID;
ALTER TABLE Country DROP COLUMN LawID;
RENAME COLUMN Country.LawID_NEW TO LawID;
It shows message like :Columns of type 'VARCHAR' cannot hold values of type 'BIGINT'.
b. second way
ALTER TABLE Country ALTER LawID SET DATA TYPE VARCHAR(50);
It shows error message like : Invalid type specified for column 'LawID'. The type of a column may not be changed.
Any help related to correct alter query is highly appreciated, Thanks
I think the first method would work with this change:
UPDATE Country SET LawID_NEW = TRIM(CHAR(LawID));
ALTER TABLE tablename MODIFY columnname VARCHAR(20);
This works in mysql. Give it a try.
Or
ALTER TABLE table CHANGE columnname columnname VARCHAR(20);

Modify default option of a column

I am trying to change the default value of 2 columns from 'N' to 'Y' using the below query, but it throws a error. Any idea whats wrong in here. Error: Invalid Alter table option.
Alter Table USER
Modify
CONTACT_FLAG Default 'Y',
APPROVAL_FLAG Default 'Y' ;
You have to enclose the columns like this
alter table
table_name
modify
(
column1_name column1_datatype,
column2_name column2_datatype,
column3_name column3_datatype,
column4_name column4_datatype
);