Monthly Archives: July 2020

Simple Stored Procedure

Creating a simple stored procedure

The following SELECT statement returns a list of products from the products table in the BikeStores sample database:

SELECT 
	product_name, 
	list_price
FROM 
	production.products
ORDER BY 
	product_name;

To create a stored procedure that wraps this query, you use the CREATE PROCEDURE statement as follows:

CREATE PROCEDURE uspProductList
AS
BEGIN
    SELECT 
        product_name, 
        list_price
    FROM 
        production.products
    ORDER BY 
        product_name;
END;

In this syntax:

  • The uspProductList is the name of the stored procedure.
  • The AS keyword separates the heading and the body of the stored procedure.
  • If the stored procedure has one statement, the BEGIN and END keywords surrounding the statement are optional. However, it is a good practice to include them to make the code clear.

Note that in addition to the CREATE PROCEDURE keywords, you can use the CREATE PROC keywords to make the statement shorter.

To compile this stored procedure, you execute it as a normal SQL statement in SQL Server Management Studio as shown in the following picture:

If everything is correct, then you will see the following message:

Commands completed successfully.

It means that the stored procedure has been successfully compiled and saved into the database catalog.

You can find the stored procedure in the Object Explorer, under Programmability > Stored Procedures as shown in the following picture:

Sometimes, you need to click the Refresh button to manually update the database objects in the Object Explorer.

Executing a stored procedure

To execute a stored procedure, you use the EXECUTE or EXEC statement followed by the name of the stored procedure:

EXECUTE sp_name;

Or

EXEC sp_name;

where sp_name is the name of the stored procedure that you want to execute.

For example, to execute the uspProductList stored procedure, you use the following statement:

EXEC uspProductList;

The stored procedure returns the following output:

SQL Server Stored Procedure output

Modifying a stored procedure

To modify an existing stored procedure, you use the ALTER PROCEDURE statement.

First, open the stored procedure to view its contents by right-clicking the stored procedure name and select Modify menu item:

SQL Server Stored Procedure modifying

Second, change the body of the stored procedure by sorting the products by list prices instead of product names:

 ALTER PROCEDURE uspProductList
    AS
    BEGIN
        SELECT 
            product_name, 
            list_price
        FROM 
            production.products
        ORDER BY 
            list_price 
    END;

Third, click the Execute button, SQL Server modifies the stored procedure and returns the following output:

Commands completed successfully.

Now, if you execute the stored procedure again, you will see the changes taking effect:

EXEC uspProductList;

The following shows the partial output:

SQL Server Stored Procedure output changes

Deleting a stored procedure

To delete a stored procedure, you use the DROP PROCEDURE or DROP PROC statement:

DROP PROCEDURE sp_name;

or

DROP PROC sp_name;    

where sp_name is the name of the stored procedure that you want to delete.

For example, to remove the uspProductList stored procedure, you execute the following statement:

DROP PROCEDURE uspProductList;

The different Data Systems

Data Lake – A Data Lake is a vast pool of raw data. Data is never thrown away because data is stored in the raw format.

Data Warehouse – A Data Warehouse is s repository for structured filtered data that has already been processed for a specific purpose.

Big Data – Big Data is extremely large data sets that may be analyzed computationally to reveal patterns, trends and associations especially relating to human behavior and interactions.

ODS – Operation Data Store is meant for operational reporting and supports current or near real-time reporting requirements. It contains only a short window of data.

Links to code manuals are found on the footer of this website.

What is Big Data?

Big data is a collection of large datasets that cannot be processed using traditional computing techniques. It is not a single technique or a tool, rather it has become a complete subject, which involves various tools, technqiues and frameworks.

What Comes Under Big Data?

Big data involves the data produced by different devices and applications. Given below are some of the fields that come under the umbrella of Big Data.

  • Black Box Data − It is a component of helicopter, airplanes, and jets, etc. It captures voices of the flight crew, recordings of microphones and earphones, and the performance information of the aircraft.
  • Social Media Data − Social media such as Facebook and Twitter hold information and the views posted by millions of people across the globe.
  • Stock Exchange Data − The stock exchange data holds information about the ‘buy’ and ‘sell’ decisions made on a share of different companies made by the customers.
  • Power Grid Data − The power grid data holds information consumed by a particular node with respect to a base station.
  • Transport Data − Transport data includes model, capacity, distance and availability of a vehicle.
  • Search Engine Data − Search engines retrieve lots of data from different databases.

Thus Big Data includes huge volume, high velocity, and extensible variety of data. The data in it will be of three types.

  • Structured data − Relational data.
  • Semi Structured data − XML data.
  • Unstructured data − Word, PDF, Text, Media Logs.

Benefits of Big Data

  • Using the information kept in the social network like Facebook, the marketing agencies are learning about the response for their campaigns, promotions, and other advertising mediums.
  • Using the information in the social media like preferences and product perception of their consumers, product companies and retail organizations are planning their production.
  • Using the data regarding the previous medical history of patients, hospitals are providing better and quick service.