Wednesday, September 17, 2008

Difference between standard subscription and data-driven subscriptions

Standard Subscriptions

  1. Standard Subscriptions are available in Sql Server Standard version.
  2. Report users can subscribe for the subscriptions.
  3. The subscription is static in the sense that the list of subscribers is not derived from a query or some Active Directory search. The subscribers request(subscribe) for the report delivery or get an information that the report has run and is ready for viewing.

Data-Driven Subscriptions

  1. Data-Driven subscriptions are available in Enterprise Edition of Sql Server
  2. The DBAs/SSRS administrators are the ones who configure the subscriptions. The reports can be subscribed with different parameters.
  3. The subscribers list can be dynamic and can be the result of a query.

What is the life time of a temporary table on Sql Server

Temporary Tables

You can create local and global temporary tables. Local temporary tables are visible only in the current session; global temporary tables are visible to all sessions.

Prefix local temporary table names with single number sign (#table_name), and prefix global temporary table names with a double number sign (##table_name).

SQL statements reference the temporary table using the value specified for table_name in the CREATE TABLE statement:

CREATE TABLE #MyTempTable (cola INT PRIMARY KEY)

INSERT INTO #MyTempTable VALUES (1)

If a local temporary table is created in a stored procedure or application that can be executed at the same time by several users, SQL Server has to be able to distinguish the tables created by the different users. SQL Server does this by internally appending a numeric suffix to each local temporary table name. The full name of a temporary table as stored in the sysobjects table in tempdb consists of table name specified in the CREATE TABLE statement and the system-generated numeric suffix. To allow for the suffix, table_name specified for a local temporary name cannot exceed 116 characters.

Temporary tables are automatically dropped when they go out of scope, unless they have already been explicitly dropped using DROP TABLE:

  • A local temporary table created in a stored procedure is dropped automatically when the stored procedure completes. The table can be referenced by any nested stored procedures executed by the stored procedure that created the table. The table cannot be referenced by the process which called the stored procedure that created the table.
  • All other local temporary tables are dropped automatically at the end of the current session.
  • Global temporary tables are automatically dropped when the session that created the table ends and all other tasks have stopped referencing them. The association between a task and a table is maintained only for the life of a single Transact-SQL statement. This means that a global temporary table is dropped at the completion of the last Transact-SQL statement that was actively referencing the table when the creating session ended.

A local temporary table created within a stored procedure or trigger is distinct from a temporary table with the same name created before the stored procedure or trigger is called. All references to the name within the stored procedure or trigger are resolved against the temporary table created in the procedure or trigger, not the version that existed before the procedure or trigger was called. Nested stored procedures can also create temporary tables with the same name as a temporary table created by the stored procedure that called it. All references to the table name in the nested stored procedure are resolved to the table created in the nested procedure, for example:

CREATE PROCEDURE Test2

AS

CREATE TABLE #t(x INT PRIMARY KEY)

INSERT INTO #t VALUES (2)

SELECT Test2Col = x FROM #t

GO

CREATE PROCEDURE Test1

AS

CREATE TABLE #t(x INT PRIMARY KEY)

INSERT INTO #t VALUES (1)

SELECT Test1Col = x FROM #t

EXEC Test2

GO

CREATE TABLE #t(x INT PRIMARY KEY)

INSERT INTO #t VALUES (99)

GO

EXEC Test1

GO

The results of running this script are:

(1 row(s) affected)

Test1Col

-----------

1

(1 row(s) affected)

Test2Col

-----------

2


Wednesday, June 18, 2008

SWITCH function in SSRS - Equivalent of CASE statement

SQL Reporting Services Switch Statement

SSRS has a switch function, which mirrors the functionality of a Case statement, and can really save you some time.

Most common use for me are setting colors for parts of my report based on the value of a field.

For example, if we have a field: Orders (Fields!Orders.Value) which carries an integer representing the number of orders this week, we may want to set the background color of our report text box based on the order performance.

=Switch(Fields!Orders.Value < class="str">"Red", Fields!Orders.Value < class="str">"Green")

In the above example, if orders are < style="color: rgb(255, 0, 0);">Red, if they are 100 to 499, we return Green.


How does this work?


The switch statement takes an expression, followed by the return value if that expression evaluated to true. Additionally, it will always take the first expression that evaluated to true, that is why it's OK that a value of 40 (for example) would satisfy both cases. 40 will always give us red, becuase it's the first expression satisfied.


If you want a 'default return value', just take advantage of the fact that the first true expression is evaluated, and modify your code like this:

=Switch(Fields!Orders.Value < class="str">"Red", Fields!Orders.Value < class="str">"Green", 1=1, "Black")

Now, if our value doesn't satisfy the other expressions, we know the last expression will evaluate to true (1 always equals 1), hence "Black" is our default return value.

Tuesday, June 17, 2008

Useful Built-in functions of SSRS

The following functions are built-in to the reporting engine processor. If you don't see a function that provides the capability you want then you can add additional by creating or using your own user functions via the code defined in the report or the defined external classes.

Aggregate(field expr [,scope])
Returns an array containing the values of the grouped field. For example, =Code.AggrToString(Aggregate(Fields!Year.Value)) // In Code element Public Function AggrToString(o as object) As String Dim ar as System.Collections.ArrayList = o Dim sb as System.Text.StringBuilder = New System.Text.StringBuilder Dim n as Integer For n = 0 To ar.Count-1 sb.Append(ar(n)) If n <>

Asc(string)

Converts the first letter in the passed string to ANSI code.
Avg(field expr [,scope])
Returns the average value of the grouped field. Returns decimal if the argument type is decimal, otherwise double.

CBool(object)
Converts the passed argument to Boolean.

CByte(string)
Converts the passed argument to Byte.

CCur(string)

Converts the argument to type Currency (really Decimal).

Choose(number, expr1, expr2, ... exprn

Evaluates the number and return the result of the coorespodning exprn. For example, if number results in 3 then expr3 is returned.

CDate(string)

Converts a string to type DateTime

CDbl(object)

Converts the passed parameter to double.

Chr(int)

Converts the specified ANSI code to a character.

CInt(object)

Converts the argument to integer.

CLng(object)

Converts the argument to long.

Count(field expr [,scope])

Returns the number of values in the grouped field. Null values don't count.

Countrows([scope])

Returns the number of rows in the group.

Countdistinct(field expr [,scope])

Returns the number distinct values in the grouped field. Null values don't count.

CSng(object)

Converts the argument to Single.

CStr(object)

Converts the argument to String.

Day(datetime)

Returns the integer day of month given a date.

First(field expr [,scope])

Returns the first value in the group.

Format(string1 [,string2)

Format string1 using the format string2. Some valid formats include '#,##0', '$#,##0.00', 'MM/dd/yyyy', 'yyy-MM-dd HH:mm:ss'... string2 is a .NET Framework formatting string.

Hex(number)

Returns the hexadecimal value of a passed number.

Hour(datetime)

Returns the integer hour given a date/time variable.

Iif(bool-expr, expr2, expr3

The Iif function evaluates bool-expr and when true returns the result of expr2 otherwise the result of expr3. expr2 and expr3 must be the same data type.

InStr([ioffset,] string1, string2 [,icase])

1 based offset of string2 in string1. You can optionally pass an integer offset as the first argument. You can also optionally pass a 1 as the last argument if you want the search to be case insensitive.

InStrRev(string1, string2[,offset[,case]])

1 based offset of string2 (second argument) in string1 (first argument) starting from the end of string1. You can optionally pass an integer offset as the third argument. You can also optionally pass a 1 as the fourth argument if you want the search to be case insensitive.

Last(field expr [,scope])

Returns the last value in the group.

LCase(string)

Returns the lower case of the passed string.

Left(string)

Returns the left n characters from the string.

Len(string)

Returns the lenght of the string.

LTrim(string)

Removes leading blanks from the passed string.

Max(field expr [,scope])

Returns the maximum value in the group.

Mid

Returns the portion of the string (arg 1) denoted by the start (arg 2) and length (arg 3).

Min(field expr [,scope])

Returns the minimum value in the group.

Minute(datetime)

Returns the integer minute given a date/time variable.

Month(datetime)

Returns the integer month given a date.

MonthName(datetime)

Get the month name given a date. If the optional second argument is 'True' then the abbreviated month name will be returned.

Next(field expr [,scope])

Returns the value of the next row in the group.

Oct(number)

Returns the octal value of a specified number.

Previous(field expr [,scope])

Returns the value of the previous row in the group.

Replace

Returns a string replacing 'count' instances of the searched for text (optionally case insensitive) starting at position start with the replace text. The function form is Replace(string,find,replacewith[,start[,count[,compare]]]).

Right(string, number)

Returns a string of the rightmost characters of a string.

Rownumber()

Returns the row number.

RTrim(string)

Removes trailing blanks from string.

Runningvalue(field expr, string1 [,scope])

Returns the current running value of the specified aggregate function. string1 is an expression returning one of the following aggregate function: "sum", "avg", "count", "max", "min", "stdev", "stdevp", "var", "varp".

Second(datetime)

Returns the integer second given a date/time variable.

Space(number)

Returns a string containing the number of spaces requested.

Stdev(field expr [,scope])

Returns the standard deviation of the group.
Stdevp(field expr [,scope])
Returns the standard deviation of the group. Use stdevp instead of stdev when the group contains the entire population of values.

StrComp(string1, string2, compare)

Compares the strings; optionally with case insensitivity. When string1 < string1 =" string2:"> string2: 1

String(number, char)

Return string with the character repeated for the length.

StrReverse(string)

Returns a string with the characters reversed.

Sum(field expr [,scope])

Returns the total of the group.

Switch(bool-expr, result1 [, bool-expr-n, result-n])

The arguments are pairs of expression. When the bool-expr is true the result is returned. bool-expr-n is evaluated until one is results in true then the cooresponding result-n expression is returned.

Today()

Return the current date/time on the computer running the report.

Trim(string)

Removes whitespace from beginning and end of string.

UCase(string)

Returns the uppercase version of the string.
Var(field expr [,scope])
Returns the variance of the group.

Varp(field expr [,scope])

Returns the variance of the group. Use varp instead of var when the group contains the entire population of values.

Year(datetime)

Obtains the year from the passed date.

Weekday()

Returns the integer day of week: 1=Sunday, 2=Monday, ..., 7=Saturday given a date.

WeekdayName(iday [,abbr])

Returns the name of the day of week given the integer Weekday. The optional second argument will return the abbreviated day of week if 'True'.

Tuesday, June 3, 2008

How to Turn-Off Grand Totals on SSAS 2005


HighLight the Dimension that you want to eliminate the Grand Totals from...Right Click ---> Unselect "SubTotals"