9 Oct 2024

Time based welcome greetings

 SELECT

    CAST(TourDate AS DATE) as date ,
    EmployeeName AS Name,
    STRING_AGG(CONCAT(
	CASE
        WHEN CAST(DATEPART(HOUR, [TourDate]) AS INT) < 12 THEN 'Good Morning '
        WHEN CAST(DATEPART(HOUR, [TourDate]) AS INT) BETWEEN 12 AND 17 THEN 'Good Afternoon '
        ELSE 'Good Evening ' End +
	'From ', DeparturePlace, ' To ', ArrivalPlace), ', ')
        WITHIN GROUP (ORDER BY TourDate) AS VisitedPlace
FROM
    Tour
GROUP BY
    CAST(TourDate AS DATE), EmployeeName;

 

How to Use a Cursor in SQL?

 Here's how to use a cursor in SQL with a practical example,

  1. First, we need to understand what a cursor is and why it might be useful.
  2. Then, we'll create a sample table and populate it with data.
  3. We'll define the cursor and declare variables to store the data.
  4. We'll open the cursor and fetch data from it.
  5. Finally, we'll close and deallocate the cursor.

Cursors in SQL

  • Cursors allow us to iterate through a result set one row at a time.
  • They are useful when we need to perform multiple operations on each row of a result set.
  • Cursors can be explicit (programmer-defined) or implicit (automatically created).
  • Explicit cursors give us more control over the iteration process.

Let's create a sample scenario where we have a table of employees, and we want to update their salaries based on certain criteria.

Create a sample table

-- Create a sample table
CREATE TABLE Employees (
    EmployeeID INT PRIMARY KEY,
    Name VARCHAR(50),
    Department VARCHAR(50),
    Salary DECIMAL(10, 2)
);
SQL

Insert sample data

-- Insert sample data
INSERT INTO Employees (EmployeeID, Name, Department, Salary)
VALUES
    (1, 'John Doe', 'IT', 50000.00),
    (2, 'Jane Smith', 'HR', 45000.00),
    (3, 'Bob Johnson', 'Finance', 55000.00),
    (4, 'Alice Brown', 'Marketing', 40000.00);
SQL

Let's See the Result by running this script.

SELECT * FROM [dbo].[Employees]
SQL

Output

Output

Write the script below and run it.

-- Open the cursor
OPEN EmployeeCursor;

-- Fetch the first row
FETCH NEXT FROM EmployeeCursor INTO @EmployeeID, @Name, @Department, @Salary;

-- Loop through the rows
WHILE @@FETCH_STATUS = 0
BEGIN
    -- Perform operations on the current row
    IF @Department IN ('IT', 'Finance')
        SET @Salary = @Salary * 1.1; -- Increase salary by 10% for IT and Finance departments

    -- Print the updated information
    PRINT 'Employee ID: ' + CAST(@EmployeeID AS VARCHAR(10)) +
          ', Name: ' + @Name +
          ', Department: ' + @Department +
          ', New Salary: ' + CAST(@Salary AS VARCHAR(10));

    -- Move to the next row
    FETCH NEXT FROM EmployeeCursor INTO @EmployeeID, @Name, @Department, @Salary;
END

-- Close and deallocate the cursor
CLOSE EmployeeCursor;
DEALLOCATE EmployeeCursor;
SQL

In this script, we update employees' salaries by 10% for IT and Finance.

Output

Employees' salaries

Summary

This example demonstrates how to use a cursor in SQL Server to iterate through a result set and perform operations on each row. Here's a breakdown of what the code does:

  1. We create a sample table of Employees and insert some data.
  2. We declare variables to hold the data from each row.
  3. We define the cursor based on our SELECT statement.
  4. We open the cursor and fetch the first row.
  5. We use a WHILE loop to continue fetching rows until there are no more rows to fetch.
  6. Inside the loop, we perform operations on the current row (increasing salaries for certain departments).
  7. We print the updated information for each row.
  8. Finally, we close and deallocate the cursor.

Top 10 SQL Skills You Need to Succeed in 2024

  Top 10 SQL Skills You Need to Succeed in 2024 To help you navigate the world of SQL and become a data-driven professional, we've ident...