sql6
sql6
Basics”
Submodule “Advanced Databases and SQL
Querying”
Yaroslav Dobrianskyi
Lead Software Engineer
3 DURATION: 2 HOURS
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'
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
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
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)
EXAMPLES :
PIVOT
• 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>;
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.
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:
3. You are AWESOME! And now you can use FREETEXT in your queries
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.
Available objects for the geometrical and geographical data types (from MSDN):
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
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.