This is very simple and can be achieved using system table sys.tables.
USE YourDBName
GO
SELECT *
FROM sys.Tables
GO
This will return all the tables in the database which user have created.
Reference : Pinal Dave (http://blog.SQLAuthority.com) , BOL





How do I list all tables on a server?
select * from sys.tables
(or)
–list of all user defined tables
select * from sysobjects where xtype=’u’
sp_msforeachdb @command1=’USE ?;SELECT * FROM sys.Tables’
select * from sys.tables
error: Invalid object name ’sys.tables’.
i use 2000 sql serv.
How do I list all databases on a server?
use master
select * from sysdatabases
Hey guys, I know that Pinal is pretty busy, so I thought I would help you out with your questions.
To get all the databases on a server, you can use the same technique, but just a bit different query.
USE Master
GO
SELECT *
FROM sys.Databases
GO
For a list of all tables on a server, I would use the sp_msforeachdb procedure like this.
sp_msforeachdb @command1=’USE ?;SELECT * FROM sys.Tables’
You can see some more examples of sp_msforeachdb on my own blog, here is a link to the article.
http://www.sqlstop.com/index.php/2007/07/11/what-bol-doesnt-know-wont-hurt-you/
Thanks Shawn,
Due to amount of emails and comment I receive I am not able to keep up with all of them. However, eventually I am able to answer them.
Regards,
Pinal Dave (SQLAuthority.com)
i can’t do it;
====================================
USE Master
GO
SELECT *
FROM sys.Databases
GO
error: Invalid object name ’sys.Databases’.
====================================
select * from sys.tables
error: Invalid object name ’sys.tables’.
=====================================
i use 2000 sql serv.
SELECT *
FROM information_schema.Tables
This will display the list of table in the database
thnks for query on sqlauthority
I Think it is not working in SQL SERVER 2005.
Hello Ram,
Its working. INFORMATION_SCHEMA views are SQL-92 ANSI standard views and will work in future versions also. This view returns only the user tables on which you have SELECT permission. Let us know the issue you are facing.
Regards,
Pinal Dave
thanks
i got it
but
select * from sys.tables
error: Invalid object name ’sys.tables’.
i use 2000 sql serv.
Hi
Thank’ sir
How could i get all the data from all the table of the specific database..?
plz reply on my mail..if possible.
thanks in advance
Bhuwanesh..
I want to view all the table in a database
USE
SELECT *
FROM information_schema.Tables where TABLE_TYPE=’BASE TABLE’
hi,
i want to see all the table names from a sigle database in sql server 2000. please anyone can help me!!!!!!!!!!!!!!!!!!!
Thank you
how can i know that any changes is occur in my table structure. is there any field in any system table that value is change if any changes is done in table structure???????
please give me reply……..
thanks…………..
i can not see systables in my database please tell me how i can see and show systables
I want to view all the table in a database
hi,
how to take the data to all back up in .sql with data also,
example my sql data base so use export command like it show the sql format also in the same i need the mssqlserver 2005 database back up to all data with quires
How do i list all database triggers only in an instance?
Hi,
Which query i used to list the tables in Particular Database?
Mani,
Please read the post on top.
Regards,
Pinal
Thank U……..
Regards,
Mani……..
i want to see all tables in sqlserver2000;
i want to see all tables in sqlserver2000; given queries are not working im my system;
Thank you…very much…its very helpful …
Thankyou & Regards,
Princes
Works like a dream!! Thank You
Thanks for ur help in viewing all tables from a database. Can u tell me how to view the table structure ?..
Thanks for your help but i am looking something like where i can display the list of database . Please anybody help me to listing of database in sql 2005.
Hi,
Thanks this helped me.
can anyone tell me how to see only the user defined tables or
stored procedures or views or functions in a given database ?
Thanks
Thanks a lot!!!!!!!!!!!!!!
dear sir,
i want to know how to see the structure of table……..
In oracle desc tablename means it will show the structure of that particular table but the sql server it is not show ………
i need solution for this question as early as possible …..
advance thanks ………. please consider as a urgent…..
Suresh,
to view the structure of the table in MS SQL Server
try this
Solution: sp_help tableName
Thanks,
Venky
I want to see all the data in all the tables in a database using the least code possible !!! Please let me know ASAP…
how many user using my db
(same user) ?
thaink you
what about
only user tables?
I got much information regarding the database list of objects
Thank you
Harinath
I am not getting abt master table.
Thank you. Helps a lot :)
How could i get all the data from all the table of the specific database?
Thanks a lot in advance!
Querry to fetch all tables in a database:
select * from sysobjects where type=’u’
————————————————–
Querry to fetch all procedures in a database:
select * from sysobjects where type=’p’
Regards
Rap…
to view all tables……
SELECT * FROM TAB;
this will work…….
Dear sir,
How do i get all databases in sql server
Regard’s
pushparaj
Dear Friends,
I want to know how to get all table name from particular data base in sql server 2005.
Thanks.
Hi ,
I am creating an application where I have to list all the servers and on selecting any server the combobox should dynamically have all the databases that are present on that server so that the user can select any one of them. Can anyone please help. its urgent!!!
thanks,
Gaurav
@Kaushal
I want to know how to get all table name from particular data base in sql server 2005.
select * from sysobjects where xtype=’U’
if i want to check the primary and foreign key relations of particular table is it possible….if there is any solution plz tell me……..
thx in advance
Regards,
Rekha…
I need to display only user defined tables from the current database.
Hi Tonny,
U will get the user defined tables using any query below.
select * from sys.tables
select * from INFORMATION_SCHEMA.TABLES
select * from sys.objects WHERE type =’U’
Hi everyone, I am new here.
I am trying to get a list of all of the tables in a DB and the size/# of rows in each table.
Does anyone know how to get this data?
Thanks in Advance for your help!
dear sir,
i want to diplay all databases name in sql2000.
@ahila
use master
Select name from sysdatabases
hope this helps,
Imran
how i can see all tables in sqlserver2000;
thanks..
/*
USE YourDBName
GO
SELECT *
FROM sys.Tables
*/
i used the above code but shows error stating
Server: Msg 208, Level 16, State 1, Line 1
Invalid object name ’sys.tables’.
is it dude to any syntax error
???
@dumdum
Script1 :This will list all the system tables and userdefined tables in the database. ( meaning all the tables in the database)
use master
create table #temp1 (id int identity , name)
insert
SELECT name FROM sysobjects where type = ‘S’ or type = ‘U’
Script2: If you want to list all the system and user defined tables in all the database at a time, then you can use this script,
I am sure there must be a another good way of doing this,
use master
create table #temp1 (id int identity , name varchar(128))
go
create table #temp2 ( table_name varchar(128), db_name varchar(128) )
go
insert into #temp1
SELECT name FROM sysdatabases
go
declare @var int
declare @db_name varchar(128)
declare @cmd varchar(1280)
set @var = 1
while @var <= (select count (1) from #temp1)
begin
select @db_name = name from #temp1 where id = @var
set @cmd = ‘SELECT name , db_name = ‘+””+@db_name+””+’ FROM ‘+ @db_name+’..sysobjects where type = ‘’s”or type = ”U”’
insert into #temp2 exec (@cmd)
set @var = @var+1
end
select table_name As ‘Table Name’ , db_name As ‘Database Name’ from #temp2 where table_name not like ‘#temp1%’ and table_name not like ‘#temp2%’
drop table #temp1
drop table #temp2
go
Hope this helps,
Thanks,
@martin joseph
You can execute that query in Sql Server2005, it will give you all userdefined tables. Your query is absolutely correct.
In Sql Server 2000 I dont think we have an object by name sys.tables that is why we canno use that query and that is why it is returning errors.
There is something similar if you might want to use it, its sp_tables, it will list all the tables and views along with some good information in the particular database.
Hope this helps,
Imran.
Can I retrieve all tables from all database in SQL Server?
If yes please give me the query.
select * from sysobjects will give you all the tables in the database.
Can anyone please tell me how to get all the tables which has same column name in a database
thanx imran for ur reply…
it was very helpful … i might have wasted a lot of time
using the code in ” sql server 2000″
Hi friends,
these are also some use full
please check
Querry to fetch all user tables in a database:
use yourdbname
select * from sysobjects where type=’s’ order by name
Querry to fetch all system tables in a database:
select * from sysobjects where type=’U’ order by name
Querry to fetch all procedures in a database:
select * from sysobjects where type=’p’
Querry to fetch all functions(userdefined or system) in a database:
select * from sysobjects where type=’fn’
Cheers,
Venkat
Hi friends,
This is very use full for finding the size, table, find …
query:
exec sp_spaceused [tablename]
result:
name rows reserved data index_size unused
tablename 145 888 KB 248 KB 16 KB 624 KB
Cheers,
venkat919
Hi Friends,
After i did the exercise
to get the all user tables from database:
— this will gives the one extra table it is “dtproperties”
1. select * from sysobjects where type=’U’ order by name
— this will gives the exact user tables from database
2.
SELECT * FROM sysobjects
WHERE
type=’U’
and
name != ‘dtproperties’
ORDER BY [name]
Cheers,
Venkat
Dear Friends
How To Find Particular Table stay Which Database ?
I want to build an if statement that will create a table with different information in it based on the database that is currently in USE (the database name in the USE statement). Different databases USE the same table but have different information in them.
Hi Pinal,
i want to fetch all databases and tables from sql server database, mysql database, oracle database and ms-access database. can u suggest me how to do this. if possible post an example for each. i want to bind all those details to a treeview in asp.net.
Thanks in Advance,
Santosh.
Does anyone have any thoughts on how to accomplish the following? I would like to query a SQL2005 db and return the table and column that contains a particular value. Based on what I have read from this site – this seems to be a good start. SELECT * FROM information_schema.Tables
I would like to add syntax that will find a varchar value and return the table, column, and value itself.
Thanks for the help.
Thanks !
I can get a list of all stored procedures that I’m using in my Db via this query:
SELECT * FROM SYS.PROCEDURES WHERE [Name] LIKE ‘usp%’ ORDER BY [Name];
(I’m looking for the name to be like ‘usp’ because that’s the prefix to all of the stored procedures I’ve created.)
What I’m looking for is where the text of the stored procedure is located. For example, I need to drop the use of a table that is causing performance issues (they’re using labor intensive triggers) and want to search the text of all stored procedures I’ve created for the table name so I know which stored procedures need changing. There are over 130 stored procedures and looking through every one of them seems too time consuming.
It would be nice to run a query that would do the search for me…like this
SELECT * FROM SYS.SomeSystemTable s INNER JOIN SYS.PROCEDURES p ON s.object_id = p.object_id WHERE [Name] LIKE tblBadTableName%’ .
Any Ideas?
I aplogize, Pinal, I found the answer within your web site.
http://blog.sqlauthority.com/2007/09/03/sql-server-2005-search-stored-procedure-code-search-stored-procedure-text/
Hi,
Could you please let me know how to retrieve the deleted table list in SQL.
Thanks,
Suba
Hi,
How can i know that how many times my tables,views and/or functions have been executed or used by the users?Can anybody help me or can give me some idea ,plz?
Hi,
Is it possible ???
how to select data when you dont know the table name, only thing I know is database where the column resides
like I have to find how many CID columns are in my database and in which table they are resides???
Please do help me … because I have to work on Large number of database tables ….
Hi,
I want to list of my database tables’ column name with their Datatype.
Can you tell me how to do that?
Regards,
Pooja
I use this to get all Table and Sprocs on all Database on a server:
sp_msforeachdb @command1=”USE ?;select name, type from sys.objects where type in (‘U’, ‘P’) and name not like ‘dt%’ and name not like ‘#%’ order by type”
Note sp_msforeachdb has a limits the query to 128 digits.
This code shows all the tables and sprocs for each DB, the size of each table int he DB and the DB file sizes:
sp_msforeachdb @command1=”USE ?;select ‘?’,name,type from sys.objects where type in(‘U’,'P’) and name not like ‘dt%’ and name not like ‘#%’ order by 3″
, @command2=”exec sp_MSforeachtable @command1 = ‘exec sp_spaceused’”
, @command3=”exec sp_helpfile”
GO
i want default databases in sql server and their tables like EMP table in oracle . plz help me
hye,
i want to modifier a field in a database and this field is related to many database what is the code to do to modifier this field in all database at the same time
best regards
Everyone that contributed to this site has helped me tremendously. Thanks for leaving your informational note. This blog Rocks who do i give the credit to?? thanks again.
Hi pinal,
i need to write the Use statement inside the stored procedure.
but it throws error.
Is there any alternate way for this?
let me make it more clear.
i have created one stored proc in master database which takes database name as parameter.
and i want to execute the code on the respective database.
How can i achieve this?
Thanks,
Anirudha ( SQL -Developer)
Grat tips, tanks a lot !!
Good Participation Friends.
I will reply all the questions asked.
Please keep it going.
Regards,
Pinal Dave
Hi Mr.Pinal,
In SQL server 2000 i got the error message.
Server: Msg 208, Level 16, State 1, Line 1
Invalid object name ’sys.Tables’.
Please help on it.
Regards,
Krishna
What should i do to become the future C.E.O of S.Q.L
@ Krishna,
In SQL Server 2000, there is no view called ’sys.tables’. This view is only in SQL Server 2005.
You can use below quiries to get user defined tables names in SQL Server 2000.
select * from sysobjects where table = ‘u’
or
select * from information_schema.tables
Regards,
IM.
HI Imran Mohammed,
Thank you for your information…..
Regards,
Vidhya
In SQL Server 2000 this works well to return all User Tables:
SELECT Name
FROM FOTNAudit.dbo.sysobjects
WHERE xtype = ‘U’
ORDER BY Name
FOTNAudit is a specific db name. Replace with whatever you need to use.
Hello sir
How can get all the table list from Sql Server 2005.
Thanks & Regards
Jignesh Patel
@Jignesh Patel
This script will give you tables in all database ( meaning in whole SQL Server )
DECLARE @CMD VARCHAR(1000)
create table #TableNames ( DatabaseName sysname, TableName sysname)
SET @CMD = ‘USE [?]; SELECT DB_NAME()DATABASE_NAME, NAME FROM SYS.Tables’
insert into #TableNames EXEC SP_MSFOREACHDB @CMD
SELECT * FROM #TableNames — Where DatabaseName not in ( ‘master’,'msdb’, ‘tempdb’, ‘model’ )
drop table #TableNames
If you dont want system databases table names, then you need to remove — ( remove comment symbol) from above script, otherwise it will include system database table names also.
Regards,
IM.
thanks Imran
Its working…
How could I find the structure of all tables in a DB in one query ?Is it possible?..
Advance thanks for the help…. :)
hello sir
i want retrieve table names of a particular selected database
please help me
first i will list the databases present in my server
then i will select one of the databases
now i want to retrieve table names of a selected database
i hope my question is clear
Advance in thanks
Hi Rashmi,
Check below query,
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]
Thanks. It was a great help.
I see the previous admin created additional database files on different drives and used this command to ge the name of each file and it’s location.
How do I find out what tables are being stored in each of these files?
Sorry, this is the command I used to see the file names and locations: select * from sys.database_files
hi,
i want to make a CSV of each tables separate.
What is sql query for this? i need to run in query analyzer.
i made a connection bet’n MS-SQL and php for this, and this php file runs every day.
Thanks
-Hitesh
how to list of tables/ views in the databases that have a direct ODBC connection
Can any one help me
Well prepared and answered were very useful to all of our team members. Thanks for your timely work and helpful
I want queries about sql server 2005
Hello Friends i need a query
Table
id , Name , Age , Salary, Add1,Add2,Add3,……
This table is generated by a tool, so i dontknow exact coloume except few ,
so i want to retrive all value for those coolume which is specified in query.
Plz help or communicate if not getting the exact problem
Hi, i m new in database. Actually i want to know how can i get tables from the database using querry.
Is there any inbuilt querry or we have to make it..
please reply soon…..
Hi Vipin,
if you are using SQL 2005, then you can use:
select * from sys.objects
WHERE Type=’U’
Thanks,
Tejas
Hi,
——————————————
DECLARE TABLE_NAMES_CURSOR CURSOR
FOR SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE LIKE ‘BASE TABLE’
DECLARE @ONE_TABLE_NAME VARCHAR(250)
OPEN TABLE_NAMES_CURSOR
FETCH NEXT FROM TABLE_NAMES_CURSOR INTO @ONE_TABLE_NAME
WHILE @@FETCH_STATUS = 0
BEGIN
PRINT @ONE_TABLE_NAME
DELETE FROM @ONE_TABLE_NAME
FETCH NEXT FROM TABLE_NAMES_CURSOR INTO @ONE_TABLE_NAME
END
CLOSE TABLE_NAMES_CURSOR
DEALLOCATE TABLE_NAMES_CURSOR
————————————————-
the above code using in sql server2005
in in this @ONE_TABLE_NAME i’ll be storing table name,
when i print this i can able to print but the same thing when i use for deleting rows from table i’m getting error
DELETE FROM @ONE_TABLE_NAME
please help me
thanx
ambu
@ Ambanna,
You cannot give table name as variable for delete statement, select statement …
But you can store that whole statement into a variable and execute that variable which will execute whole statement.
some thing like this,
declare @sql varchar(1000)
set @sql = ‘DELETE FROM ‘+@ONE_TABLE_NAME
Exec (@Sql)
Place above three lines in your cursor, it should work.
Suggestion:
Instead of going through all this pain of cursor, why don’t you take a look at sp_MSForEachTable. Very easy to handle and less complex. Just one line code.
Link : http://www.devx.com/getHelpOn/10MinuteSolution/20551
It has good explanation.
Regards
~ IM
Thanks for replying, it will great help to me..
Thanks Again.
Thank you very much Imran
U’ve save a lot of my time
thanx once again
Sir,
Actually i m doing a program in which database operations are performed from user interface using sql dmo.
when i m creating database it works,but when i want to delete database it gives some error…
so plz provide me the code for deleting database from user interface.
please dont give solution through sql querry….
please reply soon…….
I get this error:
Msg 208, Level 16, State 1, Line 1
Invalid object name ’sys.Tables’.
@Shivam
Script is to be executed in sql server 2005.
cannot be executed SQL Server 2000
~ IM
thanks a lot buddy
Very informative thread…
Can anybody tell me how may I get the constranits if any applied on the table’s column?
Thanks in anticipation for the help…
I am trying to select and id from sysobjects in another database in a stored procedure using SqlServer 2005.
I can not use a use databaseB statement inside of a stored
procedure and if I try to run this from databaseA
Select id from databaseB..sysobjects where …
I get a null id.
Any help is appreciated.
Thanks
Chip
hi all
is there is any sql query which gives all triggers in a database?
Try this
select * from sys.triggers
select * from sys.triggers WHERE name like ‘%dup%’
Ruwan
@Ruwan,
Just to add one more cent to your answer, That will give you list of DML Triggers. But you cannot get list of DDL triggers through that script.
Because the question was asked to find triggers in database, your solution works perfectly fine, no doubt about it.
But if we are talking about in general, DDL triggers are defined at server level but not database level, so sys.Triggers will not provide information about DDL triggers.
IM
Hi Pinal,
Can you explain me what is sql server 2005 client? As it is free but what else I required. Do I need SQL Server 2005 enterprise?
As I never worked with Sql Server, I always worked with oracle.
Please help me out
Thanks in Advance.
Rohitas Gangwar
Hi I am Looking for a Query that will list out tables by a keyword
I use to run following Query in Oracle for teh same purpose:–
Select * from tab where tname like ‘%BANK%’
So, this query will list out all the table names with ‘BANK’
I tried to run a similar query in SQL serever 2005:–
select * from sys.TABLES WHERE NAME like ‘%BANK%’
It didn’t return any results. Also, I want my Query to search table names from all Database( I have tables with the keyword mentioned in the database)
@Chaya,
You need to understand that, there is huge difference between oracle and SQL Server naming standards,
In Oracle, we will have one database and many schemas, so if you run below script in oracle in a database, it will search this table in all schemas but only in that one database, since ORACLE has only one database per server, there will be no problem.
Select * from tab where tname like ‘%BANK%’
BUT, SQL Server has one more layer to it, In One SQL Server instance, you can have multiple databases, and in one database you can have multiple schemas.
ORACLE :
STAGE1 : SERVER
STAGE2: ONLY 1 DATABASE
STAGE3: MORE THAN ONE SCHEMA IN DATABASE
If you execute any script at serve level, because there is only one database, it can give you results.
SQL SERVER:
STAGE1 : SERVER
STAGE2: MORE THAN 1 DATABASE
STAGE3: MORE THAN ONE SCHEMA IN EACH DATABASE
If you execute any script at server level, it sees many databases, and if you do not specify the database name, by default it will search in masterdb, which is not the database you want to search in,
So in SQL Server you first need to connect to the right database and then execute your sql script,
use database_name
select * from sys.TABLES WHERE NAME like ‘%BANK%’
~ IM
Hey, thanks Imrqan you resolved my doubts.. Henceforth I will make sure that I write querirs as per the database connected to.
Hi Pinal,
Sometime I need to find some text in the whole database to detect the table and the field which contains this kind of information.
So I have an idea to create a SELECT from all tables, and then unload it in text file. I decided to do it in XML format:
So I open cursor that passes all tables and does SELECT from them.
declare @tableName varchar(100)
DECLARE tables_cursor CURSOR FOR
SELECT name
FROM sys.Tables
OPEN tables_cursor
FETCH NEXT FROM tables_cursor
INTO @tableName
WHILE @@FETCH_STATUS = 0
BEGIN
EXEC (‘SELECT * FROM ‘ + @tableName + ‘ FOR XML AUTO’)
FETCH NEXT FROM tables_cursor
INTO @tableName
END
CLOSE tables_cursor
DEALLOCATE tables_cursor
It gets all-tables content as many selects, so I can’t mark it and copy-paste in txt file to search in.
How can I get this output in file or in console ?
Or, may be there is another solution of my problem?
I want to know how to list all my user on my SQL 2005 database ? Can someone help me out ?
Thanks in advance
@Mark,
If you are using SQL Server 2000, you can use master..syslogins. For SQL Server 2005, you can use sys.syslogins.
Thank you~!
I can make my Table list… so easy..
How do I find out the server name. I have the database name with DB_Name(). There’s got to be something out there, I can’t find it.
Query or stored procedure to List of all the databases to which user has access in sql server 2005.
Hi,
one of the previous command has the statement like
if we run this bellow query it displays the list of tables in the database
SELECT * FROM TAB
It does not works in SQL SERVER 2005.
select * from sys.tables
only used to display the list of tables in the database.
With Regards,
Dhinesh
Is there a way to list all tables in the SERVER (not just in the current database)?
I am in @Database1 and need to know if a certain table/column exists in @Database2.
While in a SPROC in @Database1, how do I return a bit to tell me if a certain table exists in the other database?
hi ,
please tell me how to genrate each IDs privileges
regards
Zee
Hi Pinal,
i have 400 tables in a database. I have a requirement which need exhaustive document showing all the dependencies and information about all tables in a database(user tables). Please let me know if there is anyway to do this.
you can see all table information by
select * from sys.tables
or
select * from infomation_schema.tables
or
sp_tables
you can see all procedures by:
select * from sys.objects where type=’p’
you can see all databases by:
sp_databases
you can see all indexes by:
select * from sys.indexes
How to get tables from sqldatabase
Dear sir,
i want to know how to see the structure of all tables…..
sp_help tablename gives the output only for 1 table..i have 200 tables in my db.. should i hit the query for each table.. or is there any other solutuion for it..
plz reply…
thanx in advance.
Hi Suyog,
You can see the structure of all tables. by using….
select * from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME in(select TABLE_NAME from INFORMATION_SCHEMA.TABLES)
Hi Sourabh,
UR answer is ohkk.. but i need INDEX constaints also in it.. so wat should i do??
plz reply..
Great man!
Hi Mani,
Please read original post as it contains answer to your question.
USE YourDBName
GO
SELECT *
FROM sys.Tables
GO
Kind Regards,
Pinal
Dear Sir,
that query is not showing me index ,constraints,columns details…
is there any other way..except sp_help tablename..
can we go for sp_msforeachtable???
can u plz provide exact query for same..
hi suyog,
If you want to see structure of all tables you can use:
select * from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME in(select TABLE_NAME from INFORMATION_SCHEMA.TABLES)
If you want to see index details then you can use:
select name,type_desc from sys.objects where type=’PK’
Hi suyog,
Not a proper way but i think u can use
declare @tableName as varchar(100)
declare @countValues as int
select @tableName=name from sys.tables
select @countValues=COUNT(*) from sys.tables
declare @startvalue as int
set @startvalue=1
while @startvalue<@countValues
begin
declare @exe as varchar(100)
set @exe='sp_help '+@tableName+''
print @exe
exec(@exe)
set @startvalue=@startvalue+1
end
after executing these statements you can see all tables structure info. If you want to combine all data into single resultset you can use temporary tables etc..
you can use this,
sp_msforeachtable ’sp_help [?]‘
Appreciate your blog. Does anyone have any ideas on how to lookup the source table/column associated with a column in a given view? I’m looking for a way to map a column from a view (that shows up in syscolumns) back to the originating table and column that the view column is based on.
how do i know the structure of table like in mysql or oracle viz desc.i uses sp_privileges but it return the result that i dont want. i want to know the data type and its size
zee
Select * from table name
where Fname like ‘%a%’ and Fname like ‘_li’
i hope you like it
zee
Select RNo+2 As RNo,FName From DBtab
Where FName=’lia’
Thanks Sir…
Big help
select * from sysobjects where xtype=’U’
How to find the number of records from all the tables in sql 2005?
Hi Lakshmi,
I think there is no any build in table or any built in function in sql server through you can count number of records in all the tables.
But i have created a logic through you can get number of records from all tables….
You can use this:
declare @dynamicqry as varchar(1000)
create table temp(tid int)
declare @startvalue as int
declare @endvalue as int
set @startvalue=1
select @endvalue=count(*) from INFORMATION_SCHEMA.TABLES
while @startvalue<@endvalue
begin
declare @tablename as varchar(100)
select @tablename=Table_name from (select Table_name,ROW_NUMBER() over(order by table_name asc) 'RowNum' from information_schema.tables) tt where RowNum=@startvalue
set @dynamicqry='insert temp select count(*) from '+@tablename
print @dynamicqry
exec(@dynamicqry)
set @startvalue+=1
end
select SUM(tid) from temp
Hello,
I tried to run the following query for display columnames in Ascending order from table Item:-
exec sp_columns Item order by Column_name Asc
I get the following error:-
Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword ‘order’.
How shhould I construct the query in order to get the desired results?
Hi Chaya,
you can use the following query.
select *from information_schema.columns where table_name=’ITEM’ order by column_name
Hi
I am new to this SQL.. I want to know how can get the tables from data base..
Can you tell me in a easy way please… I need it as soon as possible to resolve an issue :(
Thanks
Vidhyasri M
Hi Vidhyasri,
you can use any of this query to get all the tables from a particular database
select * from INFORMATION_SCHEMA.TABLES
or
select * from sysobjects where xtype=’U’
or
select * from sys.tables
Regards,
Ambanna Yatnal
Hi Vidhyasri,
you can use:
sp_tables
OR
you can use:
select * from information_schema.tables
OR
You can use:
select * from sys.tables
OR
you can use:
select * from sys.objects where type=’U’
Note: type=’U’ not use xtype
So all these are different way of getting all tables from sql server.
what is Invalid object name ’sys.tables’. in sql server 2000???
can u plss help me!!!!!!!!!
use this code:
SELECT * FROM INFORMATION_SCHEMA.TABLES
reply by example
how can i display the no of tables present in the database and also displays the table name present in the database
I have Query which requires me to display ‘Active’ or ‘Inactive’ in ‘Status’ Column based on the following Condition:-
IF Todate IS Null or Todate>=Getdate() then status = ‘ACTIVE’
If Todate<Getdate() then status = 'INACTIVE'
Whereas 'Status' is not a column by itself in Agentownership table. Above condition needs to be added in the following query:-
Select AO.OwnerfirstName, AO.OwnerLastName, P.PhoneNumber, AO.FromDate
From AGENTOWNERSHIP AO
Left Join Agent A on AO.AgentID = A.AgentID
Left Join Phone P on P.PhoneiD = AO.PhoneiD
I have the following Columns in my AgentOwnership table:-
AgentOwnershipID
AgentID
OwnershipRoleID
OwnerFirstName
OwnerLastName
PhoneID
AddressID
FromDate
ToDate
OwnerDateofBirth
OwnerDriversLicense
OwnerDriversLicenseStateId
OwnerEmailAddress
Help please! I am still trying to complete the query
@dharma
Look at INFORMATION_SCHEMA.TABLES or sys.tables.
@Chaya
Just add a CASE statement to the query:
CASE
WHEN Todate < Getdate() THEN 'INACTIVE'
ELSE 'ACTIVE'
END Status
hi
In all data bases have a common table ah?
Hi.
i want a SQL querry to list the tables in a schema in DB2. can anyone help me!! Thanks friends!!
Hi,
I m converting my .net application from Mysql to Ms sql
server 2005 but the queries just don’t go well. eg.
MS sql server gives me error (SELECT * FROM Employee).
Thanks
Hi
I’m using stored procedures to create temp tables with info and use it in SQL reporting services.
Now I have major collation issues making my life difficult.
How can I avoid collation issues in my stored procedures, or how can I change the collation of all tables and all columns in a database.
I get the following error
Msg 468, Level 16, State 9, Procedure BISSystemsOverdue, Line 103
Cannot resolve the collation conflict between “Latin1_General_CI_AS” and “SQL_Latin1_General_CP1_CI_AS” in the equal to operation.
Hello Bertus,
Different collation of working database and tempdb could cause the collation conflit when using temp tables. To resolve this either specify the collation while creating the temp table or change the default collation of tempdb.
Regards,
Pinal Dave
its very useful info.
Thanks
Hi,
How can I get all the databases on the sever and thier location(file name). i tried too many thing but I couldnt get it.
Thanks
Hi all,
I wanted where condition with two paramters which works in oracle but i wanted to execute in sql server 2000 which gives me error.
Kindly check the query
select * from t1 where (column1,column2) in (select column1,column2 from t2)
i require same query in sql server.If any have ideas on this plz forward to my mail id
thanks and request,
lokesh
Hi Lokesh,
Write as below:
select t1.* from t1 INNER JOIN t2
ON t1.column1 = t2.column1
AND t1.column2 = t2.column2
or another way:
select * from t1
where column1 in (select column1 from t2)
AND column2 in (select column2 from t2)
Regards,
Pinal Dave
How can I get all the databases on the sever and thier location(file name). i tried too many thing but I couldnt get it.
Looks like some good information here. I’m trying to create two combo boxes. The first one would show all the SQL servers (so I wouldn’t have to program to a specific server) – so I could easily compile the program for our clients. Once a server is selected, then show only those databases that contain a specific table name (to show only those databases that apply to my program) – even the first combo box could do this as well – only showing those servers that have any database that contains a specific table name.
I’m using Visual Studio 2008 and have a dataset in the program. Is there an easy way to update that with the selections from these dropdown boxes?
Thanks in advance for any help.
Hi Mike,
Use the below script:
Use tempdb
GO
SELECT TOP 0 * INTO DatabaseFiles
FROM sys.database_files
ALTER TABLE DatabaseFiles
ADD DatabaseName varchar(255)
EXECUTE sp_msforeachdb ‘INSERT INTO DatabaseFiles SELECT *, ”[?]” FROM [?].sys.database_files’
SELECT DatabaseName,[name] as [FileName],physical_name as FilePath, * from DatabaseFiles
Regards,
Pinal Dave
Hi Pinal,
Am working on scripts to help me in daily monitoring tasks..
I want to get the database names, its file names, total size and available space..till now am able to get all the bits except for the last one
—————————————————————————
SELECT a.name as [DBname], b.name AS ‘File Name’ , case when b.type=0 then (select ‘Data’) else ‘Log’ end as [File_type] ,b.physical_name AS ‘Physical Name’, b.size/128 AS ‘Total Size in MB’,
b.size/128.0 – CAST(FILEPROPERTY(b.name, ‘SpaceUsed’) AS int)/128.0 AS ‘Available Space In MB’
FROM sys.databases a left outer join sys.master_files b
on a.database_id = b.database_id
—————————————————————————
This gives me list of everything what i want on that server but regarding “available space” it can only retrieve for the current database files..I want that for all the database files on that server..
Looking forward to your reply..
Thanks
Shree
how do i select all the records in a particular database using a particular in where condition..i had a commom column Eldate in all tables……..