How to cascade the data type of a column of a table in postgres - database

I have a PostgreSQL database and I need to change the data type of a column. There is a view that uses that table. Is there any way to change the data type of the column without deleting the view using ALTER TABLE table_name ALTER COLUMN column_name TYPE new_type.

No, you will have to drop and re-create the view.
The pg_get_viewdef function that calculates the definition of a view will be helpful.
You can do the whole operation in a transaction if you don't want to expose the "view-less" state to concurrent transactions.

Related

How to add a column to in-memory table in DolphinDB?

An in-memory table in DolphinDB:
t=table(10:0,`time`sym`bid`ofr,[TIMESTAMP,SYMBOL,DOUBLE,DOUBLE])
Would like to add a new column count to t, I have tried the function addColumn, it did not work.
addColumn(t,`count,[INT])
execution was completed with exception
Usage: addColumn(table, newColNames, newColTypes). table must be a streaming table or a dfs-based table.
I think you need to use the SQL update statement to add a column to an in-memory table in DolphinDB.

SQL Server 2012: ALTER TABLE ALTER COLUMN MyColumn1 fails because one or more objects access this column

I have a table which contains MyColumn1 with user defined datatype of CustomDatatype INT NOT NULL. I need to change this column to a VARCHAR(5), but I am getting the following errors:
Msg 5074, Level 16, State 1, Line 12
The object 'CustomDatatype' is dependent on column 'MyColumn1'.
Msg 4922, Level 16, State 9, Line 12
ALTER TABLE ALTER COLUMN MyColumn1 failed because one or more objects access this column.
This does not seem to be related to an index or foreign key reference which I would omit to drop before altering the column datatype, this seems to be directly related to the originally used custom defined datatype CustomDatatype on this column.
How can I change MyColumn1's datatype without dropping the CustomDatatype because that could create apocalyptic scenario in my system or dropping the table?
Thanks
Update (2018-07-31): I found a response to the same question in different forum where it states: "...This is the problem with UDT columns. You will need to drop all the columns of this type first in all tables (and may be in functions / SPs, views), then delete the type and create correct type, then re-create all the columns..." (Naomi 2011) from Microsoft (August 2, 2011) web: https://social.msdn.microsoft.com/Forums/sqlserver/en-US/b7bf0f04-dee8-4751-803b-4c3e1f7f7c70/how-to-change-column-nametype-of-user-defined-table-type?forum=transactsql
So far the only viable solutions I came up with are to:
Create a copy of the table (script it out with all
dependencies i.e.: triggers, indexes, keys, etc.) Drop the original
table (which is ok to do even though custom datatypes are specified) and then recreate the table again with the new datatype column.
If the table cannot be dropped (for some reason) rename the old
datatype column, create a new column with the original column name
with the right datatype. Move the data from the old (renamed) column
into the new one (make sure to mind the triggers,etc.). Then force
to recompile all the Sprocs and refresh all the views which are
dependent.
Can you check if there is any Index added for those column?
If yes,
you will need to drop and recreate the index.
once you drop the constraints and index , you can alter the column.
alter table Table1
alter column MyColumn1 varchar(5);
Then re-create constraints and index.
So far the only viable solutions I came up with are to:
1) Create a copy of the table (script it out with all dependencies i.e.: triggers, indexes, keys, etc.) Drop the original table (which is ok to do even though custom datatypes are specified) and then recreate the table again with the new datatype column.
2) If the table cannot be dropped (for some reason) rename the old datatype column, create a new column with the original column name with the right datatype. Move the data from the old (renamed) column into the new one (make sure to mind the triggers,etc.). Then force to recompile all the Sprocs and refresh all the views which are dependent.

Turn off system versioning of temporal table that is used in indexed views

I have a table that is used by a number of indexed views that I want to convert to a temporal table. All the commands to add the necessary columns and to set SYSTEM_VERSIONING=ON with a history table all succeeded. But now I can not disable system versioning with this command
ALTER TABLE [dbo].[MyTable] SET(SYSTEM_VERSIONING = OFF);
I get the error
Cannot ALTER 'MyTable' because it is being referenced by object 'MyIndexedViewThatReferencesMyTable'.
It seems impractical to delete all views, alter MyTable, then recreate all views in order to accomplish this. Is there another way to get this table back to non-temporal?

effect of table hint on view and table used inside that view

We can use table hints like NOLOCK, PAGLOCK
but my question is... If table hints not used in view definition
but used in query with view like
SELECT * FROM myview WITH(nolock)
will this effect on table used inside that view
Logically it should ... cause the view query is going to run against the table as view doesn't store any data by itself (Unless it's a Indexed/Materialized view).
Per Docmentation TABLE HINTS gets propagated to actual table. Little excerpt from the document [Remarks Section]
All lock hints are propagated to all the tables and views that are
accessed by the query plan, including tables and views referenced in a
view. Also, SQL Server performs the corresponding lock consistency
checks.
But it also says that TABLE HINTS won't apply for computed columns computed from some other table column expression.
If a table contains computed columns that are computed by expressions
or functions accessing columns in other tables, the table hints are
not used on those tables and are not propagated. For example, a NOLOCK
table hint is specified on a table in the query. This table has
computed columns that are computed by a combination of expressions and
functions that access columns in another table. The tables referenced
by the expressions and functions do not use the NOLOCK table hint when
accessed.
TEST:
create a table named employee as below
create table employee(id int not null, name varchar(20));
Inserted around 1572866 rows. So it takes sme time to execute.
create a view as below (see view definition has no table hints in place)
create view testview
as
select * from employee;
Select from view with table hint
select * from testview with(paglock,holdlock)
while this is on run .. try doing an insert on employee table and see yourself how insert will keep waiting for the select .. rom view to complete.

Can you change the column length in a view in SQL Server 2000?

Not sure if this is even allowed, but if so, can someone tell me what the T-SQL is? I've tried the following but to no avail.
alter [View_Name]
alter column [Coln_Name] [New size/length] not null
GO
Any help is appreciated. Thanks!
Not directly.
This is derived automatically from the column expression. You can CAST the expression in the View SELECT list to a particular datatype though.
You would need to change the column length in the underlying table, or to change the SELECT statement forming the view to CAST or CONVERT the column to a different length data type.
Views are ways to see data in other tables; typically the data is simply whatever is in the underlying table, so you would need to change the column there.
However, you can have views that do things like cast() or convert(); these are typically a bad idea, becuase the data needs to be re-fetched every time the view is used, and these operations add overhead. In the design of the view, you can decide to cast as another data type, or do any transformation you would like - but it has overhead, and will not alter the original data.
If you know what the current view selects, you can use something like:
Alter view Viewname[cloumn] as Select cast(original_data as varchar(n)) from Original_Table
I just ran into the same situation. What I did was:
Change the column size in the table that the view looks at.
Create a script to recreate the view (if you don't already have one).
Delete the view
Use the script to recreate the view.
After that the column sizes in the view were the same as the changes I made to the underlying table.
You can not alter the column size in a view as a view is derived from other table. So if you need to change the column size, change the column size of the table. To change the column size use ALTER TABLE as :
ALTER TABLE [Table_Name]
ALTER COLUMN [Column_Name] Data_Type(Size)
After changing the column size you might need to drop the view and recreate it again.
If the length shown in the view doesn't match the underlying table then drop and recreate the view.
Use something like this to investigate the column lengths in the table and view
SELECT o.[name], *
FROM sys.all_columns c
INNER JOIN sys.objects o on o.[object_id]=c.[object_id]
WHERE c.[name] = 'OML'
-- AND c.[max_length]=11
ORDER BY O.[name];
To get the Drop and Create sql I use SSMS and right click context menu (on the view in the Object Explorer), then go to 'Script View as', and 'DROP And CREATE To'.

Resources