How do I get only Tables and only views? (SQL Server) - sql-server

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'

Related

list all tables from a database in syBase

In sql server 2012 i'm using
USE myDatabase;
GO
SELECT *
FROM sys.objects
WHERE type = 'U';
Is it possible to do the same in syBase ?
In order to get a list of all tables in the current database, you can filter the sysobjects table by type = ‘U’ e.g.:
select convert(varchar(30),o.name) AS table_name
from sysobjects o
where type = 'U'
order by table_name
Further Reference
Here is an example of getting all table names in MSSQL or SQL Server database:
USE test; //SELECT DATABASE
SELECT table_name FROM information_schema.tables WHERE table_type = 'base table'
or you can use sys.tables to get all table names from selected database as shown in following SQL query
USE test; //SELECT DATABASE
SELECT * FROM sys.tables
That's all on how to find all table names from database in SQL Server.
For SQL Anywhere 16, this query works fine.
select * from sys.SYSTABLE
It gives a list of the table_names along with the information like table id, table page count, etc.
In order to list all the tables and rows count, the following query can be used.
select convert(varchar(30),o.name) AS table_name,
row_count(db_id(), o.id) AS row_count
from sysobjects o
where type = 'U'
order by table_name
In order get a list of tables which has the table name like, use the following query.
Here table_name has to be replaced with your desired name.
select convert(varchar(30),o.name) AS table_name,
row_count(db_id(), o.id) AS row_count
from sysobjects o
where type = 'U' and o.name like '%table_name%'
order by table_name

Locating a table in the database

When I run the following query I get the results as expected.
select * from [Beep].[Bopp]
Then, I ran the query below to "find" the table.
select * from sys.objects
where type='U'
and name like '%Bopp%'
It finds the table and lists the row describing it. However, I can't see any reference to Beep there. The name only contains Bopp, so I'm guessing that there a key that I need to look up but I don't know which column that is nor in what table to look it up.
edit
Based on the comments, I improved the query but I'm still not sure in what table to look up the actual name of the schema. The following gives me waaay to many hits (and setting the type didn't actually give me anything Beep-like.
select * from sys.tables t
left join sys.objects s
on t.schema_id = s.schema_id
where t.name like '%Bopp%'
I checked the objects for the specific name like so.
select * from sys.objects
where name like '%Beep%'
To my surprise, I didn't see any hits at all. Where is the little Beep-y thing hiding?!
You can use the schema_name() function against the schema_id column of sys.objects, like so:
select schema_name([schema_id]) from sys.objects where [name] like '%Bopp%';
Alternatively, you can query INFORMATION_SCHEMA.TABLES, which has a TABLE_SCHEMA column that gives the name of the schema rather than its id.
select * from INFORMATION_SCHEMA.TABLES where TABLE_NAME = 'Bopp';
Beep is the schema name for which you should attach another sys table like so:
select sys.schemas.name as schema_name, sys.objects.name as object_name
from sys.objects
INNER JOIN sys.schemas ON sys.objects.schema_id = sys.schemas.schema_id
where type='U'
and sys.objects.name like '%Bopp%'

How to find all trigger associated with a table with SQL Server?

I created a trigger for a table in SQL Server and it works for me.
My problem is: How do find it and modify it?
I use this query to find my triggers:
select * from sys.triggers
This find all triggers, but how to open it and change the triggers?
You can do this simply with SSMS. Just go to your table name and expand the Triggers node to view a list of triggers associated with that table. Right click to modify your trigger.
select so.name, text
from sysobjects so, syscomments sc
where type = 'TR'
and so.id = sc.id
and text like '%YourTableName%'
This way you can list out all the triggers associated with the given table.
This might be useful
SELECT
t.name AS TableName,
tr.name AS TriggerName
FROM sys.triggers tr
INNER JOIN sys.tables t ON t.object_id = tr.parent_id
WHERE
t.name in ('TABLE_NAME(S)_GOES_HERE');
This way you just have to plugin the name of tables and the query will fetch all the triggers you need
select m.definition from sys.all_sql_modules m inner join sys.triggers t
on m.object_id = t.object_id
Here just copy the definition and alter the trigger.
Else you can just goto SSMS and Expand the your DB and under Programmability expand Database Triggeres then right click on the specific trigger and click modify there also you can change.
use sp_helptrigger to find the triggerlist for the associated tables
find triggers on table:
select so.name, text
from sysobjects so, syscomments sc
where type = 'TR'
and so.id = sc.id
and text like '%TableName%'
and you can find store procedure which has reference of table:
SELECT Name
FROM sys.procedures
WHERE OBJECT_DEFINITION(OBJECT_ID) LIKE '%yourtablename%'
Go through
Need to list all triggers in SQL Server database with table name and table's schema
This URL have set of queries by which you can get the list of triggers associated with particular table.
I believe you are working in sqlserver following are the steps to get modify triggers
To modify a trigger
Expand a server group, and then expand a server.
Expand Databases, expand the database in which the table containing
the trigger belongs, and then click Tables.
In the details pane, right-click the table on which the trigger
exists, point to All Tasks, and then click Manage Triggers.
In Name, select the name of the trigger.
Change the text of the trigger in the Text field as necessary. Press
CTRL+TAB to indent the text of a SQL Server Enterprise Manager
trigger.
To check the syntax of the trigger, click Check Syntax.
With this query you can find all Trigger in all tables and all views.
;WITH
TableTrigger
AS
(
Select
Object_Kind = 'Table',
Sys.Tables.Name As TableOrView_Name ,
Sys.Tables.Object_Id As Table_Object_Id ,
Sys.Triggers.Name As Trigger_Name,
Sys.Triggers.Object_Id As Trigger_Object_Id
From Sys.Tables
INNER Join Sys.Triggers On ( Sys.Triggers.Parent_id = Sys.Tables.Object_Id )
Where ( Sys.Tables.Is_MS_Shipped = 0 )
),
ViewTrigger
AS
(
Select
Object_Kind = 'View',
Sys.Views.Name As TableOrView_Name ,
Sys.Views.Object_Id As TableOrView_Object_Id ,
Sys.Triggers.Name As Trigger_Name,
Sys.Triggers.Object_Id As Trigger_Object_Id
From Sys.Views
INNER Join Sys.Triggers On ( Sys.Triggers.Parent_id = Sys.Views.Object_Id )
Where ( Sys.Views.Is_MS_Shipped = 0 )
),
AllObject
AS
(
SELECT * FROM TableTrigger
Union ALL
SELECT * FROM ViewTrigger
)
Select
*
From AllObject
Order By Object_Kind, Table_Object_Id
select B.name
from sys.objects A
join sys.triggers B
on A.object_id = B.parent_id
where A.name ='Table_name' /*Table Name*/
select o1.name as trigger_name,o2.name as table_name from sys.objects o1
join sys.objects o2 on
o1.parent_object_id=o2.object_id
where o1.type='TR'
and o2.name='my_table'
You Can View All trigger related to your database by below query
select * from sys.triggers
And for open trigger you can use below syntax
sp_helptext 'dbo.trg_InsertIntoUserTable'
Much simple query below
select (select [name] from sys.tables where [object_id] = tr.parent_id ) as TableName ,* from sys.triggers tr
select * from information_schema.TRIGGERS;
select t.name as TriggerName,m.definition,is_disabled
from sys.all_sql_modules m
inner join
sys.triggers t
on m.object_id = t.object_id
inner join sys.objects o
on o.object_id = t.parent_id
Where o.name = 'YourTableName'
This will give you all triggers on a Specified Table
Try to Use:
select * from sys.objects where type='tr' and name like '%_Insert%'
you can open your trigger with
sp_helptext yourtriggername

SQL Server INFORMATION_SCHEMA contents

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 ;

How do I get list of all tables in a database using TSQL?

What is the best way to get the names of all of the tables in a specific database on SQL Server?
SQL Server 2000, 2005, 2008, 2012, 2014, 2016, 2017 or 2019:
SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE='BASE TABLE'
To show only tables from a particular database
SELECT TABLE_NAME
FROM [<DATABASE_NAME>].INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE'
Or,
SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE'
AND TABLE_CATALOG='dbName' --(for MySql, use: TABLE_SCHEMA='dbName' )
PS: For SQL Server 2000:
SELECT * FROM sysobjects WHERE xtype='U'
SELECT sobjects.name
FROM sysobjects sobjects
WHERE sobjects.xtype = 'U'
Here is a list of other object types you can search for as well:
AF: Aggregate function (CLR)
C: CHECK constraint
D: Default or DEFAULT constraint
F: FOREIGN KEY constraint
L: Log
FN: Scalar function
FS: Assembly (CLR) scalar-function
FT: Assembly (CLR) table-valued function
IF: In-lined table-function
IT: Internal table
P: Stored procedure
PC: Assembly (CLR) stored-procedure
PK: PRIMARY KEY constraint (type is K)
RF: Replication filter stored procedure
S: System table
SN: Synonym
SQ: Service queue
TA: Assembly (CLR) DML trigger
TF: Table function
TR: SQL DML Trigger
TT: Table type
U: User table
UQ: UNIQUE constraint (type is K)
V: View
X: Extended stored procedure
SELECT * FROM INFORMATION_SCHEMA.TABLES
OR
SELECT * FROM Sys.Tables
USE YourDBName
GO
SELECT *
FROM sys.Tables
GO
OR
USE YourDBName
GO
SELECT * FROM INFORMATION_SCHEMA.TABLES
GO
SELECT * FROM information_schema.tables
where TABLE_TYPE = 'BASE TABLE'
SQL Server 2012
select * from sysobjects where xtype='U'
exec sp_msforeachtable 'print ''?'''
SELECT name
FROM sysobjects
WHERE xtype='U'
ORDER BY name;
(SQL Server 2000 standard; still supported in SQL Server 2005.)
The downside of INFORMATION_SCHEMA.TABLES is that it also includes system tables such as dtproperties and the MSpeer_... tables, with no way to tell them apart from your own tables.
I would recommend using sys.objects (the new version of the deprecated sysobjects view), which does support excluding the system tables:
select *
from sys.objects
where type = 'U' -- User tables
and is_ms_shipped = 0 -- Exclude system tables
SELECT sobjects.name
FROM sysobjects sobjects
WHERE sobjects.xtype = 'U'
Well you can use sys.objects to get all database objects.
GO
select * from sys.objects where type_desc='USER_TABLE' order by name
GO
OR
-- For all tables
select * from INFORMATION_SCHEMA.TABLES
GO
--- For user defined tables
select * from INFORMATION_SCHEMA.TABLES where TABLE_TYPE='BASE TABLE'
GO
--- For Views
select * from INFORMATION_SCHEMA.TABLES where TABLE_TYPE='VIEW'
GO
In SSMS, to get all fully qualified table names in a specific database (E.g., "MyDatabase"):
SELECT [TABLE_CATALOG] + '.' + [TABLE_SCHEMA] + '.' + [TABLE_NAME]
FROM MyDatabase.INFORMATION_SCHEMA.Tables
WHERE [TABLE_TYPE] = 'BASE TABLE' and [TABLE_NAME] <> 'sysdiagrams'
ORDER BY [TABLE_SCHEMA], [TABLE_NAME]
Results:
MyDatabase.dbo.MyTable1
MyDatabase.dbo.MyTable2
MyDatabase.MySchema.MyTable3
MyDatabase.MySchema.MyTable4
etc.
Any of the T-SQL code below will work in SQL Server 2019:
-- here, you need to prefix the database name in INFORMATION_SCHEMA.TABLES
SELECT TABLE_NAME FROM [MSSQL-TEST].INFORMATION_SCHEMA.TABLES;
-- The next 2 ways will require you to point
-- to the specific database you want to list the tables
USE [MSSQL-TEST];
-- (1) Using sys.tables
SELECT * FROM sys.tables;
-- (2) Using sysobjects
SELECT * FROM sysobjects
WHERE type='U';
Here’s a working example using [Skyvia] using sys.tables.
[Skyvia] should be the link to https://skyvia.com/connectors/sql-server
[1]: https://i.stack.imgur.com/o3qo9.png
Your SQL GUI tool should also have a way to list down all the tables in a database like the one above.
So, whatever suits your need and taste, there’s a code or GUI tool for that.
--for oracle
select tablespace_name, table_name from all_tables;
This link can provide much more information on this
topic
Please use this. You will get table names along with schema names:
SELECT SYSSCHEMA.NAME, SYSTABLE.NAME
FROM SYS.tables SYSTABLE
INNER JOIN SYS.SCHEMAS SYSSCHEMA
ON SYSTABLE.SCHEMA_ID = SYSSCHEMA.SCHEMA_ID
UPDATE 2022:
You can list/show the tables that you created with this simple query in Microsoft SQL SERVER.
select * from SYS.TABLES;
SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE='BASE TABLE'
ORDER BY TABLE_NAME
Thanks to Ray Vega, whose response gives all user tables in a database...
exec sp_msforeachtable 'print ''?'''
sp_helptext shows the underlying query, which summarises to...
select * from dbo.sysobjects o
join sys.all_objects syso on o.id = syso.object_id
where OBJECTPROPERTY(o.id, 'IsUserTable') = 1
and o.category & 2 = 0
Using SELECT * FROM INFORMATION_SCHEMA.COLUMNS also shows you all tables and related columns.
To remove tables added by replication and any other table Microsoft adds run this:
SELECT s.NAME SchemaName, t.NAME TableName
FROM [dbname].SYS.tables t
INNER JOIN [dbname].SYS.SCHEMAS s
ON t.SCHEMA_ID = s.SCHEMA_ID
WHERE t.is_ms_shipped=0 and type_desc = 'USER_TABLE'
ORDER BY s.NAME, t.NAME

Resources