Retrieve names columns of all tables in a SQL Server database - sql-server

Does there exist a system stored procedure that lists the names of all tables in a SQL Server 2000 database and per table the names of all columns in that table? I want to export this data to a file for documentation.

I don't know if a system stored procedure exists, but I use this:
SELECT SysObjects.[Name] as TableName,
SysColumns.[Name] as ColumnName,
SysTypes.[Name] As DataType,
SysColumns.[Length] As Length
FROM
SysObjects INNER JOIN SysColumns
ON SysObjects.[Id] = SysColumns.[Id]
INNER JOIN SysTypes
ON SysTypes.[xtype] = SysColumns.[xtype]
WHERE SysObjects.[type] = 'U'
ORDER BY SysObjects.[Name]

In SQL-Server 2005+ you can do it using system views sys.columns and sys.tables
SELECT t.name TableName, c.name ColumnName
FROM sys.tables t
JOIN sys.columns c ON t.object_id=c.object_id

And you can also query the INFORMATION_SCHEMA.COLUMNS view.
It's also safer to use this view. Microsoft says this about the view "Querying the system tables directly may not provide accurate information if system tables are changed in future releases. These views provide an internal, system table-independent view of the SQL Server meta data. Information schema views allow applications to work properly even though significant changes have been made to the system tables."

Related

How to find all tables depends to stored procedure

I want to find all tables used in stored procedures but it only gives me the tables that are in the database that I'm just using. Is there a solution to find all tables used in stored procedures, which are in other databases (in the same server)?
I try this:
SELECT DISTINCT p.name AS proc_name, t.name AS table_name
FROM sys.sql_dependencies d
INNER JOIN sys.procedures p ON p.object_id = d.object_id
INNER JOIN sys.tables t ON t.object_id = d.referenced_major_id
WHERE p.name like '%sp_example%'
ORDER BY proc_name, table_name
The procedures I need to analyze contain tables from different databases, but the code above gives me only results from one database.
First of all, sys.sql_dependencies is deprecated. Instead, you should use sys.sql_expression_dependencies. In it, you will find 4-part names of referenced objects (this includes objects referenced via linked server, for example).
Second, any object_id in SQL Server only makes sense within its database. If you are looking for anything outside of your current DB, don't join tables by these identifiers - use object names instead.

How to get properties of a field of table in sybase

Here is details
In sybase, I have a table "abc" having 5 fields(name, roll, address, desc,path). If i would use 'sp_help tablename' then i can see all properties of all fields how i can get properties of a particular field i.e. roll or any one field and their properties using sql or pl/sql.
As we know that we can not use any parameters in sp_help so is there any way to to get the properties of a field, (pl/sql or sql)?
Thanks in advance
sp_help tablename
This depends on what specific properties you are looking for. Assuming you are looking for Table, Column, Datatype, Datatype Length - you have to join sysobjects, syscolumns, and systypes
use YOURDB
go
select o.name, c.name, t.usertype, c.length
from
sysobject o,
syscolumns c,
systypes t
where o.id = c.id
and c.usertype = t.usertype
and o.name like "YOUR_TABLE"
and c.name like "YOUR_COLUMN"
go
If you want more than that, you'll have to bring in other columns and/or tables into the query like sysdepends, sysdefaults or sysconstraints
More information about the system tables can be found here:
Sybase ASE Reference Manual: Tables > System Tables
And the system table diagram, which shows the column mappings can be found here:
http://infocenter.sybase.com/help/topic/com.sybase.infocenter.dc70204.1550/pdf/a155pst.pdf
Also, FYI - Sybase uses T-SQL (like SQL Server), not pl/sql.

Query to get list of views that use a certain field in SQL Server

Does anybody know of a query from system tables or views to get a list of views that use a certain field in a SQL Server database?
SELECT *
FROM
sys.sql_modules m
JOIN
sys.views v ON m.object_id = v.object_id
WHERE
m.definition LIKE '%MyTable%' --or '%MyField%'
INFORMATION_SCHEMA views and legacy syscomments are unreliable for large view definition (or any definition) because they have nvarchar(4000) fields. sys.sql_modules uses nvarchar(max).
They should not be used
sys.sql_expression_dependencies may be an alternative but is more complex to use.

SQL request to list all databases in an instance of Sql-Server?

Is there a way to list all of the existing databases in an instance of Sql Server via a SQL request ?
More generally can I use SQL to fully read the schema of the databases (tables, columns, ..) ?
Thank you
Jerome Wagner
Yes. See the Information Schema Views and sys.databases.
Here's a script for table definitions that may be useful. http://www.builderau.com.au/program/sqlserver/soa/Script-Table-definitions-using-TSQL/0,339028455,339293405,00.htm
You can get a lot of infos by the following queries:
SELECT * FROM sys.databases
use Northwind
select * from sys.objects where type_desc = 'USER_TABLE'
SELECT t1.name [table], t2.*
FROM sys.objects t1
inner join sys.columns t2 on t1.object_id = t2.object_id
where type_desc = 'USER_TABLE'
sp_help 'Customers' -- Customers = tablename
Yes, Sp_msforeachdb, there is also a sp_msforeachtable. You can use both to iteratively retrieve all tables in all DB's and get what you want.
https://web.archive.org/web/1/http://blogs.techrepublic%2ecom%2ecom/datacenter/?p=395.
Try using the stored procedure sp_databases to get the list of all db

SQL Query to search schema of all tables

I am working on a SQL Server 2008 Db that has many tables in it (around 200). Many of these tables contain a field by the name "CreatedDate". I am trying to identify all the table schema with this particular field.
Is there a SQL query to do this?
I would query the information_schema - this has views that are much more readable than the underlying tables.
SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME LIKE '%create%'
You can also try doing this using one of many third party tools that are available for this.
Queries are great for simple searches but if you need to do more manipulation with data, search for references and such this is where you can do a much better job with these.
Also, these come in very handy when some objects are encrypted and you need to search for
I’m using ApexSQL Search which is free but there are also many more (also free) on the market such as Red Gate or SSMS Tool Pack.
select object_name(c.object_id) as table_name
, schema_name(t.schema_id) as schema_name
from sys.columns c
join sys.tables t on c.object_id = t.object_id
where c.name=N'CreatedDate';
It gets a little more complicated if you want alsoother table properties, but you'll refer to the object catalog views like sys.tables, sys.columns etc.
My favorite...
SELECT objParent.name AS parent, obj.name, col.*
FROM sysobjects obj
LEFT JOIN syscolumns col
ON obj.id = col.id
LEFT JOIN sysobjects objParent
ON objParent.id = obj.parent_obj
WHERE col.name LIKE '%Comment%'
OR obj.name LIKE '%Comment%'
Above I'm searching for "Comment".
Drop the percent signs if you want a direct match.
This searches tables, fields and things like primary key names, constraints, views, etc.
And when you want to search in StoredProcs after monkeying with the tables (and need to make the procs match), use the following...
SELECT name
FROM sys.procedures
WHERE OBJECT_DEFINITION(OBJECT_ID) LIKE '%Comment%'
Hope that helps, I find these two queries to be extremely useful.
For me I only have read access to run querys so I need to use this function often here is what I use:
SELECT *
FROM INFORMATION_SCHEMA.TABLES
where TABLES.TABLE_NAME like '%your table name here%'
You can replace .TABLES with .COLUMNS then it would look like this:
SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE columns.COLUMN_NAME like '%your column name here%'
Use this query :
SELECT
t.name AS table_name,
SCHEMA_NAME(schema_id) AS schema_name,
c.name AS column_name , *
FROM sys.tables AS t
INNER JOIN sys.columns c ON t.OBJECT_ID = c.OBJECT_ID
Where
( c.name LIKE '%' + '<ColumnName>' + '%' )
AND
( t.type = 'U' ) -- Use This To Prevent Selecting System Tables
Same thing but in ANSI way
SELECT
*
FROM
INFORMATION_SCHEMA.TABLES
WHERE
TABLE_NAME IN (
SELECT
TABLE_NAME
FROM
INFORMATION_SCHEMA.COLUMNS
WHERE
COLUMN_NAME = 'CreateDate'
)
You do not need to type SQL Query for this in SQL Server 2008.
In SSMS Object Explorer choose Databases or Tables of the required database (if you need to search in one database), open menu View--> Object Explorer Details (alternatively press F7), type %CreatedDate% in Search textbox, ENTER, enjoy

Resources