Search Column name in Linked Server - sql

Is there any way to find particular column in Linked Server's database within all tables.
I guess solution lies in
EXEC sp_columns_ex

SELECT t.name as TableName, c.name as ColumnName
FROM servernamehere.databasenamehere.sys.columns c
INNER JOIN servernamehere.databasenamehere.sys.tables t ON c.object_id = t.object_id
WHERE c.name like '%yoursearchhere%'

How about this:
EXECUTE [MyLinkedServer].[MyLinkedDB].dbo.sp_executesql
N'SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE ...'
And fill in the where clause depending on what you want to search for?
An alternative would be creating a view that selects from INFORMATION_SCHEMA.COLUMNS and then you query that instead.
Reference for Information_Schema.Columns

It would depend on which database your linked server is pointing to. For example, if it is Oracle, you would use Oracle syntax, if it is SQL Server, Sql Server Syntax.
The fact that you are querying the schema through a Linked server shouldnt matter.

Related

How do I get a list of columns in a table or view?

On occasion, I'm interested in getting a list of columns in one of the tables or views in my SQL Server 2008 R2 database. It's useful, for example, if you're building database documentation without using an expensive off-the-shelf product.
What's an easy way to get this information?
In SQL Server 2008 R2 (among other versions), there are system views provided automatically with every database. As long as you are connected to the database where your table resides, you can run a query like this:
DECLARE #TableViewName NVARCHAR(128)
SET #TableViewName=N'MyTableName'
SELECT b.name AS ColumnName, c.name AS DataType,
b.max_length AS Length, c.Precision, c.Scale, d.value AS Description
FROM sys.all_objects a
INNER JOIN sys.all_columns b
ON a.object_id=b.object_id
INNER JOIN sys.types c
ON b.user_type_id=c.user_type_id
LEFT JOIN sys.extended_properties d
ON a.object_id=d.major_id AND b.column_id=d.minor_id AND d.name='MS_Description'
WHERE a.Name=#TableViewName
AND a.type IN ('U','V')
Of course, this is just a starting point. There are many other system views and columns available in every database. You can find them through SQL Server Management Studio under Views > "System Views
Another way is querying the INFORMATION_SCHEMA.columns view as detailed here:
Information_Schema - COLUMNS
This will give you information for all the columns in the current database (and what table/view they belong to) including their datatypes, precision, collation and whether they allow nulls etc
Usefully as well, these views are maintained in multiple DBMS programs too, so you could potentially use the same or similar query to get the same information regarding a MySQL database as you can a SQL Server DB, which could be useful if you are developing on multiple platorms.
sp_columns returns detailed information about each of the columns in the table. SO Answer
sp_columns #tablename
sp_help returns detailed information about the entire table including the columns and constraints. SO Answer
sp_help #tablename
To get a list of Columns of a view with some other information about the column you can use the following:
SELECT * FROM sys.columns c, sys.views v
WHERE c.object_id = v.object_id
AND v.name = 'view_Name'
GO
And if you only want the list of Column Name use this.
SELECT c.name
FROM sys.columns c, sys.views v
WHERE c.object_id = v.object_id
AND v.name = 'view_UserAssessphers'
GO
exec sp_helptext <your view name>
Also works for the view only, blachniet's answer is best if you need details on the columns in the table.
In a new query window, type the name of the view/table, highlight it, and press Alt-F1. This will run sp_help, like blachniet suggested.
simple list of column names without any further information.
SELECT COLUMN_NAME FROM TABLENAME.INFORMATION_SCHEMA.COLUMNS;
Replace TABLENAME with your tables name.

How to search a column in multiple tables in SQL Server 2005

There are several tables with many columns and it takes forever to manually find the column I need. How do I search for my column from whatever table it exists in?
You can do:
SELECT COLUMN_NAME, TABLE_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME LIKE '%columnName%'
You can use the sys.columns view and then join to the sys.tables view to get that information.

How to find which views are using a certain table in SQL Server (2008)?

I have to add a few columns to a table and I also need to add these columns to all the views that use this table.
Is it possible to get a list of all the views in a database that use a certain table?
This should do it:
SELECT *
FROM INFORMATION_SCHEMA.VIEWS
WHERE VIEW_DEFINITION like '%YourTableName%'
To find table dependencies you can use the sys.sql_expression_dependencies catalog view:
SELECT
referencing_object_name = o.name,
referencing_object_type_desc = o.type_desc,
referenced_object_name = referenced_entity_name,
referenced_object_type_desc =so1.type_desc
FROM sys.sql_expression_dependencies sed
INNER JOIN sys.views o ON sed.referencing_id = o.object_id
LEFT OUTER JOIN sys.views so1 ON sed.referenced_id =so1.object_id
WHERE referenced_entity_name = 'Person'
You can also try out ApexSQL Search a free SSMS and VS add-in that also has the View Dependencies feature. The View Dependencies feature has the ability to visualize all SQL database objects’ relationships, including those between encrypted and system objects, SQL server 2012 specific objects, and objects stored in databases encrypted with Transparent Data Encryption (TDE)
Disclaimer: I work for ApexSQL as a Support Engineer
If you need to find database objects (e.g. tables, columns, triggers) by name - have a look at the FREE Red-Gate tool called SQL Search which does this - it searches your entire database for any kind of string(s).
It's a great must-have tool for any DBA or database developer - did I already mention it's absolutely FREE to use for any kind of use??
I find this works better:
SELECT type, *
FROM sys.objects
WHERE OBJECT_DEFINITION(object_id) LIKE '%' + #ObjectName + '%'
AND type IN ('V')
ORDER BY name
Filtering VIEW_DEFINTION inside INFORMATION_SCHEMA.VIEWS is giving me quite a few false positives.
SELECT VIEW_NAME
FROM INFORMATION_SCHEMA.VIEW_TABLE_USAGE
WHERE TABLE_NAME = 'Your Table'
select your table -> view dependencies -> Objects that depend on
Simplest way to find used view or stored procedure for the tableName using below query -
exec dbo.dbsearch 'Your_Table_Name'

sql server compatible queries for given oracle queries

I want Microsoft SQL server queries corresponding to the following Oracle queries
//get schema of a table
desc tablename;
//get the names of all tables
select * from tab;
You have access to that info through metadata tables. Check this link out.
INFORMATION_SCHEMA.Tables -> gives you access to table names
INFORMATION_SCHEMA.Columns -> gives you access to column names
Here is another link with a complete list of catalog tables.
INFORMATION_SCHEMA.CHECK_CONSTRAINTS
INFORMATION_SCHEMA.COLUMN_DOMAIN_USAGE
INFORMATION_SCHEMA.COLUMN_PRIVILEGES
INFORMATION_SCHEMA.COLUMNS
INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
INFORMATION_SCHEMA.CONSTRAINT_TABLE_USAGE
INFORMATION_SCHEMA.DOMAIN_CONSTRAINTS
INFORMATION_SCHEMA.DOMAINS
INFORMATION_SCHEMA.KEY_COLUMN_USAGE
INFORMATION_SCHEMA.PARAMETERS
INFORMATION_SCHEMA.REFERENCIAL_CONSTRAINTS
INFORMATION_SCHEMA.ROUTINE_COLUMNS
INFORMATION_SCHEMA.ROUTINES
INFORMATION_SCHEMA.SCHEMA_DATA
INFORMATION_SCHEMA.TABLE_CONSTRAINTS
INFORMATION_SCHEMA.TABLE_PRIVILEGES
INFORMATION_SCHEMA.TABLES
INFORMATION_SCHEMA.VIEW_COLUMN_USAGE
INFORMATION_SCHEMA.VIEW_TABLE_USAGE
INFORMATION_SCHEMA.VIEWS
Keep in mind though, that you will probably need special permission to access those tables/views.
The other thing you might try as an alternative is using ODBC, Java, .NET or any other programming language or library to access metadata information. They have complete access to that through their APIs.
Table desctiption:
sp_help table_name
All tables in the current database:
select * from sysobjects where xtype='U'
And you can use sysobjects, syscolumns, sysindexes etc. tables to get the information about database structure.

Query all user defined types from Sql Server?

How can I query all user defined data types from sql server like the way I do this for database object which are stored in sysobjects.
In SQL Server 2005 (and above) you can do this
SELECT * FROM sys.Types WHERE is_user_defined = 1
If you are on SQL Server 2000, you can use SysTypes. I think the query is as follows:
SELECT * FROM SysTypes WHERE xusertype > 256
If you are still on SQL Server 2000 (as your use of 'sysobjects' suggests), look in systypes. If you are on SQL Server 2005 or later, as rwwilden says you should use sys.types (and move to using sys.objects rather than sysobjects!)
You can use the sys.types view for this. When user_type_id and system_type_id are different, you have a user type.
As AakashM says, the sys.types view is only available from SQL Server 2005 and higher.
EDIT: A better way to determine whether a type is a user-defined type is to check the is_user_defined flag (see Tim C's answer).
There is no standard query to get all the User defined databases alone. We can use
select * from sys.databases
But it shows all the Databases, I'm using Server management studio. So by default i have the following databases named : 'Master','tempdb','msdb','model'
So i always use this following query to get the user defined databases.
select * from sys.databases where name not in('master','model','msdb','tempdb')
It works for fine. If you have extra databases by default, it will be the Following (resource,distribution,reportservice, reportservicetemp). Just use the names in that query.
You can use the INFORMATION_SCHEMA views. INFORMATION_SCHEMA.ROUTINES has UDFs and Stored Procs. INFORMATION_SCHEMA.TABLES has tables and views.
Actually system_type_id being different from user_type_id is NOT a good indication. Note that this:
SELECT * FROM sys.Types
WHERE (system_type_id <> user_type_id)
AND is_user_defined = 0.
Returns these:
hierarchyid
geometry
geography
sysname
So use SELECT * FROM sys.types WHERE is_user_defined = 1 as was pointed out by Tim C and Ronald Wildenberg
You can also find these in INFORMATION_SCHEMA.DOMAINS.
personally I prefer to use that over the sys schema, as it's an ANSI standard that works over multiple types of databases.