Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 577
Chinesinho Avatar asked Jan 05 '12 15:01

Chinesinho


People also ask

Can we ALTER column in view?

The altered view replaces the existing view, so you cannot modify specific columns in a view. A view is a virtual table based on the result set of a SELECT query or a UNION of such queries. For more details on views, see Defining and Using Views.

How do you change the length of a column in SQL Server?

SQL - Modify Column Data Type and SizeALTER TABLE Employee ALTER COLUMN FirstName VARCHAR(50); The following will change the size in the Oracle database. ALTER TABLE Employee MODIFY (FirstName VARCHAR2(50)); The following will change the size in the PostgreSQL database.

Can we modify views in SQL?

In Object Explorer, click the plus sign next to the database where your view is located and then click the plus sign next to the Views folder. Right-click on the view you wish to modify and select Design.


3 Answers

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.

like image 61
Martin Smith Avatar answered Oct 02 '22 15:10

Martin Smith


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.

like image 31
David M Avatar answered Oct 02 '22 16:10

David M


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
like image 41
David Manheim Avatar answered Oct 02 '22 15:10

David Manheim