Query a view to get its column names - sql

I have a large number of SQL 2008 R2 views. I would like to know which database fields are referenced in the views.
Is there a way that I can query the schema to list out these column names?

Use this query against sys.sql_dependencies.
SELECT
ViewName = O.name,
ReferencedTableName = X.name,
ReferencedColumnName = C.name,
T.is_selected,
T.is_updated,
T.is_select_all,
ColumnType = M.name,
M.max_length,
M.precision,
M.scale
FROM
sys.sql_dependencies AS T
INNER JOIN sys.objects AS O ON T.object_id = O.object_id
INNER JOIN sys.objects AS X ON T.referenced_major_id = X.object_id
INNER JOIN sys.columns AS C ON
C.object_id = X.object_id AND
C.column_id = T.referenced_minor_id
INNER JOIN sys.types AS M ON
M.system_type_id = C.system_type_id AND
M.user_type_id = C.user_type_id
WHERE
O.type = 'V'
ORDER BY
O.name,
X.name,
C.name

You can look at the view definition and see the referenced tables there. For that you can simply use sp_helptext like so:
sp_helptext 'vStores'

You can retrieve the view defintion from the metatdata in 'sys.objects', see also this answer:
Is there a way to retrieve the view definition from a SQL Server using plain ADO?
That won't give you the underlying tables directly, though.
SQL Server does this trick itself when you define an object as having Schema Binding: when any objects are changed that are referenced by other objects using schema binding, that change is stopped and an error is given. Perhaps you can look into how SQL Server keeps track of those references to see what columns are used in a view. More on Schema binding here: https://www.mssqltips.com/sqlservertip/4673/benefits-of-schemabinding-in-sql-server/

Related

How do I get a table of a database's stored procedures (excluding system stored procedures) along with its definition?

This is what I have now. This gets me a list of the stored procedures, but I need an additional column with their respective definition.
SELECT
name
FROM sys.objects
WHERE type = 'P'
order by name
I've also tried adding another line below the SELECT (definition as OBJECT_DEFINITION).
Here is how to get the definition and stored procedure name in Sql Server
SELECT o.name, m.definition
FROM sys.objects o
INNER JOIN sys.sql_modules m
ON o.object_id = m.object_id
WHERE o.type = 'P';

Get SQL Table Info

How can I get SQL table information from a linked server? I have a table called "Address" and it is on a remote server which is linked under "Server Objects"->"Linked Servers". I need to know the information about the table. I tried exec sp_help 'Address'
but it doesnt work with remote servers.
You can access the INFORMATION_SCHEMA views from a linked server using the fully qualified domain name. So, data types and columns, for example, would be something like this:
SELECT * FROM MYLINKEDSERVER.MYDATABASE.INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'Address'
You can use OpenQuery with system tables on the remote database. Here is a basic example, but you build it out to query other system tables to get dependencies etc.
Select *
From OpenQuery([TargetServer], 'Select ss.name As SchemaName,
so.name As OjbectName,
sc.name As ColumnName,
st.name As ColumnDataType
From [TargetDatabase].sys.sysobjects so
Join [TargetDatabase].sys.syscolumns sc
On so.id = sc.id
Join [TargetDatabase].sys.schemas ss
On so.uid = ss.Schema_ID
Join [TargetDatabase].sys.types st
On sc.xtype = st.system_type_id
Where so.name = ''TableName''')

Get only table columns with DbConnection.GetSchema method

With C# and SQL Server 2005 and by using DbConnection.GetSchema() method, I want to get all a table's columns (not of views) only. I have found two collection names related to this
Columns that returns table and views' columns
ViewColumns returns all the view's columns
Neither of above two returns table columns only, nor they have any property to filter Table-columns.
Any help is respected.
I don't see any easy way to do this with this particular API you're trying to achieve this with - but why not just use a query like this to get your information?
SELECT
c.name AS 'ColumName',
ty.Name AS 'TypeName',
c.max_length,
c.is_identity,
c.is_nullable,
t.name AS 'TableName'
FROM sys.columns c
INNER JOIN sys.types ty ON c.user_type_id = ty.user_type_id
INNER JOIN sys.tables t ON c.object_id = t.object_id
Just load that into a SqlCommand and execute it against the open connection you have and read the result into some DataTable or other structure for your use. This gives you only table columns - and all of them.

SQL - LINQ2SQL - Accessing sys.table

I have Googled this without any success.
Here is the SQL query and I wish to turn it into LINQ2SQL.
SELECT c.name FROM sys.tables t JOIN sys.columns c ON t.Object_ID = c.Object_ID WHERE t.Name = 'Address'
At first I tried accessing just the sys.table like this:
from n in _db.sys.table where n.table select n
_db = new DBDataContext(ConfigurationManager.ConnectionStrings["DATABASE"].ConnectionString);
I am however receiving the following error - Cannot resolve symbol 'sys'
Can you even do this in LINQ2SQL?
Thanks in advance.
Clare :-)
You could also develop a View to access sys.tables, and map that View...
A stored proc can work well too - you just drag it in and it creates a new type with all the right in and out parameters.

What is syncobj in SQL Server

When I run this script to search particular text in sys.columns and I get a lot of "dbo.syncobj_0x3934443438443332" like rows.
SELECT c.name, s.name + '.' + o.name
FROM sys.columns c
INNER JOIN sys.objects o ON c.object_id=o.object_id
INNER JOIN sys.schemas s ON o.schema_id=s.schema_id
WHERE c.name LIKE '%text%'
If I get it right, they are replication objects. Is it so? Can i just throw them away from my query just like o.name NOT LIKE '%syncobj%' or there's another way?
Thank you.
I've found a solution. Doesn't know, if it's the best one or not.
SELECT c.name, s.name + '.' + o.name
FROM sys.columns c
INNER JOIN sys.objects o ON c.object_id=o.object_id
INNER JOIN sys.schemas s ON o.schema_id=s.schema_id
WHERE c.name LIKE '%text%' AND o.type = 'U'
The result is fine now. As I said syncobj's are replication objects and they don't have a meaning for us. They're used for replication purposes only.
http://www.developmentnow.com/g/114_2007_12_0_0_443938/syncobj-views.htm
EDIT:
Forgot to add, syncobj's are stored in DB as Views, so if you need list of views, you'll probably need to ignore them as I did in my question.
While checking difference between syncobj's and my views, the only difference is is_ms_shipped column. For syncobj it's 1, for others 0. It means that syncobj views are created by system.
P.S. I'll wait for some time and if nobody gives another answer, I'll accept mine.
When you create a replication that does not include all the fields or other meta data changes from the original table. If you do a generate script from a publication it will show you how it is created (see below). The view provide a object to generate the bcp extracts during the initial snapshots.
Here is an example
-- Adding the article synchronization object exec sp_articleview #publication = N'publication_data', #article = N'tablename',
#view_name = N'syncobj_0x4239373642443436', #filter_clause = N'',
#force_invalidate_snapshot = 1, #force_reinit_subscription = 1 GO
P.S. I recently had a problem when the I dropped replication, it failed to drop these and then you have to manually drop the system views to reuse a replication script. Giving a error message
Msg 2714, Level 16, State 3: There is already an object named
'syncobj_0x3437324238353830' in the database.
Which caused the bcp to fail during the snapshot.