Sunday 10 July 2016

Convert a stored procedure in to system object and access it from all other databases in SQL SERVER

In this post we are going to see how to convert a stored procedure in to system object and access it in all other databases in Sql server.

Why we need a system objects, because it will be accessible for all databases in there selected database context. For example if we want a stored procedure which have general logic and applicable or can be run in all database then we have to kept it in some common place so any one can use it. Now we will create a something like that one.

We will create a stored procedure which will list out the tables present in the database, 




CREATE PROCEDURE SP_GET_TABLES
AS
BEGIN

SELECT * FROM SYS.OBJECTS WHERE TYPE = 'U'

END



Above stored procedure will give a result of tables present in the database, but now we have to access it from all database so we have to compile this stuff in Master Database, For testing purpose i am creating a table in master database "Master_History" , Now this stored procedure can be access from all database.


USE MASTER
GO

CREATE TABLE MASTER_HISTORY(ID INT,OBJNAME VARCHAR(200))
GO


CREATE PROCEDURE SP_GET_TABLES
AS
BEGIN

    SELECT * FROM SYS.OBJECTS WHERE TYPE = 'U'

END



After compile the stored procedure we will test the sp, by executing it from different databases.
So first we will create a new database named "Rajesh" and create a one sample table to test the SP.



CREATE DATABASE RAJESH
Go

USE RAJESH 
GO

CREATE TABLE EMPLOYEE(ID INT, NAME VARCHAR(30))
GO



Now we will test the Stored procedure by executing it.



USE MASTER
GO
SELECT DB_NAME() AS 'dbname'
EXEC SP_GET_TABLES


USE RAJESH
GO
SELECT DB_NAME() AS 'dbname'
EXEC SP_GET_TABLES






When we see the result set of this query, we will wonder because both the execution gives the same result set, but we are running the sp from individual database, for second execution we must have only one record with value of Employee table , but instead of showing that it is showing the same result of master database, i.e the Stored procedure is executing in Master database instead of "Rajesh" Database, so now we are going to fix this by marking the object as System object , it will work like the object of that selected database context., To mark a Object as System object , we should use the Master database, because our object is present in that context


USE MASTER 
GO

EXEC SYS.SP_MS_MARKSYSTEMOBJECT 'SP_GET_TABLES'
GO

Above query will mark the object stored procedure as System object., we can check this by executing the following code.


SELECT NAME, IS_MS_SHIPPED 
FROM SYS.OBJECTS 
WHERE NAME = 'SP_GET_TABLES' 
GO






Again we will test the Stored procedure by pointing out it from different databases, now we see what is happening.


USE MASTER
GO
SELECT DB_NAME() AS 'dbname'
EXEC SP_GET_TABLES


USE RAJESH
GO
SELECT DB_NAME() AS 'dbname'
EXEC SP_GET_TABLES







After marking the object as System object , the Stored procedure is working correctly.From this post you can learn how to create a stored procedure and convert in to a system object , access it from all other database in Sql Server.


Tuesday 28 June 2016

Asynchronous programming with Async and Await

Let we see the simple implementation of async and await opertor in C# code.

1. Always async should be mention in method after access specifier.
2. Method name should be end in async.
3. Return type of async method be in void, Task, Task<T>



                The first thing is to know is that any method that you put async in front of is an asynchronous method, which means it can be started and stopped rather than just run from first to last instruction. We could create a new method and mark is as asynchronous but to keep the example as much like the synchronous case described above we can simply change the Click event handler into an asynchronous method:

private async void button1_Click(object sender, RoutedEventArgs e)
      {
         label1..Text = "Started";
         DoWork();
  label2.Text = "Finished";
      }

private int DoWork()
{
 return 1+2;
}



Now the compiler will say a message as Asynchronous method with not Await, make to run the method in synchronous way, So i have added await keyword in calling a method.

                  private async void button1_Click(object sender, RoutedEventArgs e)
    {
               label1..Text = "Started";
                await DoWork();
        label2.Text = "Finished";
          }

private int DoWork()
{
 return 1+2;
}

Now compiler will say a message that you can't await a method that returns void. we have to change the return type as Task or Task<T>.Now let we see the Task 




What is Task ?
    Task is an object that runs the code in separate thread from the UI thread which release the UI to do some other work.

Now we see the implemetation of await with task return type

              private async void button1_Click(object sender, RoutedEventArgs e)
              {
                    label1..Text = "Started";
                   await Task.Run(()=> DoWork());
                    label2.Text = "Finished";
              }

             private int DoWork()
             {
                 return 1+2;
             }




From this article I hope you will understand the async and await operation clearly when comparing to synchronous operation, and learn to change a call sync method to await call of Task return type method.








Sunday 26 June 2016

Information about Hedge Funds

In this post we are going to see what is a "Hedge Funds" in Investors world, and how it is differ from other 
Fund systems.

Hedge Funds are the normally named as “Alternative Investments” using pooled Funds. Hedge Funds uses different
strategies to yield a “Active Return” or “Absolute Return” for their investors. 

Hedge Fund are usually used by Wealthy individuals or Institutions, They used a aggressive strategies that are 
unavailable in Mutual Funds including Selling Shot, Leverage, Trading, arbitrage and derivatives Hedge Funds are restricted by law No more than 100 or 500 investors by Fund. 






Hedge Funds are the funds where highly wealthy people are allowed to invest, they are consider as investors because each and every investments are in 1 Million $. Each hedge fund follows a strategy is Constructed to take advantage of certain identifiable market opportunities. Hedge funds use different investment strategies and thus are often 
classified according to investment style. 

There is substantial diversity in risk attributes and investment opportunities among styles, which reflects the flexibility of the hedge fund format. In general, this diversity benefits investors by increasing the range of choices among investment attributes.





Hedge Funds main target is generating positive returns in Both up and Down markets. Through out the time investors are looking for ways to maximize the profit and minimizing the Risks. Shielding an investment from market 
risk through a alternate investment is called as “Hedge Funds”.




Always Absolute return is the term is used along with Hedge Funds, this describes that there will be a active return will be for investors in Good and Bad markets. 

“More sophisticated individuals or institutions are the investors in these kind of Hedge Funds”, A investor must pass an “Accredited investor test or qualifier purchase test“ whose net worth exceeds 1$ million or whose income in the 
last year $200,000 , there also another entity asses exceeds 5$ Million.

Hedge Funds rely on sections 3 (c)(1), and 3 (c)(7) of the investment company act of 1940 , Hedge funds are 
prohibited from advertising there funds from Public. 





Hedge Fund Managers are applying strategies they will add alpha ,they will use there skill to solve the inefficiency
in the market. Hedge Funds concentrate on the Active or absolute returns for their investors

Hedge Funds try to protect their investors in the downside of the market , an Hedge Funds portfolio have lot of 
investments where investors can choose there hedge funds based on the sophistication 




From this post you can learn some basic information about the Hedge Funds and there advantages to the investors