Content by Category
.NET 1.x
.NET 2.0
.NET 3.0
.NET 3.5
.NET 4.0
.NET Assemblies
.NET Framework
.NET Getting Started
Accessibility
ADO.NET
Advertorials
Agile Development
AJAX
Architecture
ASP.NET
ASP.NET MVC
ASP.NET WebForms
Azure
B2B (Business Integration)
Bing
BizTalk
Book Excerpts
Build and Deploy
C#
C++
ClickOnce
Cloud Computing
Code Contracts
CODE on the Road!
COM+
Community
Conferences
Continuous Integration
Crystal Reports
CSLA.NET
CSS
Data
Design Patterns
Development Process
Display Technologies
Distributed Computing
DotNetNuke
DSL
Dynamic Programming
Editorials
Enterprise Services ("COM+")
Entity Framework
Events
Expression Blend
F#
Fox to Fox
Frameworks
Functional Programming
Git
Graphics
Internet Explorer 8.0
Interviews
iPhone
Iron Ruby
Java
Java Script
jQuery
LINQ
Linux
Mac OS X
MDX
Microsoft Application Blocks
Microsoft Business Rules Framework
Microsoft Dynamics
Microsoft Expression
Microsoft Office
Mobile Development
Mobile PC
Mono
MsBuild
Network
NHibernate
Object Oriented Development
Open Source
Opinion
Opinions
Oracle
ORM
Other Languages
Parallel Programming
Patterns
Podcasts
Post Mortem
PowerPoint
Print/Output
Product News
Product Reviews
Project Management
Python
Q&A
Rails
Rake
Reporting Services
REST
RIA Services
Ruby
Ruby on Rails
Search
Security
Services
SharePoint
Silverlight
SOA
Social Networks
Software & Law
Software Business
Source Control
Speech-Enabled Applications
SQL Server
SQL Server 2000
SQL Server 2005
SQL Server 2008
SQL Server CE/AnyWhere/Mobile/Compact
Subversion
Sync Framework
Tablet PC
TDD
Team System
Techniques
Testing and Quality Control
Tips
UI Design
UML
User Groups
VB Script
VB.NET
Version Control
VFP and .NET
VFP and SQL Server
Virtual Earth
Vista
Visual Basic
Visual Basic 6 (and older)
Visual FoxPro
Visual Studio .NET
Visual Studio 2005
Visual Studio 2008
Visual Studio 2010
Visual Studio Tools for Office
VSX
WCF
Web Development (general)
Web Services
WF
Whitepapers
Windows 7
Windows Azure
Windows Live
Windows Server
Windows Vista
WinForms
Workflow
WPF
XAML
XML
XNA
XSLT



DevConnections


 


DevReach

Reader rating:
Click here to read 14 comments about this article.
Article source: CoDe (2005 - Mar/Apr)


Article Pages:  1  2 3 4 5 - Next >


The Baker's Dozen: 13 Productivity Tips for Database Development Using Transact-SQL

Many application developers face the challenges of working with SQL Server 2000. These challenges include retrieving and grouping large amounts of data, building result sets, and tracking changes to data. All require professional strategies that hold water against a seemingly endless number of possibilities. This installment of "The Baker's Dozen" presents a variety of real-world database situations and how you can use Transact-SQL and SQL-92 to tackle these issues.

Beginning with the End in Mind

A major objective of "The Baker's Dozen" is to provide tips for those wishing to become more productive in a certain technology. Hopefully, even those with experience may still find a gold nugget in one of the tips. Just like the legendary Van Amsterdam Bakery that gave us the original Baker's Dozen name, I've spent the last few months baking a batch of tasty tips to serve up to developers, in hopes of helping you meet the challenges of SQL Server 2000.

"
Developers of similar strength and experience can and will disagree on the emphasis of performance (or the promise of performance) versus maintenance and readability. Sometimes there is no single correct answer. The key is to apply consistency in approaching these types of situations.
"

Most of the tips raise a business application requirement, and address how you can use Transact-SQL code to solve it. The tips in this issue are:

  • Return multiple result sets from a single stored procedure
  • Write User-Defined-Functions (UDFs) to return a scalar value
  • Write a UDF to parse a comma-separated list of selection keys and return a table that can be used for subsequent JOINs
  • Use LIKE to build text-search routines
  • Understand and use CASE syntax to evaluate run-time conditions
  • Baker's Dozen Spotlight: Use subqueries and derived tables to handle involved requirements, and a brief discussion about different techniques
  • Use table variables, and understand the differences between table variables and temporary tables
  • Use datepart functions to summarize daily data by a week-ending date
  • Learn how to use functions like CAST and CONVERT to make different data types work together
  • Use triggers to define specific actions when database row(s) are modified
  • Extend triggers to implement basic audit trail capability
  • Use Dynamic SQL to build queries where key conditions are not known until runtime
  • Retrieve a list of tables and columns from a particular database

You can find all listings in this article on the Common Ground Solutions Web site at www.commongroundsolutions.net. So once again, borrowing from the theme of Van Amsterdam's bakery, here are thirteen productivity tips, a "Baker's Dozen".

Tip 1: Return Multiple Result Sets

Requirement: You need to create a simple stored procedure to return all the Order Information for a single customer. The result set must contain three tables: the order header, order detail, and order notes.

A single SQL 2000 stored procedure can easily return multiple result sets. The general rule is that any SQL SELECT statement in a stored procedure with no explicit output declaration (to a table, variable, etc.) returns the results to the calling routine.

Listing 1 shows a basic stored procedure that contains one argument, an integer key for a customer account. The procedure queries the Order Header, Detail, and Notes tables. Note that the procedure creates a variable of type table, and stores the list of order keys to be used for all three queries. Tip #7 covers table variables in more detail.

Because the three queries do not contain an output destination, the results are returned to the calling procedure. You can execute the stored procedure from a script in Query Analyzer, which displays the three results sets when the query runs. If you called the stored procedure from C#, the data adapter fills the result set (dataset) with the three data tables, like so:

SqlDataAdapter oDa = 
new SqlDataAdapter(cSQLString, oMyConn);
oDa.Fill(DsReturn,"MyResults");
// Table 0 will contain the order headers
// Table 1 will contain the order details
// Table 2 will contain the order notes

Normally, you would use some type of data access methodology to call stored procedures. A future Baker's Dozen article will discuss these types of development strategies in a distributed computing environment.

&

By: Kevin S Goff

Kevin S. Goff, a Microsoft MVP award recipient for 2007, is the founder and principal consultant of Common Ground Solutions, a consulting group that provides custom Web and desktop software solutions in .NET, VFP, SQL Server, and Crystal Reports. Kevin is the author of Pro VS 2005 Reporting using SQL Server and Crystal Reports, published by Apress. Kevin has been building software applications since 1988. He has received several awards from the U.S. Department of Agriculture for systems automation. He has also received special citations from Fortune 500 Companies for solutions that yielded six-figure returns on investment. He has worked in such industries as insurance, accounting, public health, real estate, publishing, advertising, manufacturing, finance, consumer packaged goods, and trade promotion. In addition, Kevin provides many forms of custom training. Contact Kevin at kgoff@commongroundsolutions.net

kgoff@commongroundsolutions.net

Fast Facts

A sampling of Transact SQL tips for new and veteran developers.



Listing 1: A simple stored procedure to return multiple result sets

CREATE PROCEDURE GetOrdersByAcct (@iAcctKey integer) AS

-- Retrieve Orders for a single customer

   -- Since we're querying the order table three times,
   -- let's create a table variable (@tOrderList) of all
-- order keys for the specific customer. That way, we
-- can JOIN off the temporary order list 

DECLARE @tOrderList TABLE (OrderKey integer)
   INSERT INTO @tOrderList 
      SELECT OrderKey FROM OrderHdr 
         WHERE AcctKey = @iAcctKey

   SELECT OH.*, AM.AcctName 
      FROM OrderHdr OH
      JOIN AcctMast    AM ON AM.AcctKey = OH.AcctKey
      JOIN @tOrderList TMP ON TMP.OrderKey = OH.OrderKey
      
   SELECT OD.*, PM.ProdName
      FROM OrderDtl OD
      JOIN ProdMast PM ON PM.ProdKey = OD.ProdKey
      JOIN @tOrderList TMP ON TMP.OrderKey = OD.OrderKey
      
   SELECT * FROM OrderNotes ON
      JOIN @tOrderList TMP ON TMP.OrderKey = ON.OrderKey

GO



Article Pages:  1  2 3 4 5 - Next Page: 'Tip 2: UDFs to Return a Value' >>

Page 1: The Baker's Dozen: 13 Productivity Tips for Database Development Using Transact-SQL
Page 2: Tip 2: UDFs to Return a Value
Page 3: Tip 4: Use LIKE to Perform Test Searches
Page 4: Tip 8: Date Functions
Page 5: Tip 12: Dynamic SQL
Page 6:

How would you rate the quality of this article?
1 2 3 4 5
Poor      Outstanding

Tell us why you rated the content this way. (optional)

Average rating:
4.8 out of 5

23 people have rated this article.

      DevConnections

 

DevLink