0% found this document useful (0 votes)
18 views

sql6

The document outlines a training module on Advanced Databases and SQL Querying led by Yaroslav Dobrianskyi, covering topics such as Dynamic SQL Queries, SQL Filestream, Full-Text Search, and SQL Geography and Geometry types. It includes guidelines for participants, the agenda, and detailed explanations of SQL syntax and functionalities. The session is designed to enhance understanding of advanced database management techniques and SQL querying capabilities.

Uploaded by

and311003
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

sql6

The document outlines a training module on Advanced Databases and SQL Querying led by Yaroslav Dobrianskyi, covering topics such as Dynamic SQL Queries, SQL Filestream, Full-Text Search, and SQL Geography and Geometry types. It includes guidelines for participants, the agenda, and detailed explanations of SQL syntax and functionalities. The session is designed to enhance understanding of advanced database management techniques and SQL querying capabilities.

Uploaded by

and311003
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Module “DBMS

Basics”
Submodule “Advanced Databases and SQL
Querying”
Yaroslav Dobrianskyi
Lead Software Engineer

CONFIDENTIAL | © 2020 EPAM Systems, Inc.


Recommendations

1 MUTE YOUR MIC

2 RAISE HAND ASK QUESTIONS

3 DURATION: 2 HOURS

4 COFFEE BREAK: 10-15 MINUTES

5 Q&A AFTER EACH MODULE

6 TEAMS QUESTIONS CHANNEL

CONFIDENTIAL | © 2020 EPAM Systems, Inc. 2


YA R O S L AV D O B R I A N S K Y I
Lead Software Engineer

• 13 years experience in software development


• Worked at different positions -> DBA, DB Development, ETL,
Python Developer
• Worked at EPAM in 2015-2019 years, joined second time in
2021

CONFIDENTIAL | © 2020 EPAM Systems, Inc. 3


Agenda

1. Dynamic SQL Queries


2. Filestream and Full-Text search
3. SQL Geography and Geometry types

CONFIDENTIAL | © 2020 EPAM Systems, Inc. 4


DY N A M I C S Q L Q U E R I E S

CONFIDENTIAL | © 2020 EPAM Systems, Inc.


Dynamic SQL Queries

SYNTAX:
In SQL Server, local variables are used to store data
during the batch execution period. The local DECLARE @Variable_name <data_Type> ;
DECLARE @Variable_name <data_Type> = value ;
variables can be created for different data types
and can also be assigned values. Additionally, SET @Variable_name = Value
variable assigned values can be changed during the SELECT @Variable_name = Value
execution period.
SELECT @Variable_name = Value FROM <table_name>
The life cycle of the local variable starts from the
point where it is declared and has to end at the end
EXAMPLES:
of the batch. On the other hand, If a variable is
being used in a stored procedure, the scope of the DECLARE @A INT = 1 , @name NVARCHAR(20);
variable is limited to the current stored procedure.
SET @A=10;
SELECT @name = 'Hello'

SELECT @A as IntValue, @name as CharValue

SELECT @name + ' world!’ , @a*5

CONFIDENTIAL | © 2020 EPAM Systems, Inc.


Dynamic SQL Queries

The table variable is a special type of the local SYNTAX:


variable that helps to store data temporarily,
DECLARE @LOCAL_TABLEVARIABLE TABLE
similarly to the temp table in SQL Server.
(column_1 <DATATYPE>,
In fact, the table variable provides all the properties column_2 <DATATYPE>,
of the local variable, but the local variables have column_N <DATATYPE>
)
some limitations, unlike temp or regular tables.

EXAMPLES:
Unlike simple local variables that store data in
memory, table variables are stored in tempdb. All DECLARE @ListOWeekDays TABLE
DML operation INSERT , UPDATE, DELETE can be (DayNumber INT, ShortName VARCHAR(40) , WeekName VARCHAR(40))
applied to modify data sored in table variables, INSERT INTO @ListOWeekDays
DDL statements such as DROP, CREATE and ALTER VALUES
and TRUNCATE are not applied to table variables. (1,'Mon','Monday') ,
(2,'Tue','Tuesday') ,
That’s because table variable created after (3,'Wed','Wednesday') ,
definition and dropped after batch execution (4,'Thu','Thursday'),
completes. Also, we can’t create index on table (5,'Fri','Friday'),
(6,'Sat','Saturday'),
variable columns , but instead we can create (7,'Sun','Sunday')
PRIMARY KEY or UNIQUE constraint, SELECT * FROM @ListOWeekDays

CONFIDENTIAL | © 2020 EPAM Systems, Inc.


Dynamic SQL Queries

SYNTAX:
SQL Server provides a massive number of global
variables, which are very effective to use in our SELECT @Global_Variable_name
regular Transact-SQL. Global variables represent a
special type of variable. The server always maintain
the values of these variables. All the global variables EXAMPLES:
represent information specific to the server or a
current user session. @@CONNECTIONS @@PACKET_ERRORS
@@MAX_CONNECTIONS @@ROWCOUNT
@@CPU_BUSY @@SERVERNAME
@@ERROR @@SPID
Global variable names begin with a @@ prefix. You @@IDENTITY @@TEXTSIZE
do not need to declare them, since the server @@IDLE @@TIMETICKS
constantly maintains them. @@IO_BUSY @@TOTAL_ERRORS
@@LANGID @@TOTAL_READ /
They are system-defined functions and you cannot @@LANGUAGE @@TOTAL_WRITE
declare them. @@MAXCHARLEN @@TRANCOUNT
@@PACK_RECEIVED @@VERSION
@@PACK_SENT

CONFIDENTIAL | © 2020 EPAM Systems, Inc.


Dynamic SQL Queries

Dynamic SQL is the SQL statement that is


constructed and executed at runtime based on
input parameters passed.
Let us go through some examples using the EXEC
command and sp_executesql extended stored EXAMPLES:
procedure.
-- Simple dynamic SQL statement
DECLARE @SQL nvarchar(1000)
SYNTAX:
declare @Pid varchar(50)
set @pid = '680'
EXEC (@sql_statement);

sp_execute_sql @sql_stmt,
SET @SQL = 'SELECT ProductID,Name,ProductNumber FROM
N'@parameter_name data_type’
SalesLT.Product where ProductID = '+ @Pid
, @param1 = 'value1’
, @param2 = 'value2’
EXEC (@SQL)

CONFIDENTIAL | © 2020 EPAM Systems, Inc.


Dynamic SQL Queries

EXAMPLES :

-- Simple dynamic SQL statement


EXECUTE sp_executesql
N'SELECT ProductID,Name,ProductNumber
FROM SalesLT.Product
WHERE ProductID = @Pid
and ProductNumber=@PNumber',
N'@Pid varchar(50),@PNumber varchar(50)',
@pid = '680',@PNumber='FR-R92B-58';

CONFIDENTIAL | © 2020 EPAM Systems, Inc.


Dynamic SQL Queries

PIVOT

CONFIDENTIAL | © 2020 EPAM Systems, Inc.


Dynamic SQL Queries

PIVOT operator rotates a table-valued expression. It


turns the unique values in one column into multiple
columns in the output and performs aggregations on any
remaining column values.
You follow these steps to make a query a pivot table:

• First, select a base dataset for pivoting. SYNTAX:

• Second, create a temporary result by using a derived SELECT first_column AS <first_column_alias>, [pivot_value1], [pivot_value2], ...
table or common table expression (CTE) [pivot_value_n]
FROM (<source_table>) AS <source_table_alias>
• Third, apply the PIVOT operator. PIVOT (
aggregate_function(<aggregate_column>)
FOR <pivot_column>
IN ([pivot_value1], [pivot_value2], ... [pivot_value_n])
) AS <pivot_table_alias>;

CONFIDENTIAL | © 2020 EPAM Systems, Inc.


SQL FILESTREAM
AND FULL-TEXT SERACH

CONFIDENTIAL | © 2020 EPAM Systems, Inc.


SQL Filestream and Free-Text Search

FILESTREAM, in SQL Server, allows storing these large documents, images or files onto the file system itself. In FILESTREAM, we do not have a limit of
storage up to 2 GB, unlike the BLOB data type. Therefore, we get the performance benefit of this streaming API as well while accessing these
documents.
In SQL Server FILESTREAM, We need to define a new filegroup ‘FILESTREAM’. We need to define a table having varbinary(max) column with the
FILESTREAM attribute. It allows SQL Server to store the data in the file system for these data type. When we access the documents that are stored in
the file system using the FILESTREAM, we do not notice any changes in accessing it. It looks similar to the data stored in a traditional database.

pictures from: https://www.sqlshack.com/filestream-in-sql-server/

CONFIDENTIAL | © 2020 EPAM Systems, Inc.


SQL Filestream and Free-Text Search

FREETEXT - is a predicate used in the Transact-SQL WHERE clause of a Transact-SQL SELECT statement to perform a SQL Server full-text search on full-
text indexed columns containing character-based data types. This predicate searches for values that match the meaning and not just the exact
wording of the words in the search condition. When FREETEXT is used, the full-text query engine internally performs the following actions on
the freetext_string, assigns each term a weight, and then finds the matches:

Comparison of LIKE to Full-Text Search


• In contrast to full-text search, the LIKE Transact-SQL predicate works on character patterns only.
• You cannot use the LIKE predicate to query formatted binary data.
• LIKE query against a large amount of unstructured text data is much slower than an equivalent full-text query against the same data. A LIKE query
against millions of rows of text data can take minutes to return; whereas a full-text query can take only seconds or less against the same data,
depending on the number of rows that are returned.

CONFIDENTIAL | © 2020 EPAM Systems, Inc.


SQL Filestream and Free-Text Search

Steps to work with FREETEXT (full-text search):


1. Create FULLTEXT CATALOG

CONFIDENTIAL | © 2020 EPAM Systems, Inc.


SQL Filestream and Free-Text Search

Steps to work with FREETEXT (full-text search):


2. Create FULLTEXT INDEX

CONFIDENTIAL | © 2020 EPAM Systems, Inc.


SQL Filestream and Free-Text Search

Steps to work with FREETEXT (full-text search):


2. Create FULLTEXT INDEX

CONFIDENTIAL | © 2020 EPAM Systems, Inc.


SQL Filestream and Free-Text Search

Steps to work with FREETEXT (full-text search): /SYNTAX VERSION


1. Create FULLTEXT CATALOG

2. Create FULLTEXT INDEX CREATE FULLTEXT CATALOG ftCatalog AS DEFAULT;


GO

CREATE FULLTEXT INDEX ON [Production].[ProductDescription](Description)


KEY INDEX [PK_ProductDescription_ProductDescriptionID]
GO

CONFIDENTIAL | © 2020 EPAM Systems, Inc.


SQL Filestream and Free-Text Search

Steps to work with FREETEXT (full-text search):

3. You are AWESOME! And now you can use FREETEXT in your queries

CONFIDENTIAL | © 2020 EPAM Systems, Inc.


SQL GEOGRAPHY AND
GEOMETRY TYPE

CONFIDENTIAL | © 2020 EPAM Systems, Inc.


SQL Geography and Geometry datatypes

Spatial data is used to represent information about the location and shape of geometric objects. These objects can be the center point locations or
more complex structures: roads, rivers, cities or countries.

In the SQL Server, spatial data can be of two types:


• Geometrical (geometry) - data that is represented in an Euclidean system (flat-earth, 2D).
• Geographical (geography) - data that takes into account the curvature of the Earth and is represented using an ellipsoidal system (round-earth, 3D).

Available objects for the geometrical and geographical data types (from MSDN):

CONFIDENTIAL | © 2020 EPAM Systems, Inc.


SQL Geography and Geometry datatypes

SQL Server has several functions and methods that allow us to manage spatial data types: for importing
data objects (STGeomFromText, STGeomFromWKB), for making different types of operations (STContains,
STOverlaps, STUnion, STIntersection) or for making different measurements (STArea, STDistance).

https://docs.microsoft.com/ru-ru/sql/t-sql/spatial-geometry/ogc-methods-on-geometry-instances?view=sql-server-ver15

Another interesting link: https://www.sqlshack.com/spatial-data-types-in-sql-


server/

CONFIDENTIAL | © 2020 EPAM Systems, Inc.


SQL Geography and Geometry datatypes

Geometry vs Geography

The type of data you choose depends on the application and its purpose. From the point of view of data storing, there is no difference between
the two types of spatial data. But if we check performance levels, geometrical data queries are much faster. In the end, the most important
argument is functionality. If we have an application for measuring the distance between different locations, or other operations where we need
to take into account the shape of the Earth, we will need to use geographical data. In other cases, for example if we only need to visualize
different polygons, geometrical data might be enough.

CONFIDENTIAL | © 2020 EPAM Systems, Inc.


T H A N K S F O R AT T E N D I N G ! Q U E S T I O N S ?

CONFIDENTIAL | © 2020 EPAM Systems, Inc.

You might also like