In most RDBMS, the meta-model is "self contained", which means that I can find out the model of the meta-model by browsing the meta-model itself. This doesn't seem to be the case with SQL Server. What I want to do is this:
SELECT * FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'INFORMATION_SCHEMA'
That way, I can discover the INFORMATION_SCHEMA schema itself.
Is there any grant/permission/login setting that I have to configure in order to make the INFORMATION_SCHEMA views be "self contained"?
Don't think this is possible.
The definition of the INFORMATION_SCHEMA.TABLES view is
CREATE VIEW [INFORMATION_SCHEMA].[TABLES]
AS
SELECT
DB_NAME() AS TABLE_CATALOG,
s.name AS TABLE_SCHEMA,
o.name AS TABLE_NAME,
CASE o.type
WHEN 'U' THEN 'BASE TABLE'
WHEN 'V' THEN 'VIEW'
END AS TABLE_TYPE
FROM
sys.objects o LEFT JOIN sys.schemas s
ON s.schema_id = o.schema_id
WHERE
o.type IN ('U', 'V')
so it pulls its information from sys.objects however this in turn contains nothing about the INFORMATION_SCHEMA objects.
The metadata for these is accessed via sys.system_objects instead.
You can use sys.all_views
select SCHEMA_NAME(schema_id), name
from sys.all_views
order by 1,2
USE information_schema;
SHOW TABLES;
USE mysql;
SHOW TABLES ;
Related
I am looking into extract table name and all related views and schemas in my SQL Server.
Until now I have used this SQL code to do that:
SELECT
t.VIEW_SCHEMA,
t.VIEW_NAME,
t.TABLE_SCHEMA,
t.TABLE_NAME
FROM
INFORMATION_SCHEMA.VIEW_TABLE_USAGE t
WHERE
t.TABLE_NAME = 'MYTABLE'
AND t.VIEW_NAME like '%_something%'
The INFORMATION_SCHEMA.VIEW_TABLE_USAGE seems only to be updated when views actually are used, so when I run my code on a table with have new view definitions or a views has never been used it returns nothing.
I have tried to combine INFORMATION_SCHEMA.VIEWS and INFORMATION_SCHEMA.TABLES but I can't find a key to join on.
I have also tried to look into sys.tables and sys.views but I still haven't found a way to join it.
I really need to get the columns specified in SQL.
Any ideas on how to do that? Maybe my approach is all wrong.
SQL Server offers a few possibilities via sys.sql_expression_dependencies and sys.dm_sql_referencing_entities()
sys.sql_expression_dependencies is easier to use, an example for your purpose would be
select
Schema_Name(v.schema_id) View_Schema, v.name View_Name,
Schema_Name(t.schema_id) Table_Schema, t.name Table_Name
from sys.tables t
join sys.sql_expression_dependencies d on d.referenced_id=t.object_id
join sys.objects v on v.object_id=d.referencing_id and v.type='V'
where
t.name='tablename'
and v.name like '%something%'
SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES
Context: The above query lists all the tables and views. How do I list only tables? How do I list only views?
You can use sys.tables
USE <database_name>;
GO
SELECT name FROM sys.tables
You can also use sys.objects with filter for type ='U' -- corresponding to user defined tables
SELECT name FROM sys.objects WHERE type = 'U'
To List only views:
SELECT TABLE_NAME FROM INFORMATION_SCHEMA.VIEWS;
USE <database_name>;
GO
SELECT name from SYS.views
SELECT name FROM sys.objects WHERE type = 'V'
How do I output the schema of my database? I want it to output the design of the database.
Something like this might work:
SELECT TABLE_TYPE, TABLE_NAME, COLUMN_NAME, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH
FROM INFORMATION_SCHEMA.COLUMNS
ORDER BY TABLE_TYPE, TABLE_NAME, COLUMN_NAME
But I can't get it to run correctly. A excel file with table names, their columns, types, primary keys, etc. is what I want.
SELECT * FROM INFORMATION_SCHEMA.TABLES;
... sounds like what you are looking for, sans any formatting you want to do.
select
t.type_desc,
t.name as [table],
c.name as [column],
y.name,
c.max_length
from sys.tables t inner join
sys.columns c on c.object_id = t.object_id inner join
sys.types y on c.system_type_id = y.system_type_id
where y.name <> 'sysname'
order by
t.type_desc,
t.name,
c.name
Below posts would be useful for generating data dictionary
Database Documentation - http://deepakrangarajan.blogspot.com/2011/03/database-documentation.html
Generating a Database Data Dictionary - http://sqlserverdiaries.com/blog/index.php/2011/02/generating-a-database-data-dictionary/
Is there a way to query the database and retrieve a list of all stored procedures and their parameters?
I am using SQL Server 2000.
To get information on the stored procedures:
SELECT * FROM INFORMATION_SCHEMA.ROUTINES
To find the sprocs starting with a certain prefix (e.g. "usp"):
SELECT * FROM INFORMATION_SCHEMA.ROUTINES WHERE ROUTINE_NAME LIKE 'usp%'
To find all the parameters for a stored procedure:
SELECT * FROM INFORMATION_SCHEMA.PARAMETERS WHERE SPECIFIC_NAME='YourSprocName'
To find all the parameters for all stored procedures starting with a certain prefix:
SELECT * FROM INFORMATION_SCHEMA.PARAMETERS WHERE SPECIFIC_NAME LIKE 'usp%'
try this one :
select o.name,p.name from sys.all_parameters p inner join sys.all_objects o on p.object_id = o.object_id
where o.type = 'P'
To show a list of all procedures and their parameters, it would be in this way:
SELECT o.name AS [Procedure name], p.name as [Parameter name]
FROM sys.parameters p INNER JOIN sysobjects o ON p.object_id = o.id
WHERE o.name LIKE 'prefix%' AND o.xtype = 'P'
It works in SQL Server 2016 but I guess it works in older versions too.
the following query returns the procedures, functions and filters by a prefix.
I am not sure though, if it would work on sql server 2000.
I leave it here the reference anyway, because it is a good useful query.
SELECT SCHEMA_NAME(SCHEMA_ID) AS [Schema],
SO.name AS [ObjectName],
SO.Type_Desc AS [ObjectType (UDF/SP)],
COALESCE(P.parameter_id,0) AS [ParameterID],
COALESCE(P.name, 'NO PARAMETER') AS [ParameterName],
COALESCE(TYPE_NAME(P.user_type_id),'') AS [ParameterDataType],
COALESCE(P.max_length,0) AS [ParameterMaxBytes],
COALESCE(P.is_output,0) AS [IsOutPutParameter]
FROM sys.objects AS SO
LEFT OUTER JOIN sys.parameters AS P
ON SO.OBJECT_ID = P.OBJECT_ID
WHERE SO.OBJECT_ID IN ( SELECT OBJECT_ID
FROM sys.objects
WHERE TYPE IN ('P','FN'))
AND SO.NAME LIKE 'U%' --starting with a certain prefix
ORDER BY [Schema], SO.name, P.parameter_id
GO
I would like to list all objects of a particular database in SQL Server. I created a query as shown below:
select name, type_desc from sys.objects
WHERE type in ( 'C', 'D', 'F', 'L', 'P', 'PK', 'RF', 'TR', 'UQ', 'V', 'X' )
union
select name, type_desc from sys.indexes
order by name
However, this query list all objects of ALL databases rather than a particular database.
My question is: Is there a way to query all objects of just a particular database?
Which database are you running this in? When I run it in a particular database, I don't get anything outside that database.
List all procs, views, tables, functions in Sql Server:
SELECT DISTINCT
o.name AS Object_Name,
o.type_desc
FROM sys.sql_modules m
INNER JOIN
sys.objects o
ON m.object_id = o.object_id
--WHERE '.' + m.definition + '.' LIKE '%[^a-z]employeeid[^a-z]%'
order by type_desc, object_name
The comment is if you want to search for a particular (whole) word.
This is what I use.
SELECT o.type_desc AS Object_Type
, s.name AS Schema_Name
, o.name AS Object_Name
FROM sys.objects o
JOIN sys.schemas s
ON s.schema_id = o.schema_id
WHERE o.type NOT IN ('S' --SYSTEM_TABLE
,'PK' --PRIMARY_KEY_CONSTRAINT
,'D' --DEFAULT_CONSTRAINT
,'C' --CHECK_CONSTRAINT
,'F' --FOREIGN_KEY_CONSTRAINT
,'IT' --INTERNAL_TABLE
,'SQ' --SERVICE_QUEUE
,'TR' --SQL_TRIGGER
,'UQ' --UNIQUE_CONSTRAINT
)
ORDER BY Object_Type
, SCHEMA_NAME
, Object_Name