Presentation is loading. Please wait.

Presentation is loading. Please wait.

Build on-the-fly reporting with Dynamic SQL

Similar presentations


Presentation on theme: "Build on-the-fly reporting with Dynamic SQL"— Presentation transcript:

1 Build on-the-fly reporting with Dynamic SQL

2 Thank you to our sponsors!

3 Dorothy Ling <dorothy. w. ling@gmail
Dorothy Ling Midco Senior Software Developer Business Intelligence and .NET Development * MS SQL (table, view, stored procedures) * SSAS, SSIS, SSRS * .NET (webform, MVC) * research MCAD (.NET), MCP (SQL)

4 Topics Pivoting and Un-pivoting Data Window Functions User-defined Functions Cursor Dynamic SQL Putting It All Together

5 Pivot Turn rows into columns Un-pivot Turn columns into rows

6 Pivot. Select <select list>. From <data source>
Pivot Select <select list> From <data source> Pivot (<aggregate function>(<aggregate column>) For <spreading column> In (<distinct spreading values>) ) as p * Notice that you indicate the aggregation and spreading elements but not the grouping element, which is identified by elimination.

7 Un-pivot Select <column list>,<names column>,<values column> From <data source> Unpivot (<values column> For <name column> In (<source columns> ) ) as u * The datatype of all value columns that you are unpivoting must be the same.

8 Window Functions. <function> Over (. <Partition by>
Window Functions <function> Over ( <Partition by> <Order by> <Row or Range>) * Aggregate: Sum(), Count(), Avg(), Min(), Max()… * Ranking Row_Number(), Rank(), Tile()… * Analystic Lag(), Lead(), Percent_Rank()…

9 User-defined Functions. encapsulate reusable T-SQL code and return a
User-defined Functions * encapsulate reusable T-SQL code and return a scalar value or a table to the caller * embedded in T-SQL statements * execute as part of a T-SQL command * cannot perform any data definition language (DDL) such as Create Table * cannot make modifications to tables, indexes… * cannot change any data in permanent tables

10 Scalar-valued Function
Scalar-valued Function Returns a single data value Create Function <function name> ( <parameter list> ) Returns <datatype> As Begin Return <value in declared datatype> End

11 Table-valued Function
Table-valued Function Returns a table data type Create Function <function name> ( <parameter list> ) Returns Table as Return ( Select <select list> From <data source> )

12 Multi-statement Table-valued Function Create Function <function name> ( <parameter list> ) Returns <table variable> Table ( <column definitions> ) As Begin <insert into table variable> Return End

13 Cursor Iterations for operations that must be done per row Impact performance so only use when cannot be done using set-based solution

14 DECLARE <variable> AS INT; DECLARE <cursor> CURSOR FAST_FORWARD FOR SELECT <select value> FROM <data source>; OPEN <cursor>; FETCH NEXT FROM <cursor> INTO <variable>; WHILE = 0 BEGIN < do whatever you need to do with the variable > FETCH NEXT FROM <cursor> INTO <variable>; END; CLOSE <cursor>; DEALLOCATE <cursor>;

15 Dynamic SQL Used where variables cannot be substituted for literals in T-SQL code varchar(8000) = ‘Select…’

16 Putting It All Together

17 Thank You!


Download ppt "Build on-the-fly reporting with Dynamic SQL"

Similar presentations


Ads by Google