0% found this document useful (0 votes)
23 views2 pages

ITI Database SQL Functions Guide

The document outlines a series of SQL Server tasks involving the creation of various functions and queries using the ITI database. It includes instructions for scalar and table-valued functions, as well as queries for data manipulation and retrieval. Additionally, there are bonus tasks related to Hierarch ID data type and batch insertion into an employee table in the CompanyDB.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views2 pages

ITI Database SQL Functions Guide

The document outlines a series of SQL Server tasks involving the creation of various functions and queries using the ITI database. It includes instructions for scalar and table-valued functions, as well as queries for data manipulation and retrieval. Additionally, there are bonus tasks related to Hierarch ID data type and batch insertion into an employee table in the CompanyDB.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

SQLServer Lab

Note: Use ITI DB

1. Create a scalar function that takes date and returns Month name of that
date.

2. Create a multi-statements table-valued function that takes 2 integers


and returns the values between them.

3. Create inline function that takes Student No and returns Department


Name with Student full name.

4. Create a scalar function that takes Student ID and returns a message to


user
a. If first name and Last name are null then display 'First name &
last name are null'
b. If First name is null then display 'first name is null'
c. If Last name is null then display 'last name is null'
d. Else display 'First name & last name are not null'

5. Create inline function that takes integer which represents manager ID


and displays department name, Manager Name and hiring date

6. Create multi-statements table-valued function that takes a string


If string='first name' returns student first name
If string='last name' returns student last name
If string='full name' returns Full Name from student table
Note: Use “ISNULL” function
7. Write a query that returns the Student No and Student first name
without the last char
8. Wirte query to delete all grades for the students Located in SD
Department
Bonus:

1. Give an example for Hierarch id Data type


2. Create a batch that inserts 3000 rows in the employee table. The values of
the emp_no column should be unique and between 1 and 3000. All values
of the columns emp_lname, emp_fname, and dept_no should be set to
'Jane', ' Smith', and ' d1', respectively.”USE CompnayDB”

Common questions

Powered by AI

SQL queries can manipulate the character set of a string field by using string functions like SUBSTRING or LEFT. For example, to alter the student information, a query can return the Student No and the Student first name without the last character by using the LEFT function with the length of the student's first name minus one, effectively truncating the last character from the string .

Table-valued functions in SQL can be utilized to return subsets of data between given values by accepting input parameters and processing them through a series of SQL statements. An example is a multi-statement table-valued function that takes two integers and returns all integer values between them. This type of function allows for custom SQL logic to handle the input parameters and the output is presented as a table format .

The ISNULL function in SQL plays a critical role in handling null values, particularly in string-based queries. It allows returning a default value when a column value is null. For retrieving student names, a multi-statement table-valued function uses ISNULL to determine if the requested column 'first name', 'last name', or 'full name' is null and, if so, replaces it with a default string as needed, ensuring that the query always returns a complete result set .

Scalar functions in SQL are used to handle specific cases of null values in student records by returning customized messages to inform users of missing data. The suggested approach includes creating a scalar function that takes a Student ID and checks for null values in first and last names. If both are null, it returns 'First name & last name are null'; if only the first name is null, it returns 'first name is null'; if only the last name is null, it returns 'last name is null'; and if neither is null, it returns 'First name & last name are not null' .

Inline functions in SQL benefit the management of student-related data by optimizing performance and simplifying queries. They allow computations to occur inline within a larger query, reducing overhead. An example is an inline function that, given a Student No, returns the Department Name and Student full name, allowing quick retrieval of related information without complex joins or multiple queries .

Designing scalar and inline functions that interact with student and departmental data involves understanding the data relationships, identifying potential null value scenarios, and ensuring optimal performance. Scalar functions are usually designed to return single values and can handle null checks for individual columns, whereas inline functions integrate with larger queries to produce combined data outputs like student names and departments. An example includes creating an inline function to take an integer representing manager ID and display the department name, manager name, and hiring date, demonstrating careful linking of related data tables .

Hierarchical data types in SQL, such as Hierarch id, are used to manage data that inherently has a tree-like structure, like organizational charts. In a company database, an example of its application could include storing and querying hierarchical positions or departments within the company, allowing for queries that easily navigate and manipulate the hierarchical relationships between entities .

SQL utilizes batch processing to efficiently manage large data insertions by performing a set of insert operations in a single transaction batch, which minimizes the overhead of multiple commits. For employee data entry, a suggest implementation would involve a batch process that inserts 3000 rows into an employee table with unique emp_no values and preset values for emp_lname, emp_fname, and dept_no, thereby ensuring efficiency while maintaining data integrity .

To dynamically generate unique values within a batch insertion in SQL, a loop or sequential mechanism is typically used to ensure that each row inserted has a unique identifier. In the provided task, a batch processes by inserting rows with unique values for the emp_no column within a range of 1 to 3000. This implementation uses a loop that increments the emp_no for each row inserted, ensuring all inserted emp_no values are unique. Other columns like emp_lname, emp_fname, and dept_no are given fixed values as 'Jane', 'Smith', and 'd1', respectively .

Effective strategies for updating or deleting data in SQL based on departmental criteria involve formulating queries that accurately target the records within the specified department. For example, deleting all grades for students located in the 'SD Department' requires a precise DELETE query with a WHERE clause that filters records based on the department name, ensuring that only the intended records are removed from the database .

You might also like