how to alter length of a table column used in views - database

i want to change length of a table column of postgresql database table.
this is the error :
ERROR: cannot alter type of a column used by a view or rule
DETAIL: rule _RETURN on view av_viewname depends on column "orderref
is there way to to change column length without destroy views?

Please check this topic and then proceed for your alteration.
You cannot change your columns' size without dropping it. Yet, first you have to get the definition of your view.

Related

Snowflake change data type of column in table, varchar -> Date

How do I change a varchar data type to a DATE for the column datatype in a table?
Tried the following:
ALTER TABLE table.name.here MODIFY COLUMN insert_dt DATE;
I just get
SQL compilation error: cannot change column INSERT_DT from type VARCHAR(16777216) to DATE
Unfortunately this sort of data type change is not allowed, generally your best option is to
Add a new column with a temp name, with the new data type
Run an update statement to set the new column to the old column's value (with any required transformations)
Rename the columns, and drop the old column if desired.
It is also sometimes easiest to do this change in a clone or CTAS table and then do an ALTER TABLE SWAP WITH.
Note that a full table update like this does mean recreating micro-partitions, which is generally ok (if a little slow), but you may want to keep an eye on if this affects your clustering. This is easier to control in a CTAS approach because you can explicitly maintain the ordering in an ORDER BY clause.

Trigger to delete column only if all values are null

How can I create a Trigger that delete a column only if all values on a specific field are null?
What do you mean "create a trigger"? Do you mean that you want an update/delete trigger that would delete the column at the point when all the values become NULL?
If so, that is not possible. "Deleting a column" is really alter table drop column. In the Remarks section for the create trigger documentation:
Additionally, the following Transact-SQL statements are not allowed
inside the body of a DML trigger when it is used against the table or
view that is the target of the triggering action . . .
ALTER TABLE when used to do the following: Add, modify, or drop columns. Switch partitions. Add or drop PRIMARY KEY or UNIQUE
constraints.
Hence what you want to do is not possible. Further, I don't understand why you would want to do it. A data-driven change to the data structure seems strange. I am guessing that your data structure really wants to be a junction/association table, but you don't provide enough information to speculate further.

Cannot use a CONTAINS or FREETEXT predicate on table or indexed view

I am trying to modify a stored procedure ( adding a new column in select statement) but I am getting this error:
Cannot use a CONTAINS or FREETEXT predicate on table or indexed view 'vwPersonSearch' because it is not full-text indexed.
When I try to create a Full text index on view 'vwPersonSearch' using SQL server 2008 R 2 management studio, I am getting this error:
A unique column must be defined on this table/view.
Please suggest solution to it
To create a full text index, you must specify a key index, which must be a unique, single-key, non-nullable column. An integer column type is recommended for best performance.
See http://technet.microsoft.com/en-us/library/ms187317.aspx for more details.
You may alter a column to be unique if that's one that could be or add an id of some sort to do that part.

Sybase - Change a table column datatype on an IDENTITY column which is a User Definied Datatype

I'm pretty good around Oracle but I've been struggling to find a decent solution to a problem I'm having with Sybase.
I have a table which has an IDENTITY column which is also a User Defined Datatype (UDD) "id" which is numeric(10,0). I've decided to replace the UDD with the native datatype but I get an error when I do this.
I've found that the only way to do this is:
Rename the original table (table_a to table_a_backup) using the procedure sp_rename
Recreate the original table (table_a) but use native data types
Copy the contents of the backup table to the original (i.e insert into table_a select * from table_b)
This works however I have over 10M records and it eventually runs out of log segment and halts (I can't increase the segment any more due to physical requirements).
Does anybody have a solution, preferably not a solution which would involve processing the records as anything but one large set?
Cheers,
JLove
conceptually, something like this works (in Sybase ASE 12.5.x) ...
do an "alter table drop column" on your current ID column
do "alter table add column" stmt to add new column (w/ native datatype) with IDENTITY attribute
Note that the ID field might not have the same numbers, so be very wary of doing the above if the ID field is used as an explicit or implicit key to other tables.

SQL Server Alter Computed Column

Does anyone know of a way to alter a computed column without dropping the column in SQL Server. I want to stop using the column as a computed column and start storing data directly in the column, but would like to retain the current values.
Is this even possible?
Not that I know of but here is something you can do
add another column to the table
update that column with the values of the computed column then drop the computed column
If you need to maintain the name of the column (so as not to break client code), you will need to drop the column and add back a stored column with the same name. You can do this without downtime by making the changes (along the lines of SQLMenace's solution) in a single transaction. Here's some pseudo-code:
begin transaction
drop computed colum X
add stored column X
populate column using the old formula
commit transaction
Ok, so let me see if I got this straight. You want to take a column that is currently computed and make it a plain-jane data column. Normally this would drop the column but you want to keep the data in the column.
Make a new table with the primary key columns from your source table and the generated column.
Copy the data from your source table into the new table.
Change the column on your source table.
Copy the data back.
No matter what you do I am pretty sure changing the column will drop it. This way is a bit more complex but not that bad and it saves your data.
[Edit: #SqlMenace's answer is much easier. :) Curse you Menace!! :)]

Resources