Four T-SQL commands for your toolbox

2 minute read time.

I have used the following T-SQL commands quite often when working on the SQL Server side.  There are other ways to find some of this information, but you should have these commands in your toolbox. 

Using the SQL Server Management Studio(SSMS) interface to find information about table definitions, indexes, or triggers is quite common.  It can be done by navigating to the appropriate table, then clicking the folders underneath until you reach the 'Indexes' or 'Triggers' folder.

Another way to get details about tables I that normally use is the T-SQL command, sp_help.  The sp_help command can tell you details about tables.  You can see below after running the command, there are a few result panels. The second panel shows you a list of the field(column) names. You can also see the field definitions and whether the field is nullable or not. 

There is a panel that shows a list of the indexes and whether they are clustered or non-clustered. It also shows the combination of fields that make up the index. 

There is a panel that states if there are any Identity columns. In this example, there is a field named ROWID that is an Identity column. You can also see the Seed or starting value and the value that will be incremented. 

The next T-SQL command is sp_helpindex.  This command simply shows a list of the indexes created against a table. In the example below, you can see the name of the index, followed by the index description.  The index description tells us whether the index is a clustered or non-clustered index.  The index keys are the fields that are part of the index. 

The third T-SQL command is sp_helptrigger.  This command will show a list of triggers that belong to the table. In this example you can see a list of the triggers for the SORDERQ table. 
The trigger naming convention tells you the type of trigger it is(Insert, Update, or Delete). You can also tell the type of trigger by looking at the isupdate, isdelete, isinsert, columns.  A "1" means yes and a "0" means no.

The last T-SQL command I wanted to share is sp_helptext .   This command is used when you want to see the code behind something.   For example, if we wanted to see the T-SQL code behind the SORDERQ_INS_BI trigger, we would see something like this. 

You will probably use some of these commands more than others.  I hope you get a chance to try out these commands. They can provide a lot of detail to help you find answers about tables, indexes and triggers.