I working on an auto synchronization project where I want to get the data types from a View on SQL Server 2008 R2 and compare it to a table in the same database. I am familiar with syscolumns, However, this only appears to work with tables, not Views. Any suggestions or references would be greatly appreciated.
Essentially, if View_A has a new column added to it, I need to add the column 开发者_C百科to Table_A with the same properties as View_A so that all data types, lengths, and fields on Table_A are always the same as View_A.
Thanks
I'm not sure about syscolumns only showing columns of base tables, but you can use this query:
SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'YourView'
sys.columns
includes column information for views:
CREATE VIEW dbo.Test_View
AS
SELECT * FROM sys.objects
SELECT *
FROM sys.columns
WHERE object_id = OBJECT_ID('dbo.Test_View')
The columns in sys.columns
are for both views and tables.
So, you should be able to do the same as a table, i.e.
SELECT *
FROM sys.columns
WHERE object_id = OBJECT_ID('MyViewName')
精彩评论