By Guest Authors on 20 December 2000 | Tags: Tutorials , Transact-SQL

This article was written by Rob Taylor (taylo in the forums). It talks about Transact-SQL and stored procedures and how they would be useful for ASP developers.

What is Transact SQL?

Transact SQL, also called T-SQL, is Microsoft's extension to the ANSI SQL language. It is the driving force of Microsoft's SQL Server and is a dynamic database programming language. There have been several extensions added to the ANSI SQL language that have become their own SQL language. Oracles PL/SQL is another. So if you were using an Oracle database, you would do database programming in PL/SQL. Just like you use T-SQL with SQL server.

How is T-SQL Used?

T-SQL is written inside of a stored procedure. A stored procedure is a stored set of SQL commands that sit on the physical server. In this case the SQL server. They are compiled after their first use and take heavy burden off the server. Often with ASP development you run in to situations where interaction between the database and the application are rapidly in succession. Like this:

A new user comes in. Lets put him in the users table. Return the identity. Now lets update the member count for his company in the Company table. Add him to the company member’s table with his new ID. Another company member sponsored him so lets track all that as well.

In a normal ASP application we would be doing ALL of the above from the application. We would execute 1 SQL statement, come back and do the next, come back and do the next, etc.... Without a valid reason for doing so. The above scenario could all be done dynamically with T-SQL in 1 stored procedure call. Thus several SQL statements execute with only 1 trip to the database as opposed to several.

Why T-SQL?

Static SQL, like you write in your ASP pages, has several drawbacks. The biggest being that it is static. With TSQL you can build your queries to get a high amount of reuse out your objects. Much like you would use IF statements and Select CASE statements in ASP program, you can do the same with T-SQL. The following is an example of a TSQL statement that selects a different field in the SQL Server Database based on the parameters passed to the Stored Procedure (Stored Proc):
@cat int,
@Price nvarchar(10)

Select CODE,TITLE,Version,Status,

Case @Price
    When 'Price' THEN Price
    When 'PriceA' Then PriceA
    When 'PriceB' Then PriceB
    When 'PriceC' Then PriceC
From Products
Where Category = @cat ORDER BY CODE

In the above example I am selecting a different Price field based on the user level of the buyer. Some additional benefits:

  • Faster Processing. Sure I could have used ASP to generate the same SQL query but at an expensive of extra data processing, combining technologies, and complexity to update.
  • Maintainability. Stored Procedures are essentially functions. Just like you build a function for maintenance, a stored procedure works the same way. It is an object-oriented approach to database programming at the database level. Not the application level (which is not an option regardless).
  • Security. Stored Procedure calls say nothing important to the observer. If you are concerned about who knows what about your database, you hide practically everything with stored procedures.
  • The Editor Itself. Editing stored procedures on the SQL server is also far more practical and user friendly than opening ASP files and re-writing static SQL. The SQL server also checks your syntax and will not let you write an invalid query. You do not have to keep hitting the refresh button in the browser until everything checks out.

So that's an overview of T-SQL. Any serious database programmer should be learning how to use it. Especially for large complex applications. Look for specific T-SQL examples soon.

Rob Taylor posts in the forums under the handle taylo. He is a consultant specializing in ASP and SQL Server. You can reach him at TConsult.

Related Articles

SQL Server 2008: Table Valued Parameters (24 July 2008)

SQL Server 2005: Using OVER() with Aggregate Functions (21 May 2007)

SQLChess - A tutorial on thinking in sets (14 May 2007)

How to search for date and time values (2 May 2003)

SQL In Simple English (18 March 2002)

DevGuru Transact-SQL Syntax Charts (9 December 2001)

ANSI SQL "School" (19 March 2001)

SQL Interpreter and Tutorial (10 December 2000)

Other Recent Forum Posts

Need Help in SQL query optimization (2d)

Info needed on field "user_scans" of table "sys.dm_db_index_usage_stats" (3d)

Suspect Parameter Sniffing? (3d)

See values of a proc when it is called from another proc? (4d)

Varchar() datatype and C# SqlDataReader truncates string (4d)

Find the extra rows (4d)

How to represent birth and death record in mysql db without duplicating data of an existing persons table existing persons table (5d)

EPR updates issues (5d)

- Advertisement -