SQL Server Performance

  • Home
  • Articles
  • Forums
  • Tips
  • Training
  • FAQ's
  • Blogs
  • Software
  • Books
  • About Us
RSS Feeds
Sign in | Join


Article Topics

All Articles
Performance Tuning
Audit
Business Intelligence
Clustering
Reporting Services
SQL Azure
Developer
General DBA
ASP.NET / ADO.NET
SQL Azure

USEFUL SITES :

ASP.NET Tutorials
Windows and SQL Azure Tutorials
Cloud Hosting Magazine
SharePoint Tutorials
Windows Server Help

Write for Us

Share your SQL Server knowledge with others and raise your profile in the community More...
Latest Articles

A High Level Comparison Between Oracle and SQL Server - Part ...
A High Level Comparison Between Oracle and SQL Server - Part ...
A High Level Comparison Between Oracle and SQL Server - Part ...
A High Level Comparison Between Oracle and SQL Server

More     
 
Latest FAQ's

Add Node to A SQL Server failover Cluster failed with invalid ...
SQL Server Destination remote server error
Setting Up Data And Log Files For SQL Server
Will Check Constraints Improve Database Performance?

More     
   
Latest Software Reviews

dbForge Review
Spotlight on ApexSQL Diff - Server-based database comparison tool ...
Spotlight on ApexSQL Data Diff - Server-based database comparison tool ...
Spotlight on ApexSQL Doc 2008

More     

articles >> developer >> SQL Server Techniques for Creating a Web ...

SQL Server Techniques for Creating a Web Reporting Page -- Part 2

By : Louis Duc Nguyen
Jun 22, 2003

In this article, we build upon our stored procedure and will demonstrate how to implement SORT and TOTAL row functionality.

SORT

Our sales manager is happy with our previous work. Now he requests the ability to sort the results, ascending and descending. In the web page, we provide an input select box where he can select which column to sort on. We also provide an input select box where he can select ascending or descending. If he selects descending, we pass “desc”. If he selects ascending, we pass nothing as ascending is the SQL default. We concatenate the values and pass it to a new parameter @orderby. The possible values for @orderby are “region”, “region desc”, “empid”, “empid desc”, “sumSales”, and “sumSales desc”. We alter the stored procedure. We add the @orderby parameter and an ORDER BY with CASE and PATINDEX. I chose this somewhat lengthy statement, as it is standard SQL. The alternative is to use dynamic SQL. (For brevity I have deleted the “sumSales”code block).

 

<Our Reporting Stored Procedure>

ALTER PROCEDURE CubeSP

(

@method varchar(50)='groupSales'

,@region varchar(50)='all'

,@startdate datetime='1900/1/1'

,@enddate datetime='2100/1/1'

,@empid varchar(50)='all'

,@group varchar(5000)=null

,@orderby varchar(5000)='region,empid'

)

AS

SET NOCOUNT ON

SET ANSI_WARNINGS OFF

SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED

select @region=case when @region='all' then '%' else @region end

select @empid=case when @empid='all' then '%' else @empid end

 

if @method='groupSales' begin

select region,empid,sumSales=sum(N)

from

(

select

region=case when patindex('%region%',@group)>0 then region else '' end

,empid=case when patindex('%empid%',@group)>0 then empid else 0 end

,n

from cube

where [date] between @startdate and @enddate

and region like @region

and empid like @empid

) as a

group by region,empid

order by

case when patindex('%desc%',@orderby)>0 and patindex('%region%',@orderby)>0 then region end desc

,case when patindex('%region%',@orderby)>0 then region end

,case when patindex('%desc%',@orderby)>0 and patindex('%empid%',@orderby)>0 then empid end desc

,case when patindex('%empid%',@orderby)>0 then empid end

,case when patindex('%desc%',@orderby)>0 and patindex('%sumSales%',@orderby)>0 then sum(N) end desc

,case when patindex('%sumSales%',@orderby)>0 then sum(N) end

return

end

</Our Reporting Stored Procedure>

 

HOW IT WORKS

The syntax for order by is: ORDER BY [EXPRESSION1] [ASC|DESC], [EXPRESSION2] [ASC|DESC],

… [EXPRESSIONn] [ASC|DESC]. For example if the output of expression1 is “region” – SQL will sort the results by region. The output of an expression must be a column name. The output of expression1 cannot be “region desc”. Hence we have to write a case statement for descending and a case statement for ascending. Let’s break the following statement down: case when patindex('%desc%',@orderby)>0 and patindex('%region%',@orderby)>0 then region end desc. If the words “desc” and “region” are found in the @orderby parameter then output “region desc” else output “[void] desc”. When @orderby=”region”, the order by statement is translated into:  ORDER BY [void] desc, region, [void] desc, [void], [void] desc, [void].

EXAMPLE OUTPUT:

exec cubesp @group='region,empid',@orderby='sumSales desc'

REGION EMPID sumSales      

South 12 44

West 32 36

West 31 24

North 1 18

North 2 6

East 21 4

West 33 4

South 13 2


    Next Page>>    








C# Help and Tutorials | PHP MySQL Tutorial | Sharepoint Tutorial | Azure Tutorial | Cloud Hosting Magazine | ASP.NET Tutorials | Windows Server Help | Windows Phone Pro | Silverlight Ace | Visual Studio Tutorials | Home | Peformance Articles | Audit Articles | Business Intelligence Articles | Clustering Articles | Developer Articles | Reporting Services Articles | DBA Articles | ASP.NET / ADO.NET Articles | SQL Server Training Videos | DBA FAQ's | Developer Peformance FAQ's | DBA Peformance FAQ's | Developer FAQ's | Clustering FAQ's | Error Messages | Audit Tool Reviews | Backup Tool Reviews | Coding Tool Reviews | Compare Tool Reviews | Documentation Tool Reviews | Design Tool Reviews | Monitoring Tool Reviews | Log Tool Reviews | Reporting Tool Reviews | Clustering Tool Reviews | Security Tool Reviews | Change Management Tool Reviews | Remote Access Tool Reviews | Book Reviews | Security Tool Reviews | ADO.NET / ASP.NET | Administration | Analysis/OLAP Services | Application Development | Configuration | Components | ETL | Hardware | High Availability | Hints | Index | Misc | Operating Systems | Performance Tuning | Replication | T-SQL | Views


              © 2010 Jude O'Kelly. All rights reserved