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

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

Related

Snowflake: Trying to make a column to use as default the value from a 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.

Alter Table Column Type Does not Work in Aster - I am Probably Screwing It Up

I am using Aster as there are some groovy Random Forest Functions to use. My dependent, or response variable, is a boolean dichotomous variable; a 0 or 1.
When I run it through the Random Forest Function of choice it creates a predicted value of the response variable. It calls this variable prediction and it automatically creates it as a VARCHAR(REALLY BIG INTEGER IN HERE).
To do some of my calculations I simply wish to cast or convert it to an integer from a string. All of the resulting character strings are either a 0 or a 1:
alter table a0q892.zf_predict alter column prediction int;
does not work. The error message I receive is:
Executed as Single statement.
Failed [34 : 42000] [AsterData][ASTERJDBCDSII](34) ERROR: syntax error at or near "int" ()
I am pretty sure there are lots of fancy & elegant ways to do this. But I would think I could simply just make it an integer for future calculations?
As per the aster docs, there are limited options to manipulate colums. You cannot change a column data type.
However aster allows you to change the size of a varchar column. You mentioned that you want to cast to INTEGER, but I guess that in your use case VARCHAR(1) would be fine too. If yes, then you can go :
ALTER TABLE a0q892.zf_predict ADD prediction VARCHAR(1);
If you really need an INTEGER (or any other type than VARCHAR(n)), then you have to proceed the old way :
create a new column in the table with the correct type
fill it from the old column
drop the old column
rename the new column
SQL Aster :
ALTER TABLE a0q892.zf_predict ADD prediction_new int;
UPDATE TABLE a0q892.zf_predict SET prediction_new = CAST(prediction AS int);
ALTER TABLE a0q892.zf_predict DROP prediction;
ALTER TABLE a0q892.zf_predict RENAME prediction_new TO prediction;

Changing datatype of a column in database

I have to change the datatype of a column from CHAR to INTEGER in database. I used the following query:
ALTER TABLE cert_request
ALTER column CERT_REQUEST_NBR SET TYPE(INTEGER);
however it's showing error in the keyword "column" and giving the error code as -104 on execution. Following is the error log;
Error: DB2 SQL Error: SQLCODE=-104, SQLSTATE=42601, SQLERRMC=ALTER TABLE CERT_REQUEST ALTER CERT_R;BEGIN-OF-STATEMENT;<create_proc>, DRIVER=3.50.152
SQLState: 42601
ErrorCode: -104
I also tried the using the Modify keyword, but it isn't identified in my database.
I also tried the following query but again of no use.
alter table CERT_REQUEST alter column cert_request_nbr integer
It gives the following error
Error: DB2 SQL Error: SQLCODE=-104, SQLSTATE=42601, SQLERRMC=integer;umn cert_request_nbr;<alter_col_action1>, DRIVER=3.50.152
SQLState: 42601
ErrorCode: -104
Please provide me some suggestions.
Use MySQL Type Conversion or ALTER
ALTER TABLE cert_request MODIFY CERT_REQUEST_NBR INTEGER;
Or try MySQL Type Conversion
CAST(CERT_REQUEST_NBR AS UNSIGNED)
You have to use MODIFY not ALTER in MYSQL
ALTER TABLE cert_request
MODIFY CERT_REQUEST_NBR INTEGER;
And in DB2 you can try like this:
ALTER TABLE cert_request ADD COLUMN Temporary INTEGER;
UPDATE cert_request SET Temporary=CAST(CERT_REQUEST_NBR AS INTEGER);
ALTER TABLE cert_request DROP COLUMN CERT_REQUEST_NBR;
ALTER TABLE cert_request CHANGE Temporary CERT_REQUEST_NBR INTEGER;
Have you tried:
ALTER TABLE `cert_request` CHANGE `CERT_REQUEST_NBR` `CERT_REQUEST_NBR` INT(10) NOT NULL;
Be careful any data that is not a integer will be lost.
It will be helpful if you specify which database you are using . In oracle the solution should be
Alter table cert_request modify CERT_REQUEST_NBR number(30)
In db2(based on the error you provided, I believe your database is db2) the solution is
Alter table cert_request alter column CERT_REQUEST_NBR set datatype decimal(30)
You are missing the word 'Data' between 'Set' and 'Type'.
It should be like this:
ALTER TABLE cert_request
ALTER column CERT_REQUEST_NBR SET DATA TYPE(INTEGER);
May be, It happen due to varchar data present in field values.
Delete that data, and then run your query
It is because your column is having data which are not integer. You will either need to remove those data and try or you can run a query mentioned in above post which will make your data null which are not int.

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.

How to alter a column datatype for derby database?

I am trying to alter a datatype for a derby db column. The current price column is set as DECIMAL(5,0). I would like to alter it to DECIMAL(7,2). I did this :
alter table item alter column price set data type DECIMAL(7,2);
But it did not work, and showing the error:
Error: Only columns of type VARCHAR may have their length altered.
May I know how is it possible to alter it? Thank you.
Here is the Derby SQL script to change column MY_TABLE.MY_COLUMN from BLOB(255) to BLOB(2147483647):
ALTER TABLE MY_TABLE ADD COLUMN NEW_COLUMN BLOB(2147483647);
UPDATE MY_TABLE SET NEW_COLUMN=MY_COLUMN;
ALTER TABLE MY_TABLE DROP COLUMN MY_COLUMN;
RENAME COLUMN MY_TABLE.NEW_COLUMN TO MY_COLUMN;
I think you can do like this:
ALTER TABLE SCHEMA.TABLE ALTER "COLUMN-NAME" SET DATA TYPE VARCHAR(255);
(column-Name SET DATA TYPE VARCHAR(integer)) for Datatype String as an example...
Here's a slightly more complicated way to alter the column's data type in this fashion:
Add a new column, of the desired data type
Issue "update ... set new-column = old-column to copy the data from the old column to the new column
drop the old column
Rename the new column to have the name of the old column.
Slightly more steps, but in the end the effect will be the same.
If you have trouble working out the exact details of the SQL to do this, let us know and we'll help.
You can alter table like this:
ALTER TABLE [table] ALTER COLUMN [column] SET DATA TYPE [type];
Or in Rails, just use:
change_column :table_name, :column_name, :integer
Posgtes Solution :
ALTER TABLE prices_table ALTER price_column TYPE decimal (7,2 )