European Windows 2019 Hosting BLOG

BLOG about Windows 2019 Hosting and SQL 2019 Hosting - Dedicated to European Windows Hosting Customer

European SQL Server 2019 Hosting :: APPLY in SQL Server

clock February 20, 2023 06:39 by author Peter

The apply keyword was introduced mainly for selecting data by combining a select query with a table-valued function, which is nothing but a select query on another table (or the result of any business logic in the function). To understand it better, let's call our main table the left table and the second table (in the table-valued function) the right table.

Let's start by creating sample data. We will have two tables, Employee and Department. Here, our Employee table will be the left table, and the Department table will be the right one. Each employee can only belong to one department.

Our intent will be to join these two tables to get the required data. One solution that immediately comes to mind is using a LEFT, RIGHT, or INNER JOIN, which will depend on our requirements. Let's see the results with the use of JOINS.

APPLY works the same way as the co-related subquery, with the fact that the select query on which the application is used, in other words, the right table, will be executed for every record of the main table or the left table, that is nothing but how the co-related sub query works. The same results can be obtained using the apply keyword. It can be applied in either of the following two ways:

    CROSS APPLY- Works the same as the INNER JOIN on two queries.
    OUTER APPLY- Works the same as the LEFT JOIN on two queries.

Let's change the queries using these two apply forms and see the results.


As we can see above, CROSS APPLY gives the same result as the INNER JOIN, and OUTER APPLY gives the same result as the LEFT OUTER JOIN. The difference with the JOIN is that APPLY results in the execution of the select statement of the Department query for each record of the Employee record (the same as that of a co-related sub-query).

Next, suppose we were using the co-related subquery. But we need to view the rest of the columns of the second table. In other words, the Department table. Can we do that? Unless we add some twist to the query, it doesn't seem to be. But this can be easily done with the APPLY keyword. Add the name of the columns we want to view in the select statement of the Department, and we are done. Let's change our queries and see the results:


Another possible and extensive use of APPLY is with the table-valued function. We create a simple function that returns Department details by Id. Next, we replace our select statement for Department with a call to the user-defined function. See the query below:


So, depending on the requirements, we can add or remove the columns' names in the function call's SELECT statement. To summarize, we can use the apply keyword as.

    A co-related subquery with the advantage of selecting multiple columns.
    A join with the table-valued user-defined function to select multiple columns from the second table.

So this was about the use of the apply keyword.

HostForLIFEASP.NET SQL Server 2019 Hosting



European SQL Server 2019 Hosting :: Deploy SSIS Package To SQL Server

clock February 16, 2023 07:01 by author Peter

Note
Before going next, first, make sure you have SQL Server Integration Services installed. Open the Visual Studio SSIS package project and right-click on the project and hit Deploy to deploy all packages, if you want to install individual packages then right-click on the package and hit deploy.

The first window is the introduction windows click the Next button.


We have two deployment targets,

    SSIS in SQL Server
    SSIS in Azure Data Factory

As in this article, we are going to deploy on SQL Server, so we must select SSIS in SQL Server and click Next.


Select a destination, Enter the SQL Server name, Authentication type, Username, and password, and click Connect. Once connect Browse the project folder path if available, if not available create a directory in SSISDB and create a new project, and hit Next.

You can review all the given changes and hit Deploy.


You can check the deployment result in the last windows. If all results are passed, then click close.

The above screenshot shows that all results are passed and successfully deployed.


Go to SQL Server and expand Integration Services Catalogs and go to SSISDB you can see the created folder and project and deployed packages there.

Conclusion
In this article, we have learned how to deploy SSIS Project to SQL Server.

HostForLIFEASP.NET SQL Server 2019 Hosting



European SQL Server 2019 Hosting :: Change Data Capture in SQL Server

clock February 13, 2023 06:42 by author Peter

Change Data Capture (CDC) captures the data of insert, update and delete activity. When you insert or delete the data in the table it maintains a record of the same data. When you update the data it maintains records before updating the data and after updating the data.

To understand the change data capture we go through the following process.

Process

Step 1. Create DB
CREATE DATABASE CDC_DEMO
GO


Step 2. Create a Table

Create one table in the preceding database.

Execute the following query and the "CDC_DEMO_TABLE1" table is created.
USE CDC_DEMO
GO

CREATE TABLE CDC_DEMO_TABLE1
(
    ID      INT         IDENTITY(1,1) PRIMARY KEY,
    Name        VARCHAR(50)     NOT NULL,
    Age     INT         NOT NULL,
);
GO


You can check the table in the Object Explorer.

Step 3. Insert Rows
Insert some rows into the table "CDC_DEMO_TABLE1".

Here we inserted two rows into the table.
USE CDC_DEMO
GO

INSERT INTO CDC_DEMO_TABLE1 (Name,Age) VALUES ('Peter',34)
GO
INSERT INTO CDC_DEMO_TABLE1 (Name,Age) VALUES ('Scott',38)
GO

Step 4. Enable CDC on DB
We have a database, table, and some rows in the table, now we need to enable CDC on the database.
Execute the following query and it will show whether CDC is enabled or not for the database.
USE CDC_DEMO
GO

SELECT name, database_id, is_cdc_enabled
FROM SYS.DATABASES
WHERE name = 'CDC_DEMO'

"is_cdc_enabled" has the value "0", which means it is not enabled for the database.

Execute the following query to enable CDC on the database. We need to execute the "sys.sp_cdc_enable_db" Stored Procedure to enable CDC on the database. It is necessary to execute it before we know any tables are enabled for the CDC.
USE CDC_DEMO
GO
EXEC sys.sp_cdc_enable_db
GO

This will create some system tables.

Check again and verify that CDC is enabled on the database.
USE CDC_DEMO
GO

SELECT name, database_id, is_cdc_enabled
FROM SYS.DATABASES

WHERE name = 'CDC_DEMO'

Now "is_cdc_enabled" has the value 1, in other words, it is enabled.

Step 5. Enable CDC on Table
Enable CDC on the "CDC_DEMO_TABLE1" table.
Before enabling CDC, we need to check whether it is enabled already or not. Execute the following query and we have a list of all tables with CDC status.
USE CDC_DEMO
GO
SELECT [name], is_tracked_by_cdc  FROM SYS.TABLES
GO

The value of "is_tracked_by_cdc" is "0" for the "CDC_DEMO_TABLE1" table, in other words, CDC is not enabled for this table.
Execute the following query to enable CDC on the table.
USE CDC_DEMO;
GO
EXECUTE sys.sp_cdc_enable_table
  @source_schema = N'dbo'
  , @source_name = N'CDC_DEMO_TABLE1'
  , @role_name = NULL
GO


We can check in the Object Explorer that one more table is created under the system tables, "cdc.dbo_CDC_DEMO_TABLE1_CT".

Check again and verify that CDC is enabled on the table.

USE CDC_DEMO
GO
SELECT [name], is_tracked_by_cdc  FROM SYS.TABLES
GO

Now "is_tracked_by_cdc" has the value 1, which represents that CDC is enabled for the table.

Step 6. Insert Operation
We have enabled CDC for the database and table. Now let's check where SQL Server persists in the change log when we insert the data in the table.
Execute the following query to insert one row into the table.
USE CDC_DEMO
GO

INSERT INTO CDC_DEMO_TABLE1 (Name,Age) VALUES ('Alex',35)
GO

Open the table "CDC_DEMO_TABLE1" and we can see that one row is inserted with the ID 3.

The change log is captured in the table "cdc.dbo_CDC_DEMO_TABLE1_CT". You can see the entire row that we have created. One more thing you can observe here is that the _$operation value is 2, in other words for Insert values.

Step 7. Update Operation
Now let's check by updating any of the rows in the table. Execute the following script that will update the value of the name field where id = 3.
USE CDC_DEMO
GO

UPDATE CDC_DEMO_TABLE1
SET Name = 'Jigi'
WHERE id = 3
GO


Open the table and verify that the value is changed.

Open the "cdc.dbo_CDC_DEMO_TABLE1_CT" table and you can see that the updated data is captured in two rows. One is with operation 3 and the other with operation 4. Operation value 3 means before updating and value 4 means after updating.

Step 8. Delete Operation
To check the captured data after the delete operation, execute the following script that deletes the record with id=3.
USE CDC_DEMO
GO

DELETE FROM CDC_DEMO_TABLE1
WHERE id = 3
GO


Open the table and verify that the record is deleted from the table.

Open the "cdc.dbo_CDC_DEMO_TABLE1_CT" table and you can see that the deleted row is captured with operation value 1.

We have seen a change in data capture for insert, update and delete operations and for those only one system table is used, "cdc.dbo_CDC_DEMO_TABLE1_CT". But there are more than six tables that were created when enabling CDC on the database. So let's see the schema and values for those tables:

Cdc.captured_columns
Provides the information of columns that are tracked for the changed data capture.


Cdc.change_tables
Provides the information in the table. It shows the default value for "capture_instance" since we have not provided a parameter when enabling CDC on the table.

Cdc.ddl_history
Provides the information for any schema changes. Currently, this table doesn't have any value since we did not change any schema for the table. So let's change the schema and check the values. Execute the following query to change the schema for the table:
USE CDC_DEMO
GO

ALTER TABLE CDC_DEMO_TABLE1
ALTER COLUMN Name VARCHAR(100) NOT NULL
GO

We have changed the datatype from varchar(50) to varchar(100) for the name field.

Open the "cdc.ddl_history" table and we can see that the ddl_command is captured as in the following:

Cdc.index_columns
Provides the information if any of the index columns are changed.

Cdc.Isn_time_mapping
Provides information about the start and end time for the operation done for changes.

Cdc.systranschemas
Provides the information for the schema changes.

Step 9. Disable CDC on Table

Execute the following query to disable CDC on the table.
USE CDC_DEMO;
GO

EXECUTE sys.sp_cdc_disable_table
    @source_schema = N'dbo',
    @source_name = N'CDC_DEMO_TABLE1',
    @capture_instance = N'dbo_CDC_DEMO_TABLE1'
GO


We can observe in the Object Explorer that one table is removed under the system tables, "cdc.dbo_CDC_DEMO_TABLE1_CT". That means CDC is disabled for this table.


Step 10. Disable CDC on Database
Execute the following query to disable CDC on the database. 
USE CDC_DEMO
GO
EXEC sys.sp_cdc_disable_db
GO


We can observe in the Object Explorer that all the tables are removed under the system tables. That means CDC is disabled on the database.

HostForLIFEASP.NET SQL Server 2019 Hosting

 



European SQL Server 2019 Hosting :: Full Text Index In SQL Server

clock February 9, 2023 08:35 by author Peter

Full-text search is one of the needs of an application to find data in a database. The full-Text Index feature in SQL Server allows you to run a full text search on a database table. In this article, we will learn about full-text index in SQL Server, including what full-text index is, how to create a full-text search index, and other use cases.

What is Full-Text Index in SQL Server?

Full-Text Search in SQL Server lets users and applications run full-text queries against character-based data in SQL Server tables. Full-Text index helps to perform complex queries against character data. These queries can include word or phrase searching. Before we can run full-text queries on a table, we first need to create a full-text index on the table. Only one full-text index is allowed per table, and this index can contain up to 1024 columns. The full-text index includes one or more character-based columns in the table. These columns can have any of the following data types: char, varchar, char, nvarchar, text, ntext, image, XML, or varbinary.

Full-text queries perform searches against text data in full-text indexes by operating on words and phrases based on rules of a particular language such as English or Japanese. Full-text queries can include simple words and phrases or multiple forms of a word or phrase. A full-text query returns any document that contains at least one match (also known as a hit). A match occurs when a target document contains all the terms specified in the full-text query and meets any other search conditions, such as the distance between the matching terms.

Why do we need a Full Text Index (FTI) if we can use a statement for searching?
Let us consider a scenario; I have a table with column name data. What query will we use if we want to search for the name ‘smith’ in the data column, basically, we use the command below.

The above query is efficient for the above scenario, but what if you're not looking for an exact match? FTS has some better algorithms for matching data, as does some better statistics on variations of names. Therefore, FTS can provide better performance for matching Smith, Smythe, Smithers, etc., when you look for Smith. In such a case, FTS provides better results compared to the traditional like method.
When to use FTI over the LIKE statement?

    A word or phrase close to the search word or phrase
    When the result size is several hundred thousand
    Millions of rows, each with a string like "wordAwordBwordC..."
    Any word derived from a particular root (for example, run, ran, or running)

How to Create a Full-Text Index?
Now, I will explain how to create a full-text index. But, first, we will read two methods to create the full-text index, using manually and using the SQL command.

Create Full-Text Index Manually
The following steps are performed to create the Full Text Index.
    Create a Full-Text Catalog
    Create Full-Text Index
    Populate the Index

1. Create a Full-Text Catalog
The full-text catalog is used for the full-text index. If we don’t specify the full-text catalog, then SQL Server will use the default catalog. So now we have learned how to create a full-text catalog.

To create a full-text catalog, select your database, go to the Storage folder, right-click on Full-Text Catalog, and select the New Full-Text Catalog option.

Now provide a name for the full-text catalog.

You can see that a new catalog has been created in the Storage folder.

2. Create Full-Text Index
To create a full-text index choose your table and right-click on that table and select the “ Define Full-Text Index” option.

Now select Unique Index. It is compulsory that for “Full-Text Index” table must have at least one unique index.

Select columns name and language types for columns. You can only select character-based and image-based columns.


Select change tracking.


Now select the full-text catalog for the index.


 

 

 

The last image confirms that the full-text index is created successfully. Now we populate this full-text index.

3. Populate the Index
To populate the index, right-click on the table and select the “Start Full Population” option.

 

Create Full-Text Index using SQL Command
Use the following command syntax to create the Full Text Index.

Syntax
CREATE FULLTEXT INDEX ON table_name
[ ( { column_name
[ TYPE COLUMN type_column_name ]
[ LANGUAGE language_term ]
[ STATISTICAL_SEMANTICS ]
} [ ,...n]
) ]
KEY INDEX index_name
[ ON<catalog_filegroup_option> ]
[ WITH [ ( ] <with_option> [ ,...n] [ ) ] ]
[;]

<catalog_filegroup_option>::=
{
fulltext_catalog_name
| ( fulltext_catalog_name, FILEGROUP filegroup_name )
| ( FILEGROUP filegroup_name, fulltext_catalog_name )
| ( FILEGROUP filegroup_name )
}

<with_option>::=
{
CHANGE_TRACKING [ = ] { MANUAL | AUTO | OFF [, NO POPULATION ] }
| STOPLIST [ = ] { OFF | SYSTEM | stoplist_name }
| SEARCH PROPERTY LIST [ = ] property_list_name
}

Parameters

Parameter Description
table_name Define the name of the table
column_name Define the name of the column included in the full-text index.
TYPE COLUMN type_column_name Define the type of column(exavarchar,varbinary)
LANGUAGE language_term Define the language of the data stored in column_name.
STATISTICAL_SEMANTICS Creates the additional keyphrase and document similarity indexes that are part of statistical semantic indexing.
KEY INDEX index_name Define the name of the unique key index on table_name. 
fulltext_catalog_name Define the full-text catalog used for the full-text index.
FILEGROUP filegroup_name Creates the specified full-text index on the specified filegroup. 
CHANGE_TRACKING [ = ] { MANUAL | AUTO | OFF [ , NO POPULATION ] } Specifies whether changes(updates, deletes, or inserts) made to table columns that are covered by the full-text index will be propagated by SQL Server to the full-text index.
STOPLIST [ = ] { OFF | SYSTEM | stoplist_name } Associates a full-text stop list with the index. 
SEARCH PROPERTY LIST [ = ] property_list_name  Associates a search property list with the index.

Example

CREATE FULLTEXTCATALOG New_CatalogASDEFAULT;
CREATE FULLTEXTINDEX ON dbo.Employee(EmployeeName TYPECOLUMN varchar LANGUAGE 1033,EmpSalary TYPECOLUMN varchar LANGUAGE 1033)
KEY INDEX UN_Pankaj
ON
New_Catalog


After creating the FULL Text Catalog and Full Text Index, we now learn how to use these in search queries for better performance. There are four principal T-SQL functions that allow one to interact with your Full-Text indices:

CONTAINS and  FREETEXT Method
CONTAINS and FREETEXT functions return a boolean value, meaning we could use them directly in a WHERE clause. The remaining two return a two-column table—KEY and RANK, allowing one to manage ranked searches.

FREETEXT

FREETEXT T-SQL function performs 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:

  • Separates the string into individual words based on word boundaries (word-breaking).
  • Generates inflectional forms of the words (stemming).
  • Identifies a list of expansions or replacements for the terms based on matches in the thesaurus.

Example
SELECT TOP 10 tmski.Keyword_TextFROMdbo.TblMaster_Searching_Keyword_Infotmski
WHERE
FREE TEXT(Keyword_Text,'Hotel Above')


Output


CONTAINS
CONTAINS searches for precise or fuzzy (less precise) matches to single words and phrases, words within a certain distance of one another, or weighted matches in SQL Server. It is similar to the Freetext but with the difference that it takes one keyword to match with the records, and if we want to combine other words as well in the search, then we need to provide the “and” or “or” in search.

CONTAINS can search for
    A word or phrase.
    The prefix of a word or phrase.
    A word near another word.
    A word is inflectionally generated from another (for example, the word drive is the inflectional stem of drives, drove, driving, and driven).
    A word that is a synonym of another word using a thesaurus (for example, the word "metal" can have synonyms such as "aluminum" and "steel").

Example
SELECT TOP 10 tmski.Keyword_TextFROMdbo.TblMaster_Searching_Keyword_Infotmski
WHERE
CONTAINS(Keyword_Text,'Hotel OR Above')

Use FULL-Text Index search when you have a large volume of data, and you want to perform a search for textual data columns for specific words and phrases. Full-Text Index can be used to search words, phrases, and multiple forms of a word or phrase using FREETEXT (), CONTAINS () with “and” or “or” operators (FREETEXT, CONTAINS).

HostForLIFEASP.NET SQL Server 2019 Hosting



European SQL Server 2019 Hosting :: Indexes in SQL Server

clock January 30, 2023 07:15 by author Peter

A SQL Server Index is used on a database table for faster data access. In this article, learn what SQL Server Indexes are, why we need them, and how to implement them in SQL Server.

Indexes in SQL Server
SQL Indexes are used in relational databases to retrieve data quickly. They are similar to indexes at the end of the books whose purpose is quickly finding a topic. SQL provides Create Index, Alter Index, and Drop Index commands used to create a new index, update an existing one, and delete an index in SQL Server.

  • Data is internally stored in a SQL Server database in "pages" where the size of each page is 8KB.
  • A continuous eight pages are called an "Ex. "nt."
  • When we create the table, one extent will be allocated for two tables; when it is computed, it is filled with the data. Then another time will be given, and this extent may or may not be continuous to the first extent.

Table Scan
In SQL Server, a system table with the name sysindexes contains information about indexes available on tables in the database. If a table has no index, there will be one row in the sysindexes table related to that table indicating no index on the table when you write a select statement with a condition where clause, the first SQL Server will refer to the "indid" (index id).

Columns of the "Sysindex" table determine whether or not the column on which you write the conditions has an index. When that indid columns, to get an address of the table's first extent and then searches each row of the table for the given value. This process checks the given condition with every table row, called a table scan. A drawback of table scan is that if there is no increase in rows in the table, the time taken to retrieve the data will increase, affecting performance.

Type of Indexes

SQL Server supports two types of indexes:

  • Clustered Index
  • Non-Clusterd Index.

Clustered Index in SQL Server
A B-Tree (computed) clustered index is the Index that will arrange the rows physically in the memory in sorted order.

An advantage of a clustered index is that searching for a range of values will be fast. A clustered index is internally maintained using a B-Tree data structure leaf node of the btree of the clustered Index will contain the table data; you can create only one clustered Index for a table.
How do we Retrieve the data with clustered Index?

When you write a select statement with a condition in a where clause, then the first SQL Server will refer to the "indid" columns of the "Sysindexes" table and when this column contains the value "1".

Then it indexes the table with a clustered index. In this case, it refers to the columns ." The Root" node of the B-tree of clustered index searches in the b-tree to find the leaf node that contains the first row that satisfies the given conditions and retrieves all the rows that satisfy the given situation that will be in sequence.

Insert and Update with Clustered Index

Since a clustered index arranges the rows physically in the memory in sorted order, insert and will become slow because the row must be inserted or updated in sorted order.
Finally, the page into which the row must be inserted or updated, and if free space is not available on the page, create the free space and then perform the insert, update and delete.

To overcome this problem while creating a clustering index, specify a fill factor, and when you specify a fill factor as 70, then in every page of that table, 70% will fill with data, and the remaining 30% will be left free. Since free space is available on every page, the insert and update will be fast.

Nonclustered Index in SQL Server

A nonclustered index is an index that will not arrange the rows physically in the memory in sorted order.
An advantage of a nonclustered index is that searching for the values in a range will be fast.
You can create a maximum of 999 nonclustered indexes on a table, 254 up to SQL Server 2005.
A nonclustered index is also maintained in a B-Tree data structure. Still, leaf nodes of a B-Tree of the nonclustered Index contain the pointers to the pages that contain the table data and not the table data directly.

How do we Retrieve data with a nonclustered index?
When you write a select statement with a condition in a where clause, then SQL Server will refer to the "indid" columns of the sysindexes table, and when this column contains a value in the range of 2 to 1000, then it indicates that the table has a non –clustered Index. In this case, it will refer to the columns root of the sysindexes table to get two addresses.

The root node of a B-Tree of a nonclustered index, and then search in the B-Tree to find the leaf node that contains the pointers to the rows that contain the value you are searching for and retrieve those rows.
Insert and Update with a Nonclustered Index

There will be no effect of inserting and updating with a nonclustered index because it will not arrange the row physically in the memory in sorted order.
With a nonclustered index, rows are inserted and updated at the end of the table.

Clustered Index Nonclustered Index
This will arrange the rows physically in the memory in sorted order This will not arrange the rows physically in the memory in sorted order.
This will fast in searching for the range of values. This will be fast in searching for the values that are not in the range.
Index for the table. You can create a maximum of 999 nonclustered indexes for the table.
The leaf node of 3 tiers of the clustered Index contains table data. The leaf nodes of the b-tree of the nonclustered Index contain pointers to get the included pointers with two table data and not the table data directly.

How to Create Indexes in SQL Server?
Use the create index command with the following system to create an index.
create [unique][clustered /non clusted] index :
<indexname> on <object name>(<column list>)
[include(<columnlst>)]
[with fillfactor=<n>]

By default, an index is nonclustered.

For example, the following examples create a nonclustered index on department_no of emp tables.
create index DNoINdex on Emp(DeptNo)

Simple & Composite Indexes

  • Based on the number of columns on which an index is created, indexes are classified into simple and composite indexes.
  • When indexes are created on single columns, it is called a simple index; when combined with multiple columns, it's called a composite index.

For example, the following example creates a nonclustered index in combination with the emp table's department number and job columns.
create index dnotedxi on emp(deptno asc,job desc)

Unique Index
    When an index is created using the keyword unique, it is called a unique index; you create a unique index on columns, and a unique index constraint will be created.
    If the columns on which you create a unique index contain duplicate values, then a unique index will not be created, and you will get an error.

Altering an Index

To alter an index, use an alter index command that has the following syntax:
    Alter index <ind Name> on <object Name> 
    rebuild/Recognize/Disable.

Alter the Index using the rebuild option. The Rebuild option will recreate the computer index; the recognize option will reorganize leaf nodes of the b-tree to Index. The disable option will disable the Index when it is eligible and then enable it.

For example, the following example alters the index "d" oidx" "available on the department number of columns on the emp table.
alter index DNOiDX on EMp rebuild

Getting a list of indexes
This stored procedure is used to compute a list of indexes available on a table.
sp_helpindex 'Stud'

Deleting indexes
Use the drop index' command that has the following syntax:
drop index <indexname> on <object name>

For example, the following example deletes the Index dnoidex available on the department number columns of the emp table.
drop index doindex on student

HostForLIFEASP.NET SQL Server 2019 Hosting



European SQL Server 2019 Hosting :: Temporal Tables In SQL Server

clock January 26, 2023 07:28 by author Peter

What is Temporal Table?
A system-versioned temporal table is a type of table in SQL Server that automatically tracks the historical changes to the data in the table, by keeping a separate history table that stores all the previous versions of the data. This allows you to easily see how the data has changed over time and also enables you to perform temporal queries to retrieve the data as it existed at a specific point in time. The system-versioning is done by using two columns, one for the start date and one for the end date, that keep track of when the data was valid. The system manages this automatically and transparently to the user.

You can also create a temporal table by specifying the Transact-SQL statements directly, as shown in the example below. Note that the mandatory elements of every temporal table are the PERIOD definition and the SYSTEM_VERSIONING clause with a reference to another user table that will store historical row versions:

CREATE TABLE [dbo].[AppUsers]
    (
        [UserID] int NOT NULL PRIMARY KEY CLUSTERED
      , [UserName] nvarchar(100) NOT NULL
      , [PagesVisited] int NOT NULL
      , [ValidFrom] datetime2 (0) GENERATED ALWAYS AS ROW START HIDDEN
      , [ValidTo] datetime2 (0) GENERATED ALWAYS AS ROW END HIDDEN
      , PERIOD FOR SYSTEM_TIME (ValidFrom, ValidTo)
     )
     WITH
     (
        SYSTEM_VERSIONING = ON
        (
            HISTORY_TABLE = dbo.AppUsersArchive,
            HISTORY_RETENTION_PERIOD = 2 MONTHS
        )
     );

ALTER TABLE AppUsers
    ADD
        ValidFrom datetime2 (2) GENERATED ALWAYS AS ROW START HIDDEN
            constraint DF_ValidFrom DEFAULT DATEADD(second, -1, SYSUTCDATETIME())
        , ValidTo datetime2 (2) GENERATED ALWAYS AS ROW END HIDDEN
            constraint DF_ValidTo DEFAULT '9999.12.31 23:59:59.99'
        , PERIOD FOR SYSTEM_TIME (ValidFrom, ValidTo);

    ALTER TABLE AppUsers
        SET (SYSTEM_VERSIONING = ON (HISTORY_TABLE = dbo.AppUsersArchive));

Advantage of Temporal Table

The main advantage of using a system-versioned temporal table is the ability to easily track and analyze changes to the data over time. Some specific benefits include:

  • Auditing: Temporal tables can be used to track changes to the data, making it easier to identify who made a change and when.
  • Point-in-time reporting: You can retrieve the state of the data at a specific point in time, which is useful for generating historical reports or analyzing trends over time.
  • Data recovery: If data is accidentally deleted or modified, you can easily recover the previous version of the data.
  • Improved data integrity: By keeping a separate history table, temporal tables can help ensure that historical data is not accidentally modified or deleted.
  • Improved performance: By keeping a separate history table, temporal tables can help improve query performance for certain types of queries that retrieve historical data.

It's worth noting that temporal tables are only available in SQL Server 2016 and later.

Temporal Table Considerations and Limitations

There are some considerations and limitations to be aware of when working with temporal tables, due to the nature of system-versioning:

  • A temporal table must have a primary key defined in order to correlate records between the current table and the history table, and the history table can't have a primary key defined.
  • The SYSTEM_TIME period columns used to record the ValidFrom and ValidTo values must be defined with a datatype of datetime2.
  • By default, the history table is PAGE compressed.
  • While temporal tables support blob data types, such as (n)varchar(max), varbinary(max), (n)text, and image, they'll incur significant storage costs and have performance implications due to their size. As such, when designing your system, care should be taken when using these data types.
  • History table must be created in the same database as the current table. Temporal querying over linked servers isn't supported.
  • History table can't have constraints (primary key, foreign key, table or column constraints).
  • TRUNCATE TABLE isn't supported while SYSTEM_VERSIONING is ON.
  • Direct modification of the data in a history table isn't permitted.
  • INSTEAD OF triggers aren't permitted on either the current or the history table to avoid invalidating the DML logic. AFTER triggers are permitted only on the current table. 
  • They're blocked on the history table to avoid invalidating the DML logic.

Hope the article would have helped you in understanding Temporal tables.

HostForLIFEASP.NET SQL Server 2019 Hosting



European SQL Server 2019 Hosting :: Dynamically Getting Database Collection From SQL Server Using C#

clock January 24, 2023 10:54 by author Peter

In this article, we will see how to dynamically get a database List using C#.

Step 1. Used Namespaces
using System.Web.Configuration;
using System.Data.SqlClient;
using Microsoft.SqlServer.Management.Common;
using Microsoft.SqlServer.Management.Smo;


Step 2. Referenced DLL

Step 3. Connection String:
<add name="<KEY>" connectionString="Data Source=<SERVERNAME>;Initial Catalog=<DATABASE>;Integrated Security=True" />

Step 4. Used to get Connection String from Web.config file.
public static string GetConnectionStringFromWebConfigByName(string name)
{
    return WebConfigurationManager.ConnectionStrings[name].ConnectionString;
}

Step 5. Usage
DatabaseCollection databases =  SQLDatabaseClass .DbCollection(connectionString);

Here you can get the collection of a database as a List.

Database database =  SQLDatabaseClass.SinglDatabase(connectionString);

Here you can get a single database by its name.

Step 6. Creating a Connection String for server connection.
public static SqlConnection Connecection(string connectionString)
{
    SqlConnection con = new SqlConnection(connectionString);
    return con;
}


Step 7. Create a Server connection using a Connection string.
public static ServerConnection GetServerConnection(string connectionString)
{
    ServerConnection serverCon = new ServerConnection(Connecection(connectionString));
    serverCon.Connect();
    return serverCon;
}


Step 8. Creating Server Object.
public static Server GetServer(string connectionString)
{
    Server server = new Server(GetServerConnection(connectionString));
    return server;
}


Step 9. Getting Database collection from the SQL Server:
public static DatabaseCollection DbCollection(string connectionString)
{
    Server server = GetServer(connectionString);
    return server.Databases;
}

Step 10. Getting a specific database from the collection of databases in the SQL Server:
public static Database SinglDatabase(string connectionString, string databaseName)
{
    return GetServer(connectionString).Databases[databaseName];
}


Code Snippet
class SQLDatabaseClass
{
    #region Database

    public static DatabaseCollection DbCollection(string connectionString)
    {
        Server server = GetServer(connectionString);
        return server.Databases;
    }

public static Database SinglDatabase(string connectionString, string Name)
{
    return GetServer(connectionString).Databases[databaseName];
}

#endregion

public static Server GetServer(string connectionString)
{
    Server server = new Server(GetServerConnection(connectionString));
    return server;
}

public static ServerConnection GetServerConnection(string connectionString)
{
    ServerConnection serverCon = new ServerConnection(Connecection(connectionString));
    serverCon.Connect();
    return serverCon;
}

public static ServerConnection GetServerConnectionByLogin(bool isWindows, string serverName)
    {
        ServerConnection serverCon = new ServerConnection();
        serverCon.LoginSecure = isWindows;
        serverCon.ServerInstance = serverName;
        serverCon.Connect();
        return serverCon;
    }

    public static SqlConnection Connecection(string connectionString)
    {
        SqlConnection con = new SqlConnection(connectionString);

        return con;
    }
}

Thanks for reading this article. I hope you have a nice day.

HostForLIFEASP.NET SQL Server 2019 Hosting

 



European SQL Server 2019 Hosting :: What Is MDF and LDF in SQL Server?

clock January 16, 2023 06:19 by author Peter

If you work with a SQL Server database, I'm sure you have heard of .mdf and .ldf files. When you install a new SQL Server database server on a machine, and create a database, these files are created on a hard drive. In this blog, let's learn about mdf and ldf files.

MDF - Main Database File

    It contains all the main information of the database that is part of the server.
    It plays a crucial role in information storage.

Note - All the successful queries go in MDF.

LDF - Log Database File

    It stores information related to transaction logs for the main data file.
    It stores changes related to CRUD Insert, Delete, and Update.

Note - All the unsuccessful or stuck queries go in LDF.

IMP Image Note
The below image shows the .mdf and .ldf files of EmployeeDB Database.

Difference between MDF and LDF

MDF file is the primary file in SQL server database. The LDF is a supporting file.
MDF contains database record data.  Records information related to changes made in the server as well as all the actions performed.
MDF can vary in its file size with the change of the table and record data. LDF files can go on to consume a lot of storage space depending on the number of changes made in the server as well as the number of transactions that took place. 

HostForLIFEASP.NET SQL Server 2019 Hosting

 



European SQL Server 2019 Hosting :: What Is Common Table Expression (CTE) In SQL Server?

clock January 13, 2023 06:31 by author Peter

Common Table Expression (CTE) in SQL offers a more readable form of a derived table. A Common Table Expression is an expression that returns a temporary result set. This result set is similar to a hybrid Derived Table. The resultset can be declared once and referenced multiple times in a query. It does not require any extra effort to declare it. CTE is more powerful than the derived table. It can self-reflect, and we can also use CTE multiple times in the same query. Mainly, CTE improves readability and makes it easy to maintain complex queries. CTE can be used for selects, and DML (Insert, Update, and Delete) statements.

Common Structure of CTE 

;WITH CTE_name [ ( column_name [,...n] ) ]
AS
(
query_definition
)
select * from CTE_name;

The Common Table Expression is created using the WITH statement followed by the CTE name and List of Columns (specifying a column is optional). After the "AS," the information used to populate the returning columns begins. The CTE is then followed by a select calling it. Always start CTE with a semi-colon.

Example

Step 1. Create a query 

The following is a sample of creating two tables, EmployeeMasters and DepartmentMasters, and inserting some default values into them.

IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[DepartmentMasters]') AND type in (N'U'))
DROP TABLE [dbo].[DepartmentMasters]
CREATE TABLE [dbo].[DepartmentMasters](
[DepartmentId] [int] IDENTITY(1,1) NOT NULL,
[DepartmentCode] [varchar](50) NULL,
[DepartmentName] [varchar](50) NULL,
CONSTRAINT [PK_DepartmentMasters] PRIMARY KEY CLUSTERED
(
[DepartmentId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[EmployeeMasters]') AND type in (N'U'))
DROP TABLE [dbo].[EmployeeMasters]
CREATE TABLE [dbo].[EmployeeMasters](
[EmployeeId] [int] IDENTITY(1,1) NOT NULL,
[EmployeeName] [varchar](50) NULL,
[EmployeeCode] [varchar](50) NULL,
[DepartmentId] [int] NULL,
CONSTRAINT [PK_EmployeeMasters] PRIMARY KEY CLUSTERED
[EmployeeId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
ALTER TABLE [dbo].[EmployeeMasters] WITH CHECK ADD CONSTRAINT [FK_EmployeeMaster_DepartmentMaster] FOREIGN KEY([DepartmentId])
REFERENCES [dbo].[DepartmentMasters] ([DepartmentId])
GO
ALTER TABLE [dbo].[EmployeeMasters] CHECK CONSTRAINT [FK_EmployeeMaster_DepartmentMaster]
GO
INSERT INTO DepartmentMasters VALUES
('Eaxm', 'Examination'),
('Staff', 'Exam Staff')
INSERT INTO EmployeeMasters VALUES
('Peter','D0093',1),
('Scott','D0094',1),
('Mike','D0095',1),
('Friedrich','D0096',2),
('Kenneth','D0097',2)

Step 2. Writing CTE Query

The following is a sample use of a CTE Query.

;WITH emp_detail(EmployeeName,EmployeeCode,DepartmentCode)
AS
(
SELECT e.EmployeeCode,e.EmployeeName,d.DepartmentCode FROM EmployeeMasters e JOIN DepartmentMasters d ON e.DepartmentId=d.DepartmentId
)
SELECT * FROM emp_detail;

 

When to Use CTE in SQL?

CTE offers the same functionality as a View (ideal for one-off usages). CTE provides the following four advantages.

  • Create a recursive query. 
  • Alternative from a view when the general use of an idea is not required, a case in which you do not have to store the definition in metadata.
  • Enable grouping by a column derived from a scalar subselect or a function that is either not deterministic or has external access.
  • Reference the resulting table multiple times in the same statement. 

CIt can not use with CTE

  • The clauses like ORDER BY, INTO, OPTION clause with query hints, FOR XML, and BROWSE cannot be used in the CTE query definition.
  •  "SELECT DISTINCT," GROUP BY, PIVOT, HAVING, Scalar aggregation, TOP, LEFT, RIGHT, OUTER JOIN, and Subqueries "are not allowed in the CTE query definition of a recursive member.
  •  A CTE can be self-referencing and previously defined CTEs in the same WITH clause. Forward referencing is not allowed.
  • Specifying more than one "WITH" clause in a CTE is prohibited. For example, if a CTE query definition contains a subquery, then that subquery cannot have a nested WITH clause to define other CTE. 

Summary
CTE provides a more readable and usable approach to derived tables. CTE is not materialized into a work table (temporary table). CTEs are not a replacement for temporary Tables. The scope of the CTE is limited to the first SELECT statement only.

HostForLIFEASP.NET SQL Server 2019 Hosting

 



European SQL Server 2019 Hosting :: Everything You Need To Know About SQL Server TempDB

clock January 4, 2023 07:37 by author Peter

SQL Server TempDB is a special database in SQL Server that stores temporary objects. It allows you to partition your workloads such that frequently accessed data is stored in memory and less frequently accessed data can be stored on disk, reducing contention for resources. It not only provides you temp storage but also helps improve the performance of the database queries and reduces workloads of the front-end applications as well.

You rarely use TempDB directly, but it has many functions behind the scenes and it is always in use by SQL Server to ensure the performance and responsiveness of some system and non-system databases. For example, if a database operation is too large that needs more memory and storage than the database server currently has, the server uses TempDB as the temp storage to help execute the operation. You can think of TempDB as virtual memory and storage on the OS level that is used when Windows OS needs extra memory and storage.

What is SQL Server TempDB

    TempDB is a system database used by SQL Server (and other RDBMS).
    Apps requiring heavy sorting, grouping, etc. can use TempDB to offload temporary data.
    TempDB files are recreated every time SQL Server starts, so the data in them will be lost after a restart.
    You can use TempDB to store your custom tables, query results, views, and variables.

What is stored in TempDB?
TempDB is a system database. Its name is derived from the fact that it stores temporary user objects. These are tables, stored procedures, table variables, cursors, or derived tables that contain intermediate results when processing queries.

SQL Server uses the TempDB database for various purposes such as the storage of temporary user objects like tables, temporary stored procedures, table variables, cursors, or derived tables that contain intermediate results when processing queries and for internal SQL Server system objects such as row versioning information.

This database is used for sorting and grouping large amounts of data during the execution of a query. It can also be used for storing rows returned by an INSERT statement in batches (one batch per thread). The size of each row varies but is usually 8 KB or 16 KB for small tables and large rows respectively. For example: If you are inserting a million rows into your table with 100 columns each then you would have 100 million pages in tempdb.*

How to access TempDB?
The TempDB is a system database and in automatically created when a SQL Server is installed on a machine. You can access TempDB object by executing queries on TempDB or using SSMS.

Let’s look at the SSMS system databases installed by default when a new SQL Server is installed. As you can see from the following image, four system databases are installed by default when a new SQL Server is installed, master, mode, msdb, and tempdb.

If you expand tempdb database, you will see Tables, Views etc. However, until you start working with databases, they all will be empty.

Where is TempDB location?
The default location of tempdb database is the data folder same where other system databases are. If you right click on tempdb in SSMS, select Properties and select Files, you can find the exact location of tempdb.mdf and other supporting files. The templog file is also in the same folder.

How to move TempDB to a Different Drive or Folder?
Often times, we do not want to store tempdb data and log files on our main drive. You can change the default location of tempdb data and log files by moving them to another drive or folder using the ALTER DATABASE statement.

Run the following command on tempdb.
ALTER DATABASE [tempdb] MODIFY FILE ( NAME = N'tempdev', FILENAME = N'D:\tempdbstorage\tmp.mdf');
GO
ALTER DATABASE [tempdb] MODIFY FILE ( NAME = N'templog', FILENAME = N'D:\tempdbstorage\tmplog.ldf');
GO

Once you execute the above queries, Stop and Restart SQL Server service to make this change in effect.

Also, don’t forget to delete old tempdb.mdf and templog.ldf files.

How to create a Temporary Table in TempDB?

We can also use tempdb explicitly by createng tables, views etc on a tempdb. We can also create regular table as well as temp tables. The following queries create two tables in tempdb, CSharpCorner and Csharp2.
CREATE TABLE CSharpCorner (ID int, data text);
CREATE TABLE #CSharp2 (ID int, name text);


Select tempdb in SSMS and execute above queries in query explorer on tempdb. Right click on tempdb, select New Query, type above SQL statements and execute.

Now refresh tempdb, Right click on tempdb, select Refresh.

Expand Tables and Temporary Tables nodes.

You will CSharpCorner tabe and CSharp2 temporaty table in the database.

To learn how to work with temproraty tables, read Working with Temporaty Tables in SQL Server.

The tempdb is also used to store Temporary Variables. Temporary Variables are used to store data temporarily in tempdb.

Apps requiring heavy sorting, grouping etc. can use TempDB to offload temporary data
You can use TempDB to offload temporary data from the transactional tables. For example, if you are writing an OLTP application that requires heavy sorting and grouping operations, you can use TempDB to store these temporary results.

Sorting or grouping is a common operation when you are processing large amounts of data in a relational database. However, sorting is one of the most expensive operations in traditional databases because it involves disk I/O as well as CPU utilization by the query processor (SQL Server).
TempDB files are recreated every time SQL Server starts.

The TempDB is a system database. It's not specific to any SQL Server instance, but it's always created when you start SQL Server.

For this reason, TempDB is recreated every time SQL Server starts. This happens regardless of whether it was dropped or not, and also if the files are manually deleted (but not using RECREATE).

If TempDB is on an SSD it performs better compared with HDD
If the TempDB database is on an SSD, it performs better compared with a HDD. SSDs are faster, more reliable and durable than HDDs, but they are also more expensive.
TempDB should be one single file per core - up to 8 cores.

SQL Server recommends that TempDB be one single file per core - up to 8 cores. The number of cores in a server can be determined by using the following query:
SELECT * FROM sys.dm_os_sys_info WHERE name='max degree of parallelism'

To determine the number of associated data files for an instance, you can use this script:
select * from sys.master_files

You can monitor TempDB usage using sys.dm_db_task_space_usage and sys.dm_exec_query_resource_semaphores DMVs.

In general, you can monitor TempDB usage using sys.dm_db_task_space_usage and sys.dm_exec_query_resource_semaphores DMVs.
    sys.dm_db_task_space_usage: This DMV returns information about the memory used by all user processes that are connected to the instance of SQL Server. The information includes a breakdown of how much memory each process is using in its buffers, as well as whether any allocations have been deferred or not yet committed by a specific session.
    sys.dm_exec
    exec (sqlserver) This function returns a recordset object that represents an execution plan for an SQL statement compiled with the specified options and parameter values—or no such plan if there is none available because of insufficient system resources or incompatible settings on this instance of SQL Server2005 2005 Management Studio (SSMS).

How to distribute TempDB files for better performance?
When multiple tempdb files are used, make sure they are evenly distributed across different physical disks to improve performance and reliability (e.g., by putting the tempdb files on separate physical disks).

When monitoring the performance of SQL Server, look at the disk I/O subsystem performance. If a disk is reached its maximum throughput, it becomes the bottleneck for SQL Server's workload. If you experience lower than expected CPU utilization, it may indicate that your I/O subsystem is limiting your system’s overall performance instead of CPU resources being consumed by other tasks such as data compression or encryption operations running concurrently with other workloads.

HostForLIFEASP.NET SQL Server 2019 Hosting



About HostForLIFE

HostForLIFE is European Windows Hosting Provider which focuses on Windows Platform only. We deliver on-demand hosting solutions including Shared hosting, Reseller Hosting, Cloud Hosting, Dedicated Servers, and IT as a Service for companies of all sizes.

We have offered the latest Windows 2019 Hosting, ASP.NET 5 Hosting, ASP.NET MVC 6 Hosting and SQL 2019 Hosting.


Month List

Tag cloud

Sign in