Showing posts with label SQL Server. Show all posts
Showing posts with label SQL Server. Show all posts

Wednesday, April 24, 2024

Understanding Indexing in SQL Server: Types and Usage

What is an Index?   

An index in SQL Server is a data structure associated with a table or view that speeds up the retrieval of rows based on the values in one or more columns. It serves as a well-organized reference guide, allowing SQL Server to efficiently locate rows that match query criteria without scanning the entire table.

Types of Indexes:

1. Clustered Index: Determines the physical order of data in a table, affecting the order of data when modified.
2. Non-clustered Index: Creates a separate structure with sorted references to actual data rows, useful for enhancing SELECT query performance.
3. Unique Index: Ensures uniqueness of values in the indexed column(s) across the table, aiding in data integrity.
4. Covering Index: Includes all columns needed to fulfill a query, minimizing I/O operations and improving query performance.
5. Filtered Index: Includes only a subset of rows in the table based on a WHERE clause, useful for optimizing queries targeting specific subsets of data.
6. Spatial Index: Specialized for spatial data types, facilitating efficient spatial queries such as distance calculations and intersections.
7. Columnstore Indexes: Organizes data by columns, beneficial for analytical queries involving aggregations and scans across large datasets.

Usage of Indexes:

 Faster Data Retrieval: Provides a shortcut to desired rows, reducing the time to locate and retrieve data, particularly helpful for SELECT queries.  
Optimizing Joins: Indexes on join columns enhance performance by quickly identifying matching rows.  
Sorting and Grouping: Speed up ORDER BY and GROUP BY operations by efficiently retrieving and organizing data.  
Constraint Enforcement: Unique indexes ensure data integrity by preventing duplicate values in indexed columns.  
Covering Queries: Minimizes I/O operations and speeds up query execution by scanning the index alone.  
Reducing I/O Operations: Efficient use of indexes minimizes I/O operations required to satisfy a query.

Best Practices for Indexing:

1. Selective Indexing: Focus on columns frequently used in WHERE clauses, JOIN conditions, and ORDER BY clauses to avoid unnecessary overhead.
2. Regular Maintenance: Monitor and maintain indexes regularly, including rebuilding or reorganizing to minimize fragmentation.
3. Avoid Over-Indexing: Strike a balance between performance gains and maintenance overhead to avoid diminishing returns.
4. Consider Clustered Index Carefully: Choose based on typical table queries and access patterns.
5. Use Indexing Tools: Leverage tools such as the Database Engine Tuning Advisor to recommend appropriate indexes based on query performance analysis.
6. Understand Query Execution Plans: Analyse plans to identify areas where indexes can optimize query performance.

Conclusion:  

Indexes in SQL Server play a crucial role in enhancing query speed by enabling quicker data retrieval and minimizing the need for full-table scans. Selecting the right type of index and adhering to best practices, including regular maintenance and thorough understanding of database access patterns, are vital for extracting maximum benefits from indexing. 

Tuesday, April 23, 2024

Types of Keys in DBMS

Here are the key points about different types of keys in the relational model:

1. Candidate Key:
   - It is a minimal set of attributes that can uniquely identify a tuple.
   - Every table must have at least one candidate key.
   - A table can have multiple candidate keys but only one primary key.
   - The value of the candidate key is unique and may be null for a tuple.

2. Primary Key:
   - It is a unique key that can identify only one tuple at a time.
   - It cannot have duplicate or null values.
   - It can be composed of more than one column.

3. Super Key:
   - It is a set of attributes that can uniquely identify a tuple.
   - Adding zero or more attributes to the candidate key generates the super key.

4. Alternate Key:
   - It is a candidate key other than the primary key.
   - All keys which are not primary keys are called alternate keys.

5. Foreign Key:
   - It acts as a primary key in one table and as a secondary key in another table.
   - It combines two or more relations at a time.

6. Composite Key:
   - It is used when a single attribute does not uniquely identify all the records in a table.
   - It is composed of multiple attributes and used together to uniquely identify rows in a table.

These types of keys are essential in database management systems as they help in distinct identification, relation development, and maintaining data integrity between tables. 

Friday, March 15, 2024

How to identify duplicate indexes along with columns in SQL Server?

To get the key column list from indexes that are duplicates in SQL Server, you can use the following query:

use databasename
go

WITH DuplicateIndexes AS (
    SELECT 
        i.OBJECT_ID,
        i.index_id
    FROM 
        sys.index_columns ic
    JOIN 
        sys.indexes i ON i.OBJECT_ID = ic.OBJECT_ID 
                     AND i.index_id = ic.index_id
    WHERE 
        i.type_desc <> 'HEAP' 
		AND OBJECT_NAME(i.OBJECT_ID) NOT LIKE '%sys%' --excluding system tables
    GROUP BY 
        i.OBJECT_ID, i.index_id
    HAVING 
        COUNT(*) > 1 -- to check duplicates 
)

SELECT 
    SCHEMA_NAME(o.schema_id) AS SchemaName,
    OBJECT_NAME(ic.OBJECT_ID) AS TableName,
    i.name AS IndexName,
    STRING_AGG(c.name, ', ') WITHIN GROUP (ORDER BY ic.key_ordinal) AS IndexedColumns
FROM 
    sys.index_columns ic
JOIN 
    sys.indexes i ON i.OBJECT_ID = ic.OBJECT_ID 
                 AND i.index_id = ic.index_id
JOIN 
    sys.objects o ON o.OBJECT_ID = ic.OBJECT_ID
JOIN 
    sys.columns c ON ic.OBJECT_ID = c.OBJECT_ID 
                 AND ic.column_id = c.column_id
JOIN 
    DuplicateIndexes di ON di.OBJECT_ID = ic.OBJECT_ID 
                        AND di.index_id = ic.index_id
GROUP BY 
    o.schema_id, ic.OBJECT_ID, i.name;
  

This query first identifies the indexes that are duplicates, and then retrieves the table name, index name, and the key column list for each duplicate index.

Execute this query in your SQL Server management tool to get the key column list from indexes that are duplicates in your database.

Wednesday, March 13, 2024

How to Review transaction order and lock acquisition in SQL Server

In SQL Server, you can review the transaction order and lock acquisition by analysing the queries and transactions that are being executed against the database. Here are some approaches to review transaction order and lock acquisition:

  1. Transaction isolation levels:

    • Review the transaction isolation levels used in your database transactions. Isolation levels such as Read Uncommitted, Read Committed, Repeatable Read, and Serializable can impact the order of lock acquisition and the behaviour of concurrent transactions.
  2. Query execution plans:

    • Use SQL Server Management Studio (SSMS) or other database management tools to analyse the query execution plans for your transactions.
    • The execution plans can provide insights into the order in which data is accessed and the types of locks acquired during query execution.
  3. Locking and blocking:

    • Monitor and analyse the locking and blocking behaviour of concurrent transactions using tools like SQL Server Profiler, Extended Events, or dynamic management views (DMVs) such as sys.dm_tran_locks and sys.dm_os_waiting_tasks.
    • Identify instances of blocking and analyse the lock types and resources involved to understand the order of lock acquisition.
  4. Transaction log and history:

    • Review the transaction log and history to understand the sequence of transactions and their impact on lock acquisition.
    • SQL Server's transaction log and history can provide valuable information about the order in which transactions are executed and their associated locks.

By using these approaches, you can gain insights into the transaction order and lock acquisition behaviour in SQL Server, which can help in identifying potential issues related to deadlocks, blocking, and overall transaction concurrency.

Tuesday, March 12, 2024

What is deadlock priority and how to address in SQL Server

In SQL Server, deadlock priority is a mechanism that allows you to influence the selection of the transaction that will be chosen as the deadlock victim when a deadlock occurs. You can use deadlock priority to specify the importance of individual transactions in the event of a deadlock.

To address deadlock priority in SQL Server, you can consider the following:

  1. Setting deadlock priority:
    • You can use the SET DEADLOCK_PRIORITY statement to specify the priority of a session or transaction.
    • The priority levels range from -10 to 10, where -10 is the lowest priority and 10 is the highest.
    • By setting the deadlock priority, you can influence the selection of the victim transaction when a deadlock occurs.
  2. Here's an example of how to set the deadlock priority for a session:

    SET DEADLOCK_PRIORITY LOW; -- Set the deadlock priority to low
      
  3. Adjusting transaction logic:

    • Design your transaction logic to handle the potential impact of being chosen as the deadlock victim based on the assigned deadlock priority.
    • Consider implementing retry logic for transactions with lower deadlock priority after being chosen as the deadlock victim.
  4. Analyzing and tuning deadlock priority:

    • Evaluate the impact of deadlock priority settings on your application's transactions and overall performance.
    • Tune the deadlock priority based on the specific requirements and characteristics of your application to effectively manage deadlocks.

It's important to carefully consider the implications of deadlock priority settings in SQL Server and design your transaction logic to handle deadlock situations appropriately. Understanding the behavior of deadlock priority in SQL Server is crucial for effectively addressing and managing deadlocks.

Wednesday, March 06, 2024

How to implement retry logic for DB Transactions

In SQL Server, you can implement retry logic for transactions using T-SQL and error handling. Here's an example of how you can create a stored procedure that includes retry logic for handling deadlock errors:

CREATE PROCEDURE usp_RetryTransaction
AS
BEGIN
    DECLARE @retryCount INT = 0
    DECLARE @maxRetries INT = 3

    WHILE @retryCount < @maxRetries
    BEGIN
        BEGIN TRY
            BEGIN TRANSACTION
            -- Your transactional logic goes here
            COMMIT TRANSACTION
            RETURN
        END TRY
        BEGIN CATCH
            IF ERROR_NUMBER() = 1205  -- Deadlock error number
            BEGIN
                ROLLBACK TRANSACTION
                SET @retryCount = @retryCount + 1
                WAITFOR DELAY '00:00:01'  -- Wait for 1 second before retrying
            END
            ELSE
            BEGIN
                -- Handle other types of errors
                THROW
            END
        END CATCH
    END
    -- If the maximum number of retries is reached, handle the situation as needed
    -- For example, raise an error or log the issue
END
  

In this example, the stored procedure attempts the transaction logic within a retry loop, and if a deadlock error (error number 1205) occurs, it rolls back the transaction, increments the retry count, and waits for a short duration before retrying the transaction. If the maximum number of retries is reached, you can handle the situation as needed based on your application's requirements.

You can then call this stored procedure whenever you need to perform a transaction with retry logic for deadlock handling.

Sunday, March 03, 2024

How to find a view in database where its used in SQL Server

To find where a specific view is used in a SQL Server database, you can query the system catalog views. Here's a query to achieve this:

SELECT 
    referencing_schema_name, 
    referencing_entity_name
FROM 
    sys.dm_sql_referencing_entities('YourSchema.YourView', 'OBJECT');
  

Replace YourSchema with the schema of your view and YourView with the name of the view you want to find. This query will return the schema and name of the objects that reference the specified view.

Execute this query in your SQL Server management tool to find where a specific view is used in your database.

Hope this help!!

Friday, February 02, 2024

Removing Cached login and password list in SQL Server Management Studio

You need to look in following location based on the SSMS Instance you have in your local PC.

Since mine is 19.0 version, Below is my path.

C:\Users\sconrey\AppData\Roaming\Microsoft\SQL Server Management Studio\19.0

Open UserSettings.xml in Notepad ++ or any editor of your choice.

Find the User you would like to remove and delete the Entire Element tag related to that User.

<ServerTypeItem>
    <Servers>   
        <Element>
 

        </Element>
    </Servers>
< /ServerTypeItem>

You need to remove complete Element tag from the file and save it. Please make sure during this process. SSMS should be closed, if not your changes will not eb updated.

Sunday, June 18, 2023

How to implement impersonation in SQL Server

To implement impersonation in SQL Server, you can follow these steps:

1. Create a Login:
First, create a SQL Server login for the user you want to impersonate. Use the `CREATE LOGIN` statement to create the login and provide the necessary authentication credentials.

Example:

CREATE LOGIN [ImpersonatedUser] WITH PASSWORD = 'password';
  

2. Create a User:
Next, create a user in the target database associated with the login you created in the previous step. Use the `CREATE USER` statement to create the user and map it to the login.

Example:  

CREATE USER [ImpersonatedUser] FOR LOGIN [ImpersonatedUser];
  

3. Grant Permissions:
Grant the necessary permissions to the user being impersonated. Use the `GRANT` statement to assign the required privileges to the user.

Example:

GRANT SELECT, INSERT, UPDATE ON dbo.TableName TO [ImpersonatedUser];
  

4. Impersonate the User:
To initiate impersonation, use the `EXECUTE AS USER` statement followed by the username of the user you want to impersonate. This will switch the execution context to the specified user.

Example:

EXECUTE AS USER = 'ImpersonatedUser';
  

5. Execute Statements:
Within the impersonated context, execute the desired SQL statements or actions. These statements will be performed with the permissions and privileges of the impersonated user.

Example:

SELECT * FROM dbo.TableName;
-- Perform other actions as needed
  

6. Revert Impersonation:
After completing the necessary actions, revert back to the original security context using the `REVERT` statement. This will switch the execution context back to the original user.

Example:

REVERT;
  

By following these steps, you can implement impersonation in SQL Server. Ensure that you grant the appropriate permissions to the user being impersonated and consider security implications when assigning privileges.

Here is the full syntax:

EXECUTE AS LOGIN = 'DomainName\impersonatedUser'
EXEC  uspInsertUpdateGridSettings @param1, @param2
REVERT;
  

Additionally, be mindful of auditing and logging to track and monitor impersonated actions for accountability and security purposes.

Tuesday, June 13, 2023

What is a SQL Injection Attack?

SQL injection is a type of web application security vulnerability and attack that occurs when an attacker is able to manipulate an application's SQL (Structured Query Language) statements. It takes advantage of poor input validation or improper construction of SQL queries, allowing the attacker to insert malicious SQL code into the application's database query.

SQL Injection attacks are also called SQLi. SQL stands for 'structured query language' and SQL injection is sometimes abbreviated to SQLi

Impact of SQL injection on your applications

  • Steal credentials—attackers can obtain credentials via SQLi and then impersonate users and use their privileges.
  • Access databases—attackers can gain access to the sensitive data in database servers.
  • Alter data—attackers can alter or add new data to the accessed database. 
  • Delete data—attackers can delete database records or drop entire tables. 
  • Lateral movement—attackers can access database servers with operating system privileges, and use these permissions to access other sensitive systems.
  • Types of SQL Injection Attacks

    There are several types of SQL injection:

  • Union-based SQL Injection – Union-based SQL Injection represents the most popular type of SQL injection and uses the UNION statement. The UNION statement represents the combination of two select statements to retrieve data from the database.
  • Error-Based SQL Injection – this method can only be run against MS-SQL Servers. In this attack, the malicious user causes an application to show an error. Usually, you ask the database a question and it returns an error message which also contains the data they asked for.
  • Blind SQL Injection – in this attack, no error messages are received from the database; We extract the data by submitting queries to the database. Blind SQL injections can be divided into boolean-based SQL Injection and time-based SQL Injection.
  • SQLi attacks can also be classified by the method they use to inject data:

  • SQL injection based on user input – web applications accept inputs through forms, which pass a user’s input to the database for processing. If the web application accepts these inputs without sanitizing them, an attacker can inject malicious SQL statements.
  • SQL injection based on cookies – another approach to SQL injection is modifying cookies to “poison” database queries. Web applications often load cookies and use their data as part of database operations. A malicious user, or malware deployed on a user’s device, could modify cookies, to inject SQL in an unexpected way.
  • SQL injection based on HTTP headers – server variables such HTTP headers can also be used for SQL injection. If a web application accepts inputs from HTTP headers, fake headers containing arbitrary SQL can inject code into the database.
  • Second-order SQL injection – these are possibly the most complex SQL injection attacks, because they may lie dormant for a long period of time. A second-order SQL injection attack delivers poisoned data, which might be considered benign in one context, but is malicious in another context. Even if developers sanitize all application inputs, they could still be vulnerable to this type of attack.
  • Here are few defense mechanisms to avoid these attacks 

    1. Prepared statements:  These are easy to learn and use, and eliminate problem  of SQL Injection. They force you to define SQL code, and pass each parameter to the query later, making a strong distinction between code and data

    2. Stored Procedures: Stored procedures are similar to prepared statements, only the SQL code for the stored procedure is defined and stored in the database, rather than in the user’s code. In most cases, stored procedures can be as secure as prepared statements, so you can decide which one fits better with your development processes.

    There are two cases in which stored procedures are not secure:

  • The stored procedure includes dynamic SQL generation – this is typically not done in stored procedures, but it can be done, so you must avoid it when creating stored procedures. Otherwise, ensure you validate all inputs.
  • Database owner privileges – in some database setups, the administrator grants database owner permissions to enable stored procedures to run. This means that if an attacker breaches the server, they have full rights to the database. Avoid this by creating a custom role that allows storage procedures only the level of access they need.
  • 3. Allow-list Input Validation: This is another strong measure that can defend against SQL injection. The idea of allow-list validation is that user inputs are validated against a closed list of known legal values.

    4. Escaping All User-Supplied Input: Escaping means to add an escape character that instructs the code to ignore certain control characters, evaluating them as text and not as code.

    Tuesday, June 06, 2023

    Find tables or procedures that are associated in SQL Jobs via Query

    Recently we need to look for a procedure where we are using in SQL Jobs. There is no easy way to find unless you script all jobs and find in the script.

    But there is some easy way to find it using below query. You could also might have similar ask to find a procedure or table that you might have used in SQL Jobs in any of those steps. It could be any string like comment, procedure, function or table, this below query works.

    USE msdb
    GO
    
    SELECT [sJOB].[job_id] AS [JobID]
    	,[sJOB].[name] AS [JobName]
    	,step.step_name AS JobStepName
    	,step.command AS JobCommand
    	,[sJOB].enabled AS ActiveStatus
    FROM [msdb].[dbo].[sysjobs] AS [sJOB]
    LEFT JOIN [msdb].dbo.sysjobsteps step ON sJOB.job_id = step.job_id
    WHERE step.command LIKE '%uspPopulateAggregatorUsageData%' ----You can change here what you are searching for
    ORDER BY [JobName]
      

    Thank you

    Friday, June 02, 2023

    SQL Server Management Studio (SSMS) Versions

    Microsoft SQL Server Management Studio (SSMS or S.S.M.S.) is the integrated environment for managing your SQL Server infrastructure. SQL Server Management Studio presents a graphical interface for configuring, monitoring, and administering instances of SQL Server. It also allows you to deploy, monitor, and upgrade the data-tier components used by your applications, such as databases. SQL Server Management Studio also provides Transact-SQL, MDX, DMX, and XML language editors for editing and debugging scripts.

    Management Studio is a completely standalone product, not tied to any specific version or edition of SQL Server, and no longer requires licensing of any kind.

    Here is a list of SQL Server Management Studio (SSMS) versions with their respective version numbers:

    1. SQL Server 2005 Management Studio - Version 9.00
    2. SQL Server 2008 Management Studio - Version 10.00
    3. SQL Server 2008 R2 Management Studio - Version 10.50
    4. SQL Server 2012 Management Studio - Version 11.0
    5. SQL Server 2014 Management Studio - Version 12.0
    6. SQL Server 2016 Management Studio - Version 13.0
    7. SQL Server 2017 Management Studio - Version 17.0
    8. SQL Server 2019 Management Studio - Version 18.0

    These version numbers correspond to the major releases of SQL Server Management Studio. It's worth noting that within each major release, there may be minor updates or service packs that increment the version number further (e.g., 13.0.1, 13.0.2, etc.).

    You can download SQL Server Management Studio (SSMS) from the official Microsoft website. Here are the steps to download SSMS:

    1. Go to the Microsoft Download Center at https://docs.microsoft.com/en-us/sql/ssms/download-sql-server-management-studio-ssms.
    2. Scroll down to the "Download SSMS" section.
    3. Select the version of SSMS you want to download. Click on the corresponding link.
    4. On the next page, review the system requirements and other information.
    5. Click the "Download" button to start the download process.

    The download page may provide additional options, such as choosing the language and the installation type (e.g., 32-bit or 64-bit). Make sure to select the appropriate options based on your system requirements.

    Please note that the availability of specific versions of SSMS may vary based on the operating system and SQL Server version you are using. It's recommended to choose the version that matches your SQL Server installation.

    How to get comma separated values from SQL

    There are few types where you can get comma separated values form SQL SERVER using SQL

    1. XML PATH method:

    SELECT 
       STUFF((SELECT ', ' + column_name
              FROM table_name
              WHERE conditions
              FOR XML PATH('')), 1, 2, '') AS csv_values;
      

    2. COALESCE and FOR XML method:

    SELECT 
       STUFF((
          SELECT ', ' + column_name
          FROM table_name
          WHERE conditions
          FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 2, '') AS csv_values;
      

    3. FOR XML PATH method

    SELECT 
       STUFF((SELECT ',' + column_name
              FROM table_name
              WHERE conditions
              FOR XML PATH('')), 1, 1, '') AS csv_values;
      

    In these examples, replace column_name with the actual column name and table_name with the appropriate table name. Customize the WHERE clause to filter the desired rows if necessary.

    When executing these queries, a single row with a single column will be returned, containing the comma-separated values from the specified column. Please note that the XML-related methods convert the values to XML and then manipulate them, resulting in a string of comma-separated values.

    Hope this helps!!

    Thursday, February 09, 2023

    How to Find Tables that Contain a Specific Column in SQL Server?

    Basic concept to understand about SQL Server is that of catalog views, which are effectively database tables (catalogs in this case) that display system-wide information about the SQL Server Database Engine.

    All catalog views are accessed via a SELECT SQL statement FROM a specific catalog within the sys. namespace.
    For example, the following statement can be used to view information about all database tables in the system via the sys.tables catalog

    use mobility
    go
    select  
            s.[name]            'Schema',
            t.[name]            'Table',
            c.[name]            'Column',
            d.[name]            'Data Type',
            c.[max_length]      'Length',
            d.[max_length]      'Max Length',
            d.[precision]       'Precision',
            c.[is_identity]     'Is Id',
            c.[is_nullable]     'Is Nullable',
            c.[is_computed]     'Is Computed',
            d.[is_user_defined] 'Is UserDefined',
            t.[modify_date]     'Date Modified',
            t.[create_date]     'Date created'
    from        sys.schemas s
    inner join  sys.tables  t
    on s.schema_id = t.schema_id
    inner join  sys.columns c
    on t.object_id = c.object_id
    inner join  sys.types   d
    on c.user_type_id = d.user_type_id
    where c.name like '%ProjectManagerID%'
      

    Hope this helps!!

    Thursday, December 01, 2022

    How to check active transactions in SQL Server

    A transaction is a single unit of work. If a transaction is successful, all of the data modifications made during the transaction are committed and become a permanent part of the database.

    Some times, if there are any issues we will get into this deadlock stage and we don't get any responses from database. In those cases, if you want know what are active connections going on at that point of time, will help identifying issue.

    if you want to know more details about active sessions like session ID, Host Name, Login Name, Transaction ID, Transaction Name, Transaction Begin Time,Database ID,Database Name etc.

    Use the below query for details,

    SELECT
    trans.session_id AS [SESSION ID],
    execSession.host_name AS [HOST NAME],login_name AS [Login NAME],
    trans.transaction_id AS [TRANSACTION ID],
    tas.name AS [TRANSACTION NAME],tas.transaction_begin_time AS [TRANSACTION 
    BEGIN TIME],
    tds.database_id AS [DATABASE ID],DBs.name AS [DATABASE NAME]
    FROM sys.dm_tran_active_transactions tas
    JOIN sys.dm_tran_session_transactions trans
    ON (trans.transaction_id=tas.transaction_id)
    LEFT OUTER JOIN sys.dm_tran_database_transactions tds
    ON (tas.transaction_id = tds.transaction_id )
    LEFT OUTER JOIN sys.databases AS DBs
    ON tds.database_id = DBs.database_id
    LEFT OUTER JOIN sys.dm_exec_sessions AS execSession
    ON trans.session_id = execSession.session_id
    WHERE execSession.session_id IS NOT NULL
      

    Wednesday, November 16, 2022

    What is the Query to display the failed SQL Jobs

    We can get this information by the standard reports available in SQL Server, right click on SQL server agent> Standard reports and select the desired report to see Job History

    It can be frustrating to find recently failed jobs in the job history in SQL Server Management Studio. A quicker way to do it is to just run a query to see what jobs have failed recently.

    Below Query will give you list of failed jobs, you can also filter by name.

    select j.name
        ,js.step_name
        ,jh.sql_severity
        ,jh.message
        ,jh.run_date
        ,jh.run_time
    FROM msdb.dbo.sysjobs AS j
    INNER JOIN msdb.dbo.sysjobsteps AS js
       ON js.job_id = j.job_id
    INNER JOIN msdb.dbo.sysjobhistory AS jh
       ON jh.job_id = j.job_id AND jh.step_id = js.step_id
    WHERE jh.run_status = 0
    --and name = 'jobName'
    order by run_date desc
      

    If you want to check results based on data range, you can use below query to find desired results.

    -- Variable Declarations 
    DECLARE @FinalDate INT;
    SET @FinalDate = CONVERT(int
        , CONVERT(varchar(10), DATEADD(DAY, -2, GETDATE()), 112)
        ) -- last two days date as Integer in YYYYMMDD format
    
    -- Final Logic 
    
    SELECT  j.[name],  
            s.step_name,  
            h.step_id,  
            h.step_name,  
            h.run_date,  
            h.run_time,  
            h.sql_severity,  
            h.message,   
            h.server  
    FROM    msdb.dbo.sysjobhistory h  
            INNER JOIN msdb.dbo.sysjobs j  
                ON h.job_id = j.job_id  
            INNER JOIN msdb.dbo.sysjobsteps s  
                ON j.job_id = s.job_id 
                    AND h.step_id = s.step_id  
    WHERE    h.run_status = 0 -- Failure  
             AND h.run_date > @FinalDate  
    ORDER BY h.instance_id DESC;
      

    Hope this helps!

    Thursday, June 24, 2021

    SSDT for Visual Studio 2019

    Install SSDT with Visual Studio 2019

    If Visual Studio 2019 is already installed, you can edit the list of workloads to include SSDT. If you don’t have Visual Studio 2019 installed, then you can download and install Visual Studio 2019 Community

    To modify the installed Visual Studio workloads to include SSDT, use the Visual Studio Installer.

    For Analysis Services, Integration Services, or Reporting Services projects, you can install the appropriate extensions from within Visual Studio with Extensions > Manage Extensions or from the Marketplace.

    Installing Visual Studio 2019

    To download Visual Studio 2019 we will use the following link.

    Wednesday, June 23, 2021

    SSDT for Visual Studio 2017

    Starting with Visual Studio 2017, the functionality of creating Database Projects has been integrated into the Visual Studio installation. There's no need to install the SSDT standalone installer for the core SSDT experience.

    Now to create Analysis Services, Integration Services, or Reporting Services projects, you still need the SSDT standalone installer.

    Install Analysis Services, Integration Services, and Reporting Services tools

    To install Analysis Services, Integration Services, and Reporting Services project support, run the SSDT standalone installer.

    The installer lists available Visual Studio instances to add SSDT tools. If Visual Studio isn't already installed, selecting Install a new SQL Server Data Tools instance installs SSDT with a minimal version of Visual Studio, but for the best experience, we recommend using SSDT with the latest version of Visual Studio



    Saturday, May 29, 2021

    How to Get Columns details from SQL Tables

    Here is how you can get column names from specified table

    SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.Columns 
    WHERETABLE_NAME = 'MobilityOrders'
    

    Here is how you can get column count from specified table

    SELECT COUNT(COLUMN_NAME) as COUNT FROM INFORMATION_SCHEMA.Columns 
    WHERE TABLE_NAME = 'MobilityOrders'
    

    Here is how you can get column count from temp table

     SELECT COUNT(*) as Cnt FROM tempdb.sys.columns
     WHERE object_id = object_id('tempdb..#temp2')
    

    Hope this helps 😀