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''')
Related
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/
Provided there are tables Application, Person, Status, etc...
Status and Person have a link to Application.
Is there a simple sql query that may be written that will give a list of all the tables linked to the Application table (in contrast of using the database diagram).
Application expected results:
Status
Person
Please advise on a feasible solution (if any).
Using SP_depends. There are other ways as well like DMV's.
sp_depends 'TableName'
Assuming you're using MSSQL Server then you can run
EXEC sp_fkeys 'YourTableName'
Otherwise you can use
SELECT *
FROM information_schema.referential_constraints
WHERE table_name = <tablename>
to establish the foreign key relationships
Another way to check each table name that references to other tables would be this:
SELECT DISTINCT OBJECT_NAME(f.parent_object_id) AS TableName,
OBJECT_NAME (f.referenced_object_id) AS ReferenceTableName
FROM sys.foreign_keys AS f
INNER JOIN sys.foreign_key_columns AS fc
ON f.OBJECT_ID = fc.constraint_object_id
INNER JOIN sys.objects AS o
ON o.OBJECT_ID = fc.referenced_object_id
You can, of course, add more conditions to check for a single table, for example:
WHERE OBJECT_NAME(f.referenced_object_id) = 'Application'
This should give you a result like:
TableName ReferenceTableName
---------- ------------------
Status Application
Person Application
i have a XML schema bound to a table. however, sometimes testers piggyback and bind to this schema too. when there is this "ninja" XML table reference, any alteration to this schema is painful.
i'd like to run a query before schema altering and raise exception if the XML schema is bound to more than one table. i've looked at sys.sql_dependencies and few of the other sys.xml_XXXX tables, but it's not clear how to do this in tsql. is something like this possible?
Something like this might be helpful
select object_name(object_id) as TableName,
col_name(object_id, column_id) as ColumnName
from sys.column_xml_schema_collection_usages as U
inner join sys.xml_schema_collections as S
on U.xml_collection_id = S.xml_collection_id
where S.name = 'YourXMLSchemaCollectionName'
This one is to find where the schema is used in a XML parameter.
select object_name(object_id)
from sys.parameter_xml_schema_collection_usages as P
inner join sys.xml_schema_collections as S
on P.xml_collection_id = S.xml_collection_id
where S.name = 'YourXMLSchemaCollectionName'
I have a field name used in a SQL Server DB - I do not know if it is a table field or a stored procedure parameter. Is there a query I can run against the DB to find candidate fields in tables and stored procedures?
Depends on which version of sql server you are using:
For 2000, use syscolumns:
select object_name(select object_name(id)
from syscolumns
where name = 'ID'
For 2005+ use sys.columns:
select object_name(object_id)
from sys.columns
where name = 'ID'
The use of the object_name() function negates the need for an inner join on the objects table.
If it's SQL Server 2005 or 2008 you can use the INFORMATION_SCHEMA views.
http://msdn.microsoft.com/en-us/library/ms186778.aspx
SQL statement like this will find what you're looking for:
select * from information_schema.parameters where parameter_name like '%field%'
select * from information_schema.columns where column_name like '%field%'
Yes you can:
select o.type, o.name from sys.columns c
join sys.objects o on c.object_id = o.object_id
where c.name = #col_to_find
Or you can install the free Red Gate Search add-in.
Given a column name how can I find
which tables in database contain that
column ?
or alternatively
How can I find that particular column
exists for all tables in Database ?
Note: Kindly explain answers with Examples as that I get most knowledge from the answer.
Edit: I am using MySQL Database.
SELECT * FROM information_schema.columns WHERE COLUMN_NAME = 'mycolumn'
Depends on the database you are using. Many database systems expose a set of tables of views that contain details of the schema. For example, you can get schema information from the SYSTABLE and SYSCOLUMN views in Sybase ASA.
in SQL Server:
select distinct t.name
from sys.Columns c
inner join sys.tables t on c.object_id = t.object_id
where c.name = 'YOUR_COLUMNNAME'