Introduction We focused on optimizing database performance and manageability, it’s important to understand the nuances of table partitioning in SQL Server, including partition switching. Partition switching is a feature in SQL Server that allows for fast data movement between tables and partitions. This blog explores the different types of partition switching and their applications in SQL Server. What is Partition Switching? Partition switching involves moving data between partitions or between a partition and a non-partitioned table without physically copying the data. Instead, metadata pointers are updated, making the operation extremely fast and efficient. This is especially useful for data archiving, loading new data, and maintaining large datasets. Types of Partition Switching 1. Switching Between Partitions in the Same Table Switching data between partitions within the same table can be useful for reorganizing data or when performing operations that require temporary partition rearrangement. Example: Suppose you have a table SalesData partitioned by month and you need to move data from one month to another. -- Switch data from partition 2 to partition 3 ALTER TABLE SalesData SWITCH PARTITION 2 TO SalesData PARTITION 3; 2. Switching Between a Table and a Partitioned Table This type of switching is typically used for bulk loading or removing data. You can switch a partition of a partitioned table to a non-partitioned table (and vice versa) to quickly load or archive data. Example: Loading new data into a partitioned table SalesData from a staging table StagingSalesData. -- Ensure the staging table matches the schema of the partitioned table CREATE TABLE StagingSalesData ( SaleID int, SaleDate datetime, Amount money ); -- Switch the staging table data into the partition ALTER TABLE StagingSalesData SWITCH TO SalesData PARTITION 1; 3. Switching Between a Partitioned Table and Another Partitioned Table This involves moving data between two different partitioned tables. It’s useful when dealing with different data lifecycle management scenarios, such as archiving old data into a separate historical table. Example: Switching data from a partition in CurrentSalesData to a partition in HistoricalSalesData. -- Both tables should have the same structure and partition scheme ALTER TABLE CurrentSalesData SWITCH PARTITION 2 TO HistoricalSalesData PARTITION 1; 4. Switching Data Out of a Partitioned Table This is used to remove data from a partitioned table and move it into a non-partitioned table for further processing or archiving. Example: Switching data from a partition in SalesData to a table OldSalesData. -- Ensure the target table matches the schema of the partitioned table CREATE TABLE OldSalesData ( SaleID int, SaleDate datetime, Amount money ); -- Switch the data out of the partition ALTER TABLE SalesData SWITCH PARTITION 1 TO OldSalesData; Guidelines for Partition Switching To ensure smooth partition switching, consider the following guidelines: Schema Matching: Ensure that the schemas of the source and target tables match exactly, including constraints and indexes. Partition Alignment: The source and target partitions must align correctly based on the partition function. Check Constraints: Check constraints on the tables must be consistent with the partition boundary conditions. Minimal Indexes: Avoid using non-aligned indexes on partitioned tables to ensure efficient switching. Benefits of Partition Switching Performance Efficiency: Since partition switching involves metadata operations rather than physical data movement, it is extremely fast and efficient. Minimal Downtime: Enables quick data loading, archiving, and reorganization with minimal downtime. Data Management Flexibility: Facilitates flexible data management strategies, allowing for efficient data lifecycle management. Conclusion Partition switching is a powerful feature in SQL Server that enhances the performance and manageability of large datasets. Understanding the different types of partition switching and their applications allows you, to implement efficient data loading, archiving, and maintenance strategies. By leveraging partition switching, you can ensure that your SQL Server environment remains robust, responsive, and well-organized, ultimately supporting your organization’s data management goals.
Introduction Overseeing data management and performance optimization, implementing table partitioning in Microsoft SQL Server is a strategic decision to enhance database performance and manageability. Table partitioning is a powerful technique that allows large tables to be divided into smaller, more manageable pieces, improving query performance and simplifying maintenance tasks. In this blog, we'll explore the concept of table partitioning, its benefits, and a step-by-step guide to implementing it in SQL Server. Understanding Table Partitioning Table partitioning involves dividing a large table into smaller, more manageable segments called partitions. Each partition can be managed and accessed independently, which can significantly improve query performance and simplify maintenance tasks. Partitioning is especially beneficial for large tables with millions or billions of rows, where operations such as data loading, archiving, and querying can become cumbersome. Key Concepts Partition Function: Defines how data is distributed across partitions based on a specified column or columns. Partition Scheme: Maps the partitions defined by the partition function to specific filegroups within the database. Aligned Indexes: Indexes that are partitioned in the same way as the table, ensuring that queries using these indexes benefit from partitioning. Benefits of Table Partitioning Improved Query Performance: Queries that target specific partitions can avoid scanning the entire table, resulting in faster response times. Parallel processing of partitions can enhance performance for complex queries. Simplified Maintenance: Partition-level operations such as loading, archiving, and deleting data can be performed independently, reducing the impact on overall database performance. Easier management of large tables, as partitions can be individually managed and optimized. Enhanced Data Management: Partitioning can facilitate better data organization and management, such as separating historical data from current data. Efficient handling of data purging and archiving processes. Types of Table Partitions in SQL Server 1. Range Partitioning Range partitioning is the most common type of partitioning in SQL Server. It involves dividing a table based on a range of values in a specified column, often a date or numerical column. Each partition holds data that falls within a specific range. Use Cases: Partitioning data by date to manage historical data efficiently. Improving query performance for range-based queries. Example: CREATE PARTITION FUNCTION rangePartitionFunction (datetime) AS RANGE LEFT FOR VALUES ('2021-01-01', '2022-01-01', '2023-01-01'); CREATE PARTITION SCHEME rangePartitionScheme AS PARTITION rangePartitionFunction TO (fg1, fg2, fg3, fg4); CREATE TABLE SalesData ( SaleID int, SaleDate datetime, Amount money ) ON rangePartitionScheme (SaleDate); 2. List Partitioning List partitioning allows you to divide a table based on a list of values. Each partition is associated with specific values of a column, often used for categorizing data by discrete values such as regions or departments. Use Cases: Partitioning data by specific categories (e.g., regions, product types). Enhancing query performance for category-based queries. Example: CREATE PARTITION FUNCTION listPartitionFunction (nvarchar(20)) AS RANGE LEFT FOR VALUES ('North', 'South', 'East', 'West'); CREATE PARTITION SCHEME listPartitionScheme AS PARTITION listPartitionFunction TO (fg1, fg2, fg3, fg4); CREATE TABLE SalesRegionData ( SaleID int, Region nvarchar(20), Amount money ) ON listPartitionScheme (Region); 3. Composite Partitioning Composite partitioning combines two or more partitioning strategies. The most common combination is range-list or range-hash partitioning. This approach allows for more complex and flexible data distribution strategies. Use Cases: Managing large datasets with multiple logical divisions. Enhancing performance and manageability for complex queries. Example: -- Range-List Partitioning Example CREATE PARTITION FUNCTION rangePartitionFunction (datetime) AS RANGE LEFT FOR VALUES ('2021-01-01', '2022-01-01', '2023-01-01'); CREATE PARTITION FUNCTION listPartitionFunction (nvarchar(20)) AS RANGE LEFT FOR VALUES ('North', 'South', 'East', 'West'); Choosing the Right Partitioning Strategy Selecting the appropriate partitioning strategy depends on several factors, including data characteristics, query patterns, and maintenance requirements. Here are some guidelines to help you choose: Range Partitioning: Best for time-series data or data with natural ranges. Ideal for scenarios where you frequently query specific ranges of data. List Partitioning: Suitable for categorical data with a limited number of discrete values. Useful for scenarios where queries target specific categories. Composite Partitioning: Best for complex data structures that require multiple partitioning dimensions. Ideal for large datasets with varied query patterns and maintenance needs. Implementing Table Partitioning in SQL Server Step 1: Planning and Design Identify Candidate Tables: Analyze your database to identify large tables that will benefit from partitioning. Consider factors such as table size, query patterns, and data lifecycle. Choose Partitioning Column: Select a column that will be used to distribute data across partitions, often based on date or range values. Ensure the column has a high degree of cardinality to evenly distribute data. Step 2: Creating a Partition Function Define the Partition Function: Create a partition function that specifies the boundaries for each partition. CREATE PARTITION FUNCTION myPartitionFunction (int) AS RANGE LEFT FOR VALUES (1000, 2000, 3000); Step 3: Creating a Partition Scheme Map Partitions to Filegroups: Create a partition scheme that maps each partition to a specific filegroup. CREATE PARTITION SCHEME myPartitionScheme AS PARTITION myPartitionFunction TO (fg1, fg2, fg3, fg4); Step 4: Creating a Partitioned Table Create the Table Using Partition Scheme: Create the partitioned table and specify the partition scheme. CREATE TABLE myPartitionedTable ( id int, data nvarchar(100), partition_column int ) ON myPartitionScheme (partition_column); Step 5: Managing Indexes on Partitioned Tables Create Aligned Indexes: Ensure indexes are partitioned in the same way as the table. CREATE INDEX idx_myPartitionedTable ON myPartitionedTable (partition_column) ON myPartitionScheme (partition_column); Step 6: Maintaining Partitioned Tables Data Management: Use partition-level operations for data loading, archiving, and purging. Utilize partition switching to efficiently move data between tables. Monitoring and Optimization: Regularly monitor partition performance and manage storage distribution. Rebuild or reorganize partitions as needed to maintain optimal performance. Conclusion Implementing table partitioning in Microsoft SQL Server is a powerful strategy for improving database performance and manageability, especially for large tables. Guiding your team through the careful planning and implementation of partitioning can lead to significant performance gains and simplified maintenance processes. By following the steps outlined in this blog, you can ensure a successful partitioning implementation that enhances your organization's data management capabilities. Table partitioning is not just a technical enhancement; it's a strategic move towards better data management and performance optimization. Embrace this powerful feature to keep your SQL Server environment robust and responsive.
Introduction The migration of an on-premise report server to Azure SQL Managed Instance requires strategic planning and meticulous execution. This transition offers numerous benefits, including scalability, reliability, and reduced maintenance overhead. In this blog, we'll explore the essential steps involved in migrating an on-premise report server to Azure SQL Managed Instance, ensuring a seamless transition for your organization. Understanding Azure SQL Managed Instance Before diving into the migration process, let's briefly understand Azure SQL Managed Instance. It is a fully managed platform as a service (PaaS) offering from Microsoft Azure, providing near-complete compatibility with on-premise SQL Server. Managed Instance offers features like automatic patching, automated backups, and built-in high availability, making it an attractive option for hosting SQL Server workloads in the cloud. Pre-Requisites 1. Azure SQL Managed Instance 2. SQL Server User Account – Using to connect Azure SQL Managed Instance 3. Azure Virtual Machine Configure Azure SQL Managed Instance 1. Go to Azure Portal and search for Azure SQL Managed Instance. 2. Set up the username and password, it will require connecting from SSMS and SSRS later. 3. Set up the required configuration. 4. Create the Azure SQL. 5. Create a new database (optional). 6. Open SSMS and verify the instance connection with SQL Server Authentication by entering a username and password of #2. 7. If it’s connecting successfully then we have configured Azure SQL Managed Instance correctly. Configure Azure Virtual Machine 1. Go to Azure Portal and search for Virtual Machine. 2. Select the Windows Operating System and set up the required configurations. 3. Create a Virtual Machine and connect via RDP. Install SSRS (SQL Server Reporting Services) in Azure VM 1. Connect your Azure VM using RDP. 2. Download the 2022 SSRS installer - Click here to download 3. Launch the installer of 2022 SSRS. 4. Choose Install Reporting Services and click Next. 5. Choose the appropriate Edition to match your licensing. Once selected choose Next. 6. Now you will want to accept the license and click Next. 7. Choose Install Reporting Services Only and click Next. 8. Change the Installation Location to a path of your choice, if you would like, then click Install. 9. Open Report Server Configuration Manager and click on Connect. 10. Start the Report Service if it’s not started. Connect On-Premises SQL Server 1. Connect to your on-premises SQL Server. 2. Take a backup of your ReportServer and ReportServerTempDB databases. 3. After successfully backup of both databases, upload it to Azure Blob Storage. Connect Azure SQL Managed Instance in SSMS 1. Connect your Azure SQL Managed Instance with your credentials. 2. Generate SAS Token to access Azure Blob Storage account. 3. Create new Credentials in SQL Managed Instance. CREATE CREDENTIAL [AZURE BLOB URL WITH CONTAINER/FOLDER] WITH IDENTITY = 'SHARED ACCESS SIGNATURE', SECRET = 'SAS TOKEN' ; GO 4. Restore ReportServer and ReportServerTempDB Databases RESTORE DATABASE ReportServer FROM URL = 'AZURE BLOB URL OF DATABASE BACKUP FILE' ; GO RESTORE DATABASE ReportServerTempDB FROM URL = 'AZURE BLOB URL OF DATABASE BACKUP FILE' ; GO 5. Delete old record from ReportServer.dbo.Keys table based on MachineName or InstanceName. (DELETE ReportServer.[dbo].[Keys] WHERE MachineName = 'OLD MACHINE NAME') 6. To view all subscriptions in the new server execute the below query. DECLARE @OldUserID uniqueidentifier DECLARE @NewUserID uniqueidentifier SELECT @OldUserID = UserID FROM dbo.Users WHERE UserName = 'OLD SERVER NAME WITH USER' SELECT @NewUserID = UserID FROM dbo.Users WHERE UserName = 'NEW SERVER NAME WITH USER' UPDATE dbo.Subscriptions SET OwnerID = @NewUserID WHERE OwnerID = @OldUserID 7. Restart SQL Server Reporting Service. 8. Open the Report Server in the browser to verify all the Reports and Subscriptions. Configure SSRS (SQL Server Reporting Services) in Azure VM 1. Connect your Azure VM using RDP. 2. Open Report Server Configuration Manager and click on Connect. 3. Start the Report Service if it’s not started. 4. Go to Database and click on Change Database. 5. Choose existing database option and click on Next. 6. Enter the database connection information of Azure SQL Managed Instance, Test the connection and click on Next. – IMPORTANT 7. Inside credentials, choose SQL Server Credentials option and, enter username and password of Azure SQL Managed Instance and click on Next. 8. Please verify the SQL Server Instance Name and other details in Summary and click on Next. 9. Click on Finish. 10. In Report Configuration Manager and select Web Service URL, then click Apply. 11. Go to Web Portal URL, then click Apply. 12. Go to E-mail Settings, update your email settings to send report subscription emails. 13. Open browser and enter your report server Web Portal URL.
Scenario: If someone say you Hey, can you transfer one of MySQL data to another MySQL data and we think about SSIS or other Thing if yes then these article made for you to reduce your effort and save your time Introduction: In the dynamic landscape of database management, the need to seamlessly access and integrate data from multiple sources has become paramount. Whether it's consolidating information from disparate servers or synchronizing databases for backup and redundancy, MySQL offers a robust solution through its querying capabilities. In this guide, we delve into the art of fetching data from one MySQL server to another using SQL queries. This method, often overlooked in favor of complex data transfer mechanisms, provides a streamlined approach to data migration, enabling developers and database administrators to efficiently manage their resources. Through a combination of MySQL's versatile querying language and the innovative use of the FEDERATED storage engine, we'll explore how to establish connections between servers, replicate table structures, and effortlessly transfer data across the network. From setting up the environment to executing queries and troubleshooting common challenges, this tutorial equips you with the knowledge and tools to navigate the intricacies of cross-server data retrieval with ease. As we know We gonna use FEDERATED feature of MySQL workbench so first we need to check that our workbench support FEDERATED engine or not? Simply open workbench and run below code show engines; It shows all engines and check our system support FEDERATED OR NOT If your system also not support don't worry we gonna enable it Open your folder where you save MySQL serve file In my case it in my C drive C>ProgramData>MySQL>MySQL Server 8.0>my.ini open it in notepad++ or preferable software Insert FEDERATED key word in script like below Now need to restart MySQL Press Window+R button and paste services.msc press ok> find MySQL and restart it Now go to workbence and run show engines; code Now your FEDERATED engine get supported It show like below Now our system Support FEDERATED engine This same process need to apply on destination side because both server (from source to destination server) need to support FEDERATED engine Now we make sure to we have permission of access source server for that we need to make user and and give permission of database and tables Below code demonstrate to make user and give permission to user CREATE USER 'hmysql'@'192.168.1.173' IDENTIFIED BY 'Hardik...'; GRANT ALL PRIVILEGES ON *.* TO 'hmysql'@'192.168.1.173' WITH GRANT OPTION; FLUSH PRIVILEGES; Now make connection of that user(we make above on source side) on destination server(our system) Click on plus(+) icon as shown in image and fill all detail Below image is for detail of user connection After filling details our user added like below image Go to user(hardikmysql) and find from which table we want to take data using MySQL query Here i am taking 'actor' table from 'sakila' database which look like below Now we need to run FEDERATED query on our system(destination server) with url string Our MySQL query like below CREATE TABLE `actor` ( `actor_id` smallint unsigned NOT NULL AUTO_INCREMENT, `first_name` varchar(45) NOT NULL, `last_name` varchar(45) NOT NULL, `last_update` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY (`actor_id`), KEY `idx_actor_last_name` (`last_name`) ) ENGINE=FEDERATED default charset=utf8mb4 CONNECTION='mysql://hmysql:[email protected]:3306/sakila/actor'; Here main part is below ENGINE=FEDERATED default charset=utf8mb4 CONNECTION='mysql://hmysql:[email protected]:3306/sakila/actor'; Here 'mysql' is mandatory for connection string you can not use other word. 'hmysql' is user name 'Hardik...' is password for user '192.168.1.173' is server adderess '3306' is port number 'sakila' is database name 'actor' is table name Now run above table code and you get data in our system(destination server)
If you want to have a list of constraints applied on a particular table in the SQL server, this will help you to get it in one go. DECLARE @TABLENAME VARCHAR(50) = '<table_name>' SELECT ObjectName ,TypeOfObject ,TypeOfConstraint ,ConstraintName ,ConstraintDescription FROM ( SELECT schema_name(t.schema_id) + '.' + t.[name] AS ObjectName ,CASE WHEN t.[type] = 'U' THEN 'Table' WHEN t.[type] = 'V' THEN 'View' END AS [TypeOfObject] ,CASE WHEN c.[type] = 'PK' THEN 'Primary key' WHEN c.[type] = 'UQ' THEN 'Unique constraint' WHEN i.[type] = 1 THEN 'Unique clustered index' WHEN i.type = 2 THEN 'Unique index' END AS TypeOfConstraint ,ISNULL(c.[name], i.[name]) AS ConstraintName ,SUBSTRING(column_names, 1, LEN(column_names) - 1) AS [ConstraintDescription] FROM sys.objects t LEFT OUTER JOIN sys.indexes i ON t.object_id = i.object_id LEFT OUTER JOIN sys.key_constraints c ON i.object_id = c.parent_object_id AND i.index_id = c.unique_index_id CROSS APPLY ( SELECT col.[name] + ', ' FROM sys.index_columns ic INNER JOIN sys.columns col ON ic.object_id = col.object_id AND ic.column_id = col.column_id WHERE ic.object_id = t.object_id AND ic.index_id = i.index_id ORDER BY col.column_id FOR XML path('') ) D(column_names) WHERE is_unique = 1 AND t.name = @TABLENAME AND t.is_ms_shipped <> 1 UNION ALL SELECT schema_name(fk_tab.schema_id) + '.' + fk_tab.name AS foreign_table ,'Table' ,'Foreign key' ,fk.name AS fk_ConstraintName ,cols.[name] + ' REFERENCES ' + schema_name(pk_tab.schema_id) + '.' + pk_tab.name + ' (' + c2.[name] + ')' FROM sys.foreign_keys fk INNER JOIN sys.tables fk_tab ON fk_tab.object_id = fk.parent_object_id INNER JOIN sys.tables pk_tab ON pk_tab.object_id = fk.referenced_object_id INNER JOIN sys.foreign_key_columns fk_cols ON fk_cols.constraint_object_id = fk.object_id INNER JOIN sys.columns cols ON cols.object_id = fk_cols.parent_object_id AND cols.column_id = fk_cols.parent_column_id INNER JOIN sys.columns c2 ON c2.object_id = fk_cols.referenced_object_id AND c2.column_id = fk_cols.referenced_column_id WHERE fk_tab.name = @TABLENAME OR pk_tab.name = @TABLENAME UNION ALL SELECT schema_name(t.schema_id) + '.' + t.[name] ,'Table' ,'Check constraint' ,con.[name] AS ConstraintName ,con.[definition] FROM sys.check_constraints con LEFT OUTER JOIN sys.objects t ON con.parent_object_id = t.object_id LEFT OUTER JOIN sys.all_columns col ON con.parent_column_id = col.column_id AND con.parent_object_id = col.object_id WHERE t.name = @TABLENAME UNION ALL SELECT schema_name(t.schema_id) + '.' + t.[name] ,'Table' ,'Default constraint' ,con.[name] ,col.[name] + ' = ' + con.[definition] FROM sys.default_constraints con LEFT OUTER JOIN sys.objects t ON con.parent_object_id = t.object_id LEFT OUTER JOIN sys.all_columns col ON con.parent_column_id = col.column_id AND con.parent_object_id = col.object_id WHERE t.name = @TABLENAME ) a ORDER BY ObjectName ,TypeOfConstraint ,ConstraintName Output: Enjoy.!
Stored procedures are an essential part of database management systems. They are used to execute frequently used queries and reduce the load on the database server. However, if not optimized correctly, they can cause performance issues. In this blog, we will discuss how to check the performance of a stored procedure. Steps to Check Performance of a SPROC Identify the SPROC: The first step is to identify the stored procedure that needs to be optimized. You can use SQL Server Management Studio (SSMS) to identify the stored procedure. Check Execution Time: Once you have identified the stored procedure, you can check its execution time. You can use the SET STATISTICS TIME ON command to check the execution time of the stored procedure. Check Query Plan: The next step is to check the query plan of the stored procedure. You can use the SET SHOWPLAN_TEXT ON command to check the query plan. Check Indexes: Indexes play a crucial role in the performance of a stored procedure. You can use the sp_helpindex command to check the indexes of the stored procedure. Check for Blocking: Blocking can cause performance issues in a stored procedure. You can use the sp_who2 command to check for blocking. Check for Deadlocks: Deadlocks can also cause performance issues in a stored procedure. You can use the DBCC TRACEON(1204) command to check for deadlocks. Examples : Here are some examples to help you understand how to check the performance of a stored procedure: To Try this queries yourself I am sharing the Table, Data, SP query so you can direct run and perform this queries : -- Step 1: Create a dummy table CREATE TABLE dbo.Orders ( OrderID INT PRIMARY KEY, CustomerID NVARCHAR(10), OrderDate DATETIME, ProductID INT, Quantity INT ); -- Step 2: Insert dummy data into the table INSERT INTO dbo.Orders (OrderID, CustomerID, OrderDate, ProductID, Quantity) VALUES (1, N'ALFKI', '2024-01-23', 101, 5), (2, N'ALFKI', '2024-01-24', 102, 3), (3, N'BONAP', '2024-01-25', 103, 7), (4, N'BONAP', '2024-01-26', 104, 2), (5, N'COSME', '2024-01-27', 105, 4); -- Step 3: Create a stored procedure CREATE PROCEDURE dbo.usp_GetOrdersByCustomer @CustomerID NVARCHAR(10) AS BEGIN SELECT * FROM dbo.Orders WHERE CustomerID = @CustomerID; END; Example 1: Check Execution Time SET STATISTICS TIME ON EXEC dbo.usp_GetOrdersByCustomer @CustomerID = N'ALFKI' SET STATISTICS TIME OFF Example 2: Check Query Plan SET SHOWPLAN_TEXT ON EXEC dbo.usp_GetOrdersByCustomer @CustomerID = N'ALFKI' SET SHOWPLAN_TEXT OFF Example 3: Check Indexes EXEC sp_helpindex 'dbo.usp_GetOrdersByCustomer' Example 4: Check for Blocking EXEC sp_who2 Example 5: Check for Deadlocks DBCC TRACEON(1204) Conclusion In conclusion, checking the performance of a stored procedure is essential to ensure that it runs efficiently. By following the steps mentioned above, you can identify the performance issues and optimize the stored procedure. I hope this blog helps you in optimizing your stored procedures. If you have any questions or suggestions, please feel free to leave a comment below.
As in recent work with the client, I got the question of finding the indexes to be applied on a particular table in the SQL server. If you want to have listed all the indexes from a particular table from the SQL server, then now you just have to write your table name in the variable and execute the below query. And see the result. DECLARE @TABLENAME VARCHAR(50) = '<table_name>' SELECT '[' + s.name + '].[' + sObj.name + ']' AS 'TableName' ,+ ind.name AS 'IndexName' ,ind.type_desc AS 'IndexType' ,STUFF(( SELECT ', [' + sc.name + ']' AS "text()" FROM syscolumns AS sc INNER JOIN sys.index_columns AS ic ON ic.object_id = sc.id AND ic.column_id = sc.colid WHERE sc.id = Obj.object_id AND ic.index_id = sind.indid AND ic.is_included_column = 0 ORDER BY key_ordinal FOR XML PATH('') ), 1, 2, '') AS 'IndexedColumns' FROM sysindexes AS sind INNER JOIN sys.indexes AS ind ON ind.object_id = sind.id AND ind.index_id = sind.indid INNER JOIN sysobjects AS sObj ON sObj.id = sind.id INNER JOIN sys.objects AS Obj ON Obj.object_id = sObj.id AND is_ms_shipped = 0 INNER JOIN sys.schemas AS s ON s.schema_id = Obj.schema_id WHERE ind.object_id = OBJECT_ID(@TABLENAME) AND ind.is_primary_key = 0 AND ind.is_unique = 0 AND ind.is_unique_constraint = 0 ORDER BY TableName ,IndexName; Output:
Setting up replication in SQL Server can be a powerful way to ensure data consistency and availability across multiple servers. In this step-by-step guide, we'll walk through the process of configuring replication on SQL Servers. Step 1: Understand Replication Types Before diving into configuration, it's crucial to understand the types of replication available in SQL Server. Snapshot Replication: Takes a snapshot of the data at a specific point in time. Transactional Replication: Replicates changes in real-time as they occur. Merge Replication: Allows bidirectional data synchronization between servers. Choose the replication type that aligns with your specific needs and database architecture. Step 2: Prepare Your Environment Ensure that your SQL Server environment is ready for replication. This involves verifying that you have the necessary permissions and establishing proper connectivity between the SQL Server instances. Remember that replication involves three key components: Publisher, Distributor, and Subscribers. The Distributor can be on the same server as the Publisher or a separate server. Step 3: Configure Distributor If a Distributor isn't already set up, proceed to configure one. This involves specifying the server that will act as the Distributor and setting up distribution databases. Use either SQL Server Management Studio (SSMS) or T-SQL scripts for this configuration. Step 4: Enable Replication on the Publisher 1. Open SSMS and connect to the Publisher. 2. Right-click on the target database and choose "Tasks" > "Replication" > "Configure Distribution." 3. Follow the wizard, specifying the Distributor configured in Step 3. Step 5: Choose Articles Define the articles by selecting the tables, views, or stored procedures you want to replicate. This step allows you to fine-tune your replication by specifying data filters, choosing columns to replicate, and configuring additional options based on your specific requirements. Step 6: Configure Subscribers 1. Connect to the Subscribers in SSMS. 2. Right-click on the Replication folder and choose "Configure Distribution." 3. Follow the wizard, specifying the Distributor and configuring additional settings based on your chosen replication type. Step 7: Configure Subscription With the Distributor and Subscribers configured, it's time to set up subscriptions. 1. In SSMS, navigate to the Replication folder on the Publisher. 2. Right-click on the Local Publications and choose "New Subscriptions." 3. Follow the wizard to configure the subscription, specifying the Subscribers and defining any additional settings. Step 8: Monitor and Maintain Regular monitoring and maintenance are essential for a healthy replication environment. - Use the Replication Monitor in SSMS to view the status of publications, subscriptions, and any potential errors. - Implement routine maintenance tasks such as backing up and restoring the replication databases. Conclusion Configuring replication in SQL Server involves a series of well-defined steps. By understanding your replication needs, preparing your environment, and carefully configuring each component, you can establish a robust and reliable replication setup. Regular monitoring and maintenance ensure the ongoing efficiency and performance of your replication environment.
Abstract This article describes a TSQL JSON parser and provides the source. It is also designed to illustrate a number of string manipulation techniques and also eliminate the issues while dealing with the JSON document containing special symbols like (“/” , ”-”....) in T-SQL. With it you can do things like this to extract the data from a JSON file or document which contains noise and complexities. Summary For Implementation The code for the JSON Parser will run in SQL Server 2005, and even in SQL Server 2000 (note: some modifications are necessary). First the function stores all strings in the temporary table, even the name of the elements, since they are 'escapes' in a different way, and may contain, unescaped, brackets, Special Characters which denote objects or lists. These are replaced in the json string by tokens which represent the strings. After this fetch all the json keywords and values for further processing by using the regular expressions, various string functions and a list of SQL queries and variables to store the values for a particular object. And at the last function will return a whole table which contains rows and columns with no noise in the values as the other tables in the particular database. Figure 1:- Json Input Figure 2:- Function Output Background TSQL isn’t really designed for doing complex string parsing which contains special characters and particularly where strings represent nested data structures such as XML, JSON, or XHTML. You can do it but it is not a pretty sight; but If you ever want to do it anyway ? (note You can now do this rather more easily using SQL Server 2016’s built-in JSON support.) But If the SQL Server version is older or not compatible with the built-in JSON support then you can use this customized function to get the desired output by parsing any type of json document. There is so much stuff behind that all happens to you. For example, it could be that DBA doesn’t allow a CLR, or you lack the necessary skills with procedural scripting. Sometimes, there isn’t any application, or you want to run code unobtrusively across databases or servers. The Traditional way for dealing with data like this is to let a separate business layer parse a JSON ‘document’ into some meaningful structure(Like Tree) and then update the database by making a series of calls and lots of sql procedures. This is pretty, but can get more complicated and headache if you need to ensure that the updates to the database are wrapped into one transaction so that if anything goes wrong or any issues occur, then the whole transaction can be rolled back. This is why a TSQL approach has advantages. Adjacency list tables have the same structure whatever the data in them. This means that you can define a single Table-Valued Type and pass data structures around between stored procedures. Converting the data to Hierarchical table form will be different for each application, but is easy with a TSQL. You can, alternatively, convert the hierarchical table into JSON and interrogate that with SQL. JSON format JSON is one of the most popular lightweight markup languages, and is probably the best choice for transfer of object data from a web page. JSON is designed to be as lightweight as possible and so it has only two structures. The first, delimited by curly brackets, is a collection of Key/value pairs, separated by commas. The key is followed by a colon. The first snag for TSQL is that the curly or square brackets are not ‘escaped’ within a string, so that there is no way of partitioning a JSON ‘document’ simply. It is difficult to differentiate a bracket used as the delimiter of an array or structure, and one that is within a string. The second complication is that, unlike YAML, the datatypes of values can’t be explicitly declared. You have to pass them out from applying the rules from the JSON Specification. Implementation The JSON outputter is a great deal simpler, since one can be sure of the input, but essentially it does the reverse process, working from the root of the json document to the leaves. The only complication is working out the indent of the formatted output string. In the implementation, you’ll see a fairly heavy use of PATINDEX.This uses a RegEx. However, it is all we have, and can be pressed into service by chopping the string it is searching (if only it had an optional third parameter like CHARINDEX that specified the index of the start position of the search!). The STUFF function is also important for this sort of string-manipulation work. CREATE FUNCTION [Platform].[parseJSON] (@JSON NVARCHAR(MAX)) RETURNS @hierarchy TABLE ( Element_ID INT IDENTITY(1, 1) NOT NULL /* internal surrogate primary key gives the order of parsing and the list order */ ,SequenceNo [int] NULL /* the place in the sequence for the element */ ,Parent_ID INT NULL /* if the element has a parent then it is in this column. The document is the ultimate parent, so you can get the structure from recursing from the document */ ,[Object_ID] INT NULL /* each list or object has an object id. This ties all elements to a parent. Lists are treated as objects here */ ,[Name] NVARCHAR(2000) NULL /* the Name of the object */ ,StringValue NVARCHAR(MAX) NOT NULL /*the string representation of the value of the element. */ ,ValueType VARCHAR(10) NOT NULL /* the declared type of the value represented as a string in StringValue*/ ) AS BEGIN DECLARE @FirstObject INT --the index of the first open bracket found in the JSON string ,@OpenDelimiter INT --the index of the next open bracket found in the JSON string ,@NextOpenDelimiter INT --the index of subsequent open bracket found in the JSON string ,@NextCloseDelimiter INT --the index of subsequent close bracket found in the JSON string ,@Type NVARCHAR(10) --whether it denotes an object or an array ,@NextCloseDelimiterChar CHAR(1) --either a '}' or a ']' ,@Contents NVARCHAR(MAX) --the unparsed contents of the bracketed expression ,@Start INT --index of the start of the token that you are parsing ,@end INT --index of the end of the token that you are parsing ,@param INT --the parameter at the end of the next Object/Array token ,@EndOfName INT --the index of the start of the parameter at end of Object/Array token ,@token NVARCHAR(200) --either a string or object ,@value NVARCHAR(MAX) -- the value as a string ,@SequenceNo INT -- the sequence number within a list ,@Name NVARCHAR(200) --the Name as a string ,@Parent_ID INT --the next parent ID to allocate ,@lenJSON INT --the current length of the JSON String ,@characters NCHAR(36) --used to convert hex to decimal ,@result BIGINT --the value of the hex symbol being parsed ,@index SMALLINT --used for parsing the hex value ,@Escape INT --the index of the next escape character /* in this temporary table we keep all strings, even the Names of the elements, since they are 'escaped' in a different way, and may contain, unescaped, brackets denoting objects or lists. These are replaced in the JSON string by tokens representing the string */ DECLARE @Strings TABLE ( String_ID INT IDENTITY(1, 1) ,StringValue NVARCHAR(MAX) ) IF ISNULL(@JSON, '') = '' RETURN SELECT @characters = '0123456789abcdefghijklmnopqrstuvwxyz' --initialise the characters to convert hex to ascii ,@SequenceNo = 0 --set the sequence no. to something sensible. ,@Parent_ID = 0; /* firstly we process all strings. This is done because [{} and ] aren't escaped in strings, which complicates an iterative parse. */ WHILE 1 = 1 --forever until there is nothing more to do BEGIN SELECT @start = PATINDEX('%[^a-zA-Z]["]%', @json collate SQL_Latin1_General_CP850_Bin);--next delimited string IF @start = 0 BREAK --no more so drop through the WHILE loop IF SUBSTRING(@json, @start + 1, 1) = '"' BEGIN --Delimited Name SET @start = @Start + 1; SET @end = PATINDEX('%[^\]["]%', RIGHT(@json, LEN(@json + '|') - @start) collate SQL_Latin1_General_CP850_Bin); END IF @end = 0 --either the end or no end delimiter to last string BEGIN -- check if ending with a double slash... SET @end = PATINDEX('%[\][\]["]%', RIGHT(@json, LEN(@json + '|') - @start) collate SQL_Latin1_General_CP850_Bin); IF @end = 0 --we really have reached the end BEGIN BREAK --assume all tokens found END END SELECT @token = SUBSTRING(@json, @start + 1, @end - 1) --now put in the escaped control characters SELECT @token = REPLACE(@token, FromString, ToString) FROM ( SELECT '\b' ,CHAR(08) UNION ALL SELECT '\f' ,CHAR(12) UNION ALL SELECT '\n' ,CHAR(10) UNION ALL SELECT '\r' ,CHAR(13) UNION ALL SELECT '\t' ,CHAR(09) UNION ALL SELECT '\"' ,'"' UNION ALL SELECT '\/' ,'/' ) substitutions(FromString, ToString) SELECT @token = Replace(@token, '\\', '\') SELECT @result = 0 ,@escape = 1 --Begin to take out any hex escape codes WHILE @escape > 0 BEGIN SELECT @index = 0 --find the next hex escape sequence ,@escape = PATINDEX('%\x[0-9a-f][0-9a-f][0-9a-f][0-9a-f]%', @token collate SQL_Latin1_General_CP850_Bin) IF @escape > 0 --if there is one BEGIN WHILE @index < 4 --there are always four digits to a \x sequence BEGIN SELECT --determine its value @result = @result + POWER(16, @index) * (CHARINDEX(SUBSTRING(@token, @escape + 2 + 3 - @index, 1), @characters) - 1) ,@index = @index + 1; END -- and replace the hex sequence by its unicode value SELECT @token = STUFF(@token, @escape, 6, NCHAR(@result)) END END --now store the string away INSERT INTO @Strings (StringValue) SELECT @token -- and replace the string with a token SELECT @JSON = STUFF(@json, @start, @end + 1, '@string' + CONVERT(NCHAR(5), @@identity)) END -- all strings are now removed. Now we find the first leaf. WHILE 1 = 1 --forever until there is nothing more to do BEGIN SELECT @Parent_ID = @Parent_ID + 1 --find the first object or list by looking for the open bracket SELECT @FirstObject = PATINDEX('%[{[[]%', @json collate SQL_Latin1_General_CP850_Bin) --object or array IF @FirstObject = 0 BREAK IF (SUBSTRING(@json, @FirstObject, 1) = '{') SELECT @NextCloseDelimiterChar = '}' ,@type = 'object' ELSE SELECT @NextCloseDelimiterChar = ']' ,@type = 'array' SELECT @OpenDelimiter = @firstObject WHILE 1 = 1 --find the innermost object or list... BEGIN SELECT @lenJSON = LEN(@JSON + '|') - 1 --find the matching close-delimiter proceeding after the open-delimiter SELECT @NextCloseDelimiter = CHARINDEX(@NextCloseDelimiterChar, @json, @OpenDelimiter + 1) --is there an intervening open-delimiter of either type SELECT @NextOpenDelimiter = PATINDEX('%[{[[]%', RIGHT(@json, @lenJSON - @OpenDelimiter) collate SQL_Latin1_General_CP850_Bin) --object IF @NextOpenDelimiter = 0 BREAK SELECT @NextOpenDelimiter = @NextOpenDelimiter + @OpenDelimiter IF @NextCloseDelimiter < @NextOpenDelimiter BREAK IF SUBSTRING(@json, @NextOpenDelimiter, 1) = '{' SELECT @NextCloseDelimiterChar = '}' ,@type = 'object' ELSE SELECT @NextCloseDelimiterChar = ']' ,@type = 'array' SELECT @OpenDelimiter = @NextOpenDelimiter END ---and parse out the list or Name/value pairs SELECT @contents = SUBSTRING(@json, @OpenDelimiter + 1, @NextCloseDelimiter - @OpenDelimiter - 1) SELECT @JSON = STUFF(@json, @OpenDelimiter, @NextCloseDelimiter - @OpenDelimiter + 1, '@' + @type + CONVERT(NCHAR(5), @Parent_ID)) WHILE (PATINDEX('%[A-Za-z0-9@+.e]%', @contents collate SQL_Latin1_General_CP850_Bin)) <> 0 BEGIN IF @Type = 'object' --it will be a 0-n list containing a string followed by a string, number,boolean, or null BEGIN SELECT @SequenceNo = 0 ,@end = CHARINDEX(':', ' ' + @contents) --if there is anything, it will be a string-based Name. SELECT @start = PATINDEX('%[^A-Za-z@][@]%', ' ' + @contents collate SQL_Latin1_General_CP850_Bin) --AAAAAAAA SELECT @token = RTrim(Substring(' ' + @contents, @start + 1, @End - @Start - 1)) ,@endofName = PATINDEX('%[0-9]%', @token collate SQL_Latin1_General_CP850_Bin) ,@param = RIGHT(@token, LEN(@token) - @endofName + 1) SELECT @token = LEFT(@token, @endofName - 1) ,@Contents = RIGHT(' ' + @contents, LEN(' ' + @contents + '|') - @end - 1) SELECT @Name = StringValue FROM @strings WHERE string_id = @param --fetch the Name END ELSE SELECT @Name = NULL ,@SequenceNo = @SequenceNo + 1 SELECT @end = CHARINDEX(',', @contents) -- a string-token, object-token, list-token, number,boolean, or null IF @end = 0 --HR Engineering notation bugfix start IF ISNUMERIC(@contents) = 1 SELECT @end = LEN(@contents) + 1 ELSE --HR Engineering notation bugfix end SELECT @end = PATINDEX('%[A-Za-z0-9@+.e][^A-Za-z0-9@+.e]%', @contents + ' ' collate SQL_Latin1_General_CP850_Bin) + 1 SELECT @start = PATINDEX('%[^A-Za-z0-9@+.e][-A-Za-z0-9@+.e]%', ' ' + @contents collate SQL_Latin1_General_CP850_Bin) --select @start,@end, LEN(@contents+'|'), @contents SELECT @Value = RTRIM(SUBSTRING(@contents, @start, @End - @Start)) ,@Contents = RIGHT(@contents + ' ', LEN(@contents + '|') - @end) IF SUBSTRING(@value, 1, 7) = '@object' INSERT INTO @hierarchy ( [Name] ,SequenceNo ,Parent_ID ,StringValue ,[Object_ID] ,ValueType ) SELECT @Name ,@SequenceNo ,@Parent_ID ,SUBSTRING(@value, 8, 5) ,SUBSTRING(@value, 8, 5) ,'object' ELSE IF SUBSTRING(@value, 1, 6) = '@array' INSERT INTO @hierarchy ( [Name] ,SequenceNo ,Parent_ID ,StringValue ,[Object_ID] ,ValueType ) SELECT @Name ,@SequenceNo ,@Parent_ID ,SUBSTRING(@value, 7, 5) ,SUBSTRING(@value, 7, 5) ,'array' ELSE IF SUBSTRING(@value, 1, 7) = '@string' INSERT INTO @hierarchy ( [Name] ,SequenceNo ,Parent_ID ,StringValue ,ValueType ) SELECT @Name ,@SequenceNo ,@Parent_ID ,StringValue ,'string' FROM @strings WHERE string_id = SUBSTRING(@value, 8, 5) ELSE IF @value IN ('true', 'false') INSERT INTO @hierarchy ( [Name] ,SequenceNo ,Parent_ID ,StringValue ,ValueType ) SELECT @Name ,@SequenceNo ,@Parent_ID ,@value ,'boolean' ELSE IF @value = 'null' INSERT INTO @hierarchy ( [Name] ,SequenceNo ,Parent_ID ,StringValue ,ValueType ) SELECT @Name ,@SequenceNo ,@Parent_ID ,@value ,'null' ELSE IF PATINDEX('%[^0-9-]%', @value collate SQL_Latin1_General_CP850_Bin) > 0 INSERT INTO @hierarchy ( [Name] ,SequenceNo ,Parent_ID ,StringValue ,ValueType ) SELECT @Name ,@SequenceNo ,@Parent_ID ,@value ,'real' ELSE INSERT INTO @hierarchy ( [Name] ,SequenceNo ,Parent_ID ,StringValue ,ValueType ) SELECT @Name ,@SequenceNo ,@Parent_ID ,@value ,'int' IF @Contents = ' ' SELECT @SequenceNo = 0 END END INSERT INTO @hierarchy ( [Name] ,SequenceNo ,Parent_ID ,StringValue ,[Object_ID] ,ValueType ) SELECT '-' ,1 ,NULL ,'' ,@Parent_ID - 1 ,@type RETURN END Code Snippet 1:- ParseJson Function Closure The so-called ‘impedance-mismatch’ between applications and databases is an illusion. if the developer has understood the data correctly then there is less complexity while processing it. But has been trickier with other formats such as JSON. By using techniques like this, it should be possible to liberate the application or website from having to do the mapping from the object model to the relational, and spraying the database with ad-hoc T-SQL that uses the fact/dimension tables or updateable views. If the database can be provided with the JSON, or the Table-Valued parameter, then there is a better chance of maintaining full transactional integrity for the more complex updates. The database developer already has the tools to do the work with XML, but why not the simpler, and more practical JSON? I hope these routines get you started with experimenting with all this for your requirements.