Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Code to retrieve No of rows, no of columns, PK

Author  Topic 

yougandhar1
Starting Member

7 Posts

Posted - 2013-05-31 : 11:40:54
Hello I am trying to retrieve the following. Need help writing the code. I know it sounds simple. I was able to write some of it. But stumbling at the end (Primary Key areas)

1) Table name
2) Schema
3) No of Columns
4) Primary Key
5) No of rows

6) Is there a code to find out what stored procedures are run on a table?

Thanks,
Reddy

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-06-01 : 03:34:41
use following system views

1, INFORMATION_SCHEMA.TABLES - gives Table_name,Schema_Name etc
2, INFORMATION_SCHEMA.COLUMNS - gives columns in a table
3, KEY_COLUMN_USAGE
http://msdn.microsoft.com/en-us/library/ms189789.aspx
4, select count(*) from table will give you no of rows

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

MuMu88
Aged Yak Warrior

549 Posts

Posted - 2013-06-01 : 14:04:49
To answer your 6th question,
you can use system stored procedure sp_depends,
As per msdn this is what this procedure does:
Displays information about database object dependencies, such as the views and procedures that depend on a table or view, and the tables and views that are depended on by the view or procedure. References to objects outside the current database are not reported

here is how to use it:
[CODE]
EXEC SP_DEPENDS [TableName]
[/CODE]


In addition, you can run the following command to see the system stored procedure sp_help that returns all the information pertaining to a database object.

[CODE]
EXEC sp_help [TableName]
EXEC sys.sp_helptext
@objname = N'dbo.sp_help';
[/CODE]

Refer to this link for even more information :
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=185721

Refer to this link for even more information :
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=185721
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-06-01 : 20:12:24
quote:
Originally posted by MuMu88

To answer your 6th question,
you can use system stored procedure sp_depends,
As per msdn this is what this procedure does:
Displays information about database object dependencies, such as the views and procedures that depend on a table or view, and the tables and views that are depended on by the view or procedure. References to objects outside the current database are not reported

here is how to use it:
[CODE]
EXEC SP_DEPENDS [TableName]
[/CODE]


In addition, you can run the following command to see the system stored procedure sp_help that returns all the information pertaining to a database object.

[CODE]
EXEC sp_help [TableName]
EXEC sys.sp_helptext
@objname = N'dbo.sp_help';
[/CODE]

Refer to this link for even more information :
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=185721

Refer to this link for even more information :
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=185721


this will still not give cases where table was used inside dynamic string like sp_executesql

I prefer this

http://visakhm.blogspot.in/2012/03/advantages-of-using-syssqlmodules-view.html

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -