Menu
  • HOME
  • TAGS

SQL - Add column and set value after IF NOT EXISTS

sql,sql-server-2008-r2,alter-table

When SQL Server processes a script, there are two phases. The first phase is compilation. The second is execution. The error that you are getting is a compilation error. All the code is compiled, regardless of the if conditions. So, you are getting an error because the column doesn't exist....

Alter Table Move command - ORACLE

oracle11g,alter-table,tablespace

According to the Database SQL Language Reference: The move_table_clause lets you relocate data of a nonpartitioned table or of a partition of a partitioned table into a new segment, optionally in a different tablespace, and optionally modify any of its storage attributes. Therefore, with your first statement Oracle will move...

Alter AUTO_INCREMENT value by select result

mysql,sql,select,auto-increment,alter-table

Try this: SELECT `AUTO_INCREMENT` INTO @AutoInc FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'database_name' AND TABLE_NAME = 'another_table_name'; SET @s:=CONCAT('ALTER TABLE `database_name`.`table_name` AUTO_INCREMENT=', @AutoInc); PREPARE stmt FROM @s; EXECUTE stmt; DEALLOCATE PREPARE stmt; ...

Renaming multiple columns in one statement with PostgreSQL

sql,postgresql,ddl,alter-table,table-rename

No. While other actions can be combined, that's not possible with RENAME. As per documentation: All the actions except RENAME and SET SCHEMA can be combined into a list of multiple alterations to apply in parallel. Since RENAME is a tiny operation on a system catalog, there is no harm...

Shifting id values of existing rows in mysql

mysql,phpmyadmin,auto-increment,alter-table,value

UPDATE myTable SET id=id+1923 Simple as that...

SQL Invalid ALTER TABLE option

sql,oracle,datetime,add,alter-table

DATETIME is not a valid column data type. If you're trying to add a default value, you need to specify a DEFAULT. If you are trying to use a date format, please use one of the datetime or interval data types. If you need to store date and time, the...

Drop column from multiple tables in particular schema in SQL Server

sql-server-2008-r2,alter-table,alter

back up first DECLARE @SqlStatement VARCHAR(MAX) SELECT @SqlStatement = COALESCE(@SqlStatement, '') + 'ALTER TABLE [stg].' + QUOTENAME(TABLE_NAME) + ' DROP COLUMN [RowId];' + CHAR(13) FROM INFORMATION_SCHEMA.TABLES t INNER JOIN sys.objects o ON o.name=t.TABLE_NAME INNER JOIN sys.columns c ON o.object_id= c.object_id WHERE TABLE_SCHEMA = 'stg' and c.name='RowId' PRINT @SqlStatement Once you...

mysql convert float to double

mysql,double,alter-table

You can't. The FLOAT and DOUBLE types represent approximate numeric data values. They are stored in such a way that they can contain a wide range of numbers (from very big to very small), but at the cost of being a bit inaccurate. See Floating point types for more information....

Error when link 2 table in sql server

sql-server,alter-table

This could have many reasons. Do you have a type missmatch between those columns? Is every customer in products also in your customer table? You can check the second part by using this: SELECT DISTINCT p.customer_id FROM products as p LEFT JOIN customers as c ON p.customer_id = c.customer_id WHERE...

Having error for alter table to add constraint

sql,oracle,constraints,add,alter-table

If you only want the constraint to apply for future data changes, as you said in a comment, then you can make it ignore other existing values with the NOVALIDATE clause: ALTER TABLE ACCOUNT ADD CONSTRAINT AccountCK_Type CHECK (TYPE IN('Saving','Credit Card','Home Loan','Personal Loan', 'Fixed Deposit','Current','iSaver')) ENABLE NOVALIDATE; Otherwise, you will...

Drop all columns in a table if their names follow a specific pattern in SQL Server

sql-server,alter-table

Try this. Use Dynamic SQL to generate Drop scripts of columns which is following specific pattern DECLARE @sql NVARCHAR(max)='' SELECT @sql += ' Alter table ' + TABLE_NAME + ' DROP COLUMN ' + COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'tablename' AND TABLE_SCHEMA = 'SchemaName' AND TABLE_CATALOG = 'DatabaseName' AND...

In Oracle, How do I alter a user that have a special character in the name?

oracle,special-characters,alter-table,user-accounts

You can enclose it in double quotes, but you need to match the case from dba_users, which is usually uppercase but may not be if it was created quoted: alter user "FOO.MOCK" account unlock; Read more about quoted and non-quoted identifiers....

How can I change the datatype of a column from integer to text in SQL Server?

sql-server,delphi,delphi-7,alter-table

ALTER TABLE is precisely what you want to do. Your SQL might look something like this: ALTER TABLE dbo.MyTable ALTER COLUMN MyColumn VARCHAR(20) NOT NULL; Note that if you have columns that reference this one, you will have to update those as well, generally by dropping the foreign key constraints...

Add auto-computed column in SQL

mysql,alter-table

AFAIK. MySQL doesn't have support for built-in computed column like in SQL Server. With that, you have two options. Add the column column_1 using ALTER statement ALTER TABLE users ADD column_1 ....; Then UPDATE it like UPDATE users SET column_1 = numerator/denominator; (OR) CREATE A VIEW like below and then...

Is it faster to alter multiple columns in the same query?

mysql,sql,alter-table

Yes, it's faster. You only have to make one call to the database API, and it only has to parse the query once. However, for ALTER TABLE queries, performance usually isn't a concern. You shouldn't be doing this frequently, only when you redesign your schema. But if your question were...

Oracle sql: Drop table column shows error: invalid identifier? [closed]

oracle,multiple-columns,ddl,alter-table

You have to use the following statement to drop more than one columns: alter table table_name drop (col_name1, col_name2); Then you have to delete the "column" keyword in your statement. ...

How to alter a collection structure in mongodb?

php,mongodb,collections,alter-table

Well you didn't answer the comment that was given, but it is important for the reasons that will be explained. So If you truly have "millions of documents" that you now have to alter and do this quickly then you have a clear problem to face. Since there is no...

ORA-00905: missing keyword - Alter table modify column not working in Oracle 10g

mysql,sql,database,oracle,alter-table

Do this ALTER TABLE table_name MODIFY column_name datatype ...

How to search for a table with an undefined column

cassandra,alter-table,cqlsh

You cannot select for values of null in Cassandra.

How do I get rid off error on adding Foreign Key to a table which contains data

sql-server,foreign-keys,constraints,alter-table

You probably have inconsistent data in your tables try running select * from ServiceDetails where OCF_ID not in (select OCF_Internal_ID from OCF_Commerce) This will output all rows with ID absent in master table...

Why receiving a SYNTAX error while ALTERing a SQL table

sql,sql-server,alter-table

Don't use parentheses. ALTER TABLE DSPCONTENT01.dbo.RADRESULTSTOTALS2 ADD [TEST1] INT, [TEST2] INT ...

Alter Microsoft Access Column from MEMO to TEXT(255)

sql,ms-access,alter-table

Do it in two steps: update MyTable set MyField = LEFT(MyField, 255): ALTER TABLE MyTable ALTER COLUMN MyField TEXT(255); ...

change the default value of an integer column

mysql,database,alter-table

You can alter your table ALTER TABLE `userdetails` CHANGE `uid` `uid` INT(5) NOT NULL; DEMO Also its better if you define it as AUTO_INCREMENT so every row will have a unique no. ALTER TABLE `userdetails` CHANGE `uid` `uid` INT(5) NOT NULL AUTO_INCREMENT; ...

how to set column property NOT NULL in SQL Server

sql-server,alter-table

Possible data present in this table and null values in this column. Try to update table, set values in this column to 1 for example before altering. Or just remove all data from the table before altering.

Prevent duplicate entries in a database table based on 2 columns

mysql,sql,duplicates,unique,alter-table

You simply have to define a unique constraint containing both columns: alter table xtu_datas_competition add unique index(id_competition,email); Hope this will help you....

Oracle - Setting column to NULL do not remove CHECK contraint

sql,oracle,alter-table

Well, I come with this solution alter table USER rename column FIRST_NAME to FIRST_NAME_OLD; alter table USER ADD FIRST_NAME VARCHAR2(40 CHAR) NULL; update USER set first_name=first_name_old; alter table USER drop column FIRST_NAME_OLD; I know It may have performance issue on large tables, but that is not my case...

Create column and insert into it within the same transaction?

sql-server,tsql,transactions,alter-table

Another alternative, if you don't want to split the code into separate batches, is to use EXEC to create a nested scope/batch: Print 'Beginning Upgrade' Begin Transaction -- -------------------------------------------------------------------- USE [MyDatabase]; /* Widgets now can be ordered and the order can be modified */ ALTER TABLE [dbo].[Widgets] ADD [IndexNumber] [int]...

Wrong column values SQL Server Views after table schema update

sql-server,views,alter-table

After updating schema, you should refresh the view metadata for any and all views dependent on the schema: EXEC sp_refreshview @viewName Where the @viewName variable holds the name of the view. You can use this stored procedure in a script that can grab the views dependent on the table(s) in...

AFTER keyword in mysql ALTER table query

mysql,alter-table

Use it when you want a new column you add to an existing table should not be at the end of the column list but at a specific position. Example: Existing table persons persons: id firstname, lastname, password, permissions If you now want to add a new column fullname you...

Change table name for admin grid collections

magento,collections,alter-table

It is model name that you changed, if you like to change the table name then go to config.xml and search for then replace the argument inside that table from <table>banner</table> to <table>chilly_banner</table> then everywhere in your module, chilly table will use automatically...

Change column data type from text to array by splitting on delimiter

postgresql,types,alter-table

If you just want to select that as an array: select string_to_array(your_column, ' ') from your_table; If you permanently want to change the data type to e.g text[] use this: alter table your_table alter your_column type text[] using string_to_array(your_column, ' '); ...

Change engine of a table having foreign key

mysql,foreign-keys,alter-table

You can't change the engine for the table to one which doesn't support foreign keys without removing the foreign key constraints. Documentation for the MEMORY engine If the new engine doesn't support them, they simply can't exist, so you need to remove them before making the appropriate changes....