Monday, October 15, 2012

Creating YTD Calulation using MDX

Ytd function returns a set of sibling members from the same level as a given member, starting with the first sibling and ending with the given member, as constrained by the Year level in the Time dimension.
And
PeriodsToDate function returns a set of sibling members from the same level as a given member, starting with the first sibling and ending with the given member, as constrained by a specified level in the Time dimension.
One of these function can be used for YTD calculation in MDX queries.
Prior to use of these funtion, please check your Time Dimesion as below:
1. Time dimension attributes should have properites as shown below:
2. Time dimension should have hierarchies specified.
3. Time dimension should also have attribute relationships specified.
Now, you can add the calculated members within the cube to get the Year-to-date values, as below:

New Calulated Member
Name : [YTD Amount]
Parent hierarchy: Measures
Expression: SUM(PERIODSTODATE([Dim Time].[Fiscal].[Fiscal Year], [Dim Time].[Fiscal].CURRENTMEMBER),[Measures].[Amount])

Thursday, September 6, 2012

Windows Management Instrumentation (WMI)

Microsoft Systems Management Server (SMS) 2003 is a Windows-based product that you can use to manage, support, and maintain a distributed network of computer resources. SMS enhances its abilities by using Windows Management Instrumentation (WMI), which is management infrastructure that supports monitoring and controlling system resources through a common set of interfaces.
WMI is a middle-layer technology that enables standardized management of Windows-based computers. It collects computer management data from a wide variety of sources and makes it accessible by using standard interfaces. WMI can be accessed remotely, but it does not consolidate the management data in a central location - that is one of the functions of SMS. You can also use WMI to set configuration details on your computer and to detect and respond to changes in the configuration of your computer (using WMI events).
You can manage WMI by using the following tools:
  • WMI Control
  • MOF Compiler (MOFComp.exe)
  • WinMgmt.exe
MOF Files:
The Managed Object Format (MOF) file describes Common Information Model (CIM) classes; defines textual descriptions of classes that are used by a provider to return data; commonly used to change WMI settings and to transfer them between computers.
MOF files are programmed in a language derived from the Microsoft Visual C++ syntax. The contents of MOF files only become effective when the files are compiled. They can can be compiled into the WMI repository using the program Mofcomp.exe. They typically are bundled with a corresponding .DLL file, which contains the skeleton code that returns the data defined in the MOF file.
The MOF file format and compiler were developed by the Desktop Management Task Force (DMTF).

MOF Compiler (MOFComp.exe):
MOF files are usually automatically compiled during the installation of the systems with which they are provided, but you can also compile MOF files by using the MOF Compiler (Mofcomp.exe). The MOF Compiler is available in the %Windir%\System32\wbem directory. You must specify the MOF file as the parameter of the MOF Compiler. The MOF compiler parses a file containing MOF statements and adds the classes and class instances defined in the file to the WMI repository. You can also specify an Autorecover switch if you want the MOF file to be automatically recompiled if the CIM Repository ever has to be automatically recovered.

Monday, August 27, 2012

.NET : Partial Methods

A partial class or struct may contain a partial method. One part of the class contains the signature of the method. An optional implementation may be defined in the same part or another part. If the implementation is not supplied, then the method and all calls to the method are removed at compile time.
Partial methods enable the implementer of one part of a class to define a method, similar to an event. The implementer of the other part of the class can decide whether to implement the method or not. If the method is not implemented, then the compiler removes the method signature and all calls to the method. The calls to the method, including any results that would occur from evaluation of arguments in the calls, have no effect at run time. Therefore, any code in the partial class can freely use a partial method, even if the implementation is not supplied. No compile-time or run-time errors will result if the method is called but not implemented.
A partial method declaration consists of two parts: the definition, and the implementation. These may be in separate parts of a partial class, or in the same part. If there is no implementation declaration, then the compiler optimizes away both the defining declaration and all calls to the method.
Example:
// Definition in file1.cs
partial void onNameChanged();

// Implementation in file2.cs
partial void onNameChanged()
{
  // method body
}
The Rules:
  • Partial method declarations must begin with the contextual keyword partial and the method must return void.
  • Partial methods can have ref but not out parameters.
  • Partial methods are implicitly private, and therefore they cannot be virtual.
  • Partial methods cannot be extern, because the presence of the body determines whether they are defining or implementing.
  • Partial methods can have static and unsafe modifiers.
  • Partial methods can be generic. Constraints are put on the defining partial method declaration, and may optionally be repeated on the implementing one. Parameter and type parameter names do not have to be the same in the implementing declaration as in the defining one.
  • You can make a delegate to a partial method that has been defined and implemented, but not to a partial method that has only been defined.
Source : http://msdn.microsoft.com/en-us/library/wa80x488.aspx

.NET : Extension Method

An Extension method is a new language feature of C# starting with the 3.0 specification, as well as Visual Basic.NET starting with 9.0. Extension methods enable you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. An extension method is a static method of a static class that you can call as though it were an instance method of a different class. 
For example, you could create an extension method named ToDouble that is a static method in a static class you create named StringConversions, but that is called as though it were a method of an object of type string.
Extension Method Declarations and Invocations:
Specifying a method’s first argument with the this keyword modifier will make that method an extension method. The extension method will appear as an instance method of any object with the same type as the extension method’s first argument’s data type. For example, if the extension method’s first argument is of type string, the extension method will appear as a string instance method and can be called on any string object. Also, extension methods can only be declared in static classes.
Example of an extension method:
namespace Netsplore.Utilities
{
    public static class StringConversions
    {
        public static doubleToDouble(this string s)
        {
            return Double.Parse(s);
        }
        public static boolToBool(this string s)
        {
            return Boolean.Parse(s);
        }
    }
}
Here both the class and every method it contains are static. ToDouble is an extension method, because method is static and its first argument specifies the this keyword.
Calling an extension method:
    usingNetsplore.Utilities;
    double pi = "3.1415926535".ToDouble();
    Console.WriteLine(pi);
This produces the following results:
    3.1415926535
Extension Method Precedence:
Normal object instance methods take precedence over extension methods when their signature matches the calling signature. Extension methods seem like a really useful concept, especially when you want to be able to extend a class you cannot, such as a sealed class or one for which you do not have source code. The previous extension method examples all effectively add methods to the string class. Without extension methods, you couldn’t do that because the string class is sealed.

Thursday, August 23, 2012

T-SQL Syntax

Recently I have came across very good question on SQLServerCentral.com
Different T-SQL constructs can assign a value to a regular identifier with a leading '@' without using SET nor SELECT :
  1. An Input argument to a Procedure or Function - http://msdn.microsoft.com/en-us/library/ms187926
  2. An Output argument to a Procedure - http://msdn.microsoft.com/en-us/library/ms187926
  3. EXECUTE a Function: EXEC @return = udfFunct() - http://msdn.microsoft.com/en-us/library/ms188332
  4. The Stored Procedure return status: EXEC @status = uspProc (This is very different from a Function return.) - http://msdn.microsoft.com/en-us/library/ms188332
  5. The OUTPUT clause: OUTPUT INTO @tablevar (Could count this 4 times but it is really one construct) http://msdn.microsoft.com/en-us/library/ms177564
  6. RECEIVE .... FROM INTO @tablevar - http://msdn.microsoft.com/en-us/library/ms186963.aspx
  7. FETCH NEXT FROM cursor INTO @varname - http://msdn.microsoft.com/en-us/library/ms180152
  8. DECLARE @varname INT = 0; - http://msdn.microsoft.com/en-us/library/ms188927

Wednesday, August 8, 2012

WCF Endpoints : Addresses

All communication with a Windows Communication Foundation (WCF) service occurs through the endpoints of the service. Endpoints provide clients access to the functionality offered by a WCF service.
Each endpoint consists of four properties:
  • An address that indicates where the endpoint can be found.
  • A binding that specifies how a client can communicate with the endpoint.
  • A contract that identifies the operations available.
  • A set of behaviours that specify local implementation details of the endpoint.
Addresses:
In WCF, every service is associated with a unique address. The address provides two important elements: the location of the service and the transport protocol, or transport scheme, used to communicate with the service. The location portion of the address indicates the name of the target machine, site, or network; a communication port, pipe, or queue; and an optional specific path, or URI (Universal Resource Identifier). A URI can be any unique string, such as the service name or a globally unique identifier (GUID).
WCF supports the following transport schemes:
  • HTTP/HTTPS
  • TCP
  • IPC
  • Peer network
  • MSMQ
  • Service bus
Addresses always have the following format:
    [base address]/[optional URI]
The base address is always in this format:
    [transport]://[machine or domain][:optional port]
Here are a few sample addresses:
    http://localhost:8001
    http://localhost:8001/MyService
    net.tcp://localhost:8002/MyService
    net.pipe://localhost/MyPipe
    net.msmq://localhost/private/MyQueue
    net.msmq://localhost/MyQueue


TCP Addresses
TCP addresses use net.tcp for transport and typically include a port number, as in:
    net.tcp://localhost:8002/MyService
When a port number is not specified, the TCP address defaults to port 808:
    net.tcp://localhost/MyService
It is possible for two TCP addresses (from the same host) to share a port:
    net.tcp://localhost:8002/MyService
    net.tcp://localhost:8002/MyOtherService
You can configure TCP-based addresses from different service hosts to share a port.

HTTP Addresses
HTTP addresses use http for transport and can also use https for secure transport. You typically use HTTP addresses with outward-facing Internet-based services, and you can specify a port as shown here:
    http://localhost:8001
If you do not specify the port number, it defaults to 80 (and port 443 for HTTPS). As with TCP addresses, two HTTP addresses from the same host can share a port, even on the same machine.

IPC Addresses
IPC (Inter-Process Communication) addresses use net.pipe for transport, to indicate the use of the Windows named pipe mechanism. In WCF, services that use IPC can only accept calls from the same machine. Consequently, you must specify either the explicit local machine name or localhost for the machine name, followed by a unique string for the pipe name:
    net.pipe://localhost/MyPipe
You can open a named pipe only once per machine, so it is not possible for two named pipe addresses to share a pipe name on the same machine.

MSMQ Addresses
MSMQ addresses use net.msmq for transport, to indicate the use of the Microsoft Message Queue (MSMQ). You must specify the queue name. When you’re dealing with private queues, you must also specify the queue type, but you can omit that for public queues:
    net.msmq://localhost/private/MyService
    net.msmq://localhost/MyService

Service Bus Addresses
Windows Azure AppFabric Service Bus addresses use sb, http, or https for transport, and must include the service bus address along with the service namespace, for example:
    sb://MyNamespace.servicebus.windows.net/

Monday, July 23, 2012

#temp table Vs @table variable

Some facts about Table Variables and Temp Tables are:
  • You can create local and global temporary tables. Local temporary tables (#table_name) are visible only in the current session, and global temporary tables (##table_name) are visible to all sessions. 
  • Table Variables and Temp Tables both use the tempdb database.
  • Table variables are Transaction neutral. They are variables and thus aren't bound to a transaction.
  • Temp tables behave same as normal tables and are bound by transactions.
  • Assignment operation between table variables is not supported.
  • Clustered indexes can be created on both table variables and temporary tables.
  • Both are logged in the transaction log.
  • Temporary tables cannot be partitioned.
  • Table variables are only allowed in SQL Server 2000+, with compatibility level set to 80 or higher.
Difference:
#temp table @table variable
You can truncate a temp table. You cannot truncate a table variable.
You can alter temp table. Table variables cannot be altered after they have been declared.
One of the most valuable assets of a temp table is the ability to add either a clustered or non-clustered index. You cannot explicitly add an index to a table variable, however you can create a system index through a PRIMARY KEY CONSTRAINT, and you can add as many indexes via UNIQUE CONSTRAINTs as you like.
Temp tables can be used in the following situations:
INSERT #temp EXECsp_someProcedure
SELECT * INTO #temp FROM someTable
You cannot use a table variable in either of the following situations:
INSERT @table EXECsp_someProcedure
SELECT * INTO @table FROM someTable
You cannot create temp tables inside user-defined function. You can declare table variable inside user-defined function.
Temp tables allow for the auto-generated statistics to be created against them. The system will not generate automatic statistics on table variables. Likewise, you cannot manually create statistics
Statistics generated on temp table, help the optimizer to determine cardinality. A table variable will always have a cardinality of 1, because the table doesn'texist at compile time.
Temp tables are automatically dropped when they go out of scope, unless explicitly dropped by using DROP TABLE. You cannot drop a table variable when it is no longer necessary. They are cleaned up automatically at the end of the function, stored procedure, or batch in which they are defined.
Temp table can be referenced by its name or by an alias, except in the FROM clause.
e.g.: SELECT id FROM #t1 t INNER JOIN #t2 ON t.id = #t2.id
Table variables must be referenced by an alias, except in the FROM clause.
e.g.: SELECT id FROM @foo f INNER JOIN #foo ON f.id = #foo.id
Temp tables are visible to the calling procedure in the case of nested procs. Table variables are not visible to the calling procedure in the case of nested procs.
Use of temp table in stored procedure can cause stored procedure recompilations. Table variables used in stored procedures result in fewer recompilations of the stored procedures than when temporary tables are used
Temp tables are involved in SQL transactions. Transactions involving table variables last only for the duration of an update on the table variable. Thus, table variables require less locking and logging resources.
Temp tables are preferred when cost-based choices are required. This typically includes queries with joins, parallelism decisions, and index selection choices. Table variables are not supported in the SQL Server optimizer's cost-based reasoning model. Therefore, they should not be used when cost-based choices are required to achieve an efficient query plan.
Only static tables and temporary tables support the statement "SET IDENTITY_INSERT ON".The table variable does not support the statement "SET IDENTITY_INSERT ON".