Wednesday 12 February 2020

How to Create a Linked Server In SQL Server

In this article, I am explaining, How to Linked Server In SQL Server.

There are three way to linked server:-

Using SQL Server Management Studio

To create a linked server to another instance of SQL Server Using SQL Server Management Studio
  1. In SQL Server Management Studio, open Object Explorer, expand Server Objects, right-click Linked Servers, and then click New Linked Server.
  2. On the General page, in the Linked server box, type the name of the instance of SQL Server that you are linking to.

To create a linked server to another instance of SQL Server using Transact-SQL

In Query Editor, enter the following Transact-SQL command to link to an instance of SQL Server named SRVR002\ACCTG:
    SQLCopy
    
    
    USE [master]
    GO
      EXEC master.dbo.sp_addlinkedserver
      @server = N'DOMAIN\SERVER', -- You need to change Server Name
      @srvproduct=N'SQL Server' ;
     GO

Execute the following code to configure the linked server to use the domain credentials of the login that is using the linked server.

SQLCopy
    EXEC master.dbo.sp_addlinkedsrvlogin
     @rmtsrvname = N'SERVER NAME', -- YOU need to change the server name
     @locallogin = NULL ,
     @useself = N'True' ;
     GO
    
    

Ref- https://docs.microsoft.com/en-us/sql/relational-databases/linked-servers/create-linked-servers-sql-server-database-engine?view=sql-server-ver15

Friday 7 February 2020

Currently All running queries in SQL Server

In this article, I am explaining how to find which SQL queries are being run on an SQL Server.

Using the below query, we can get list the queries running on SQL Server


SELECT b.TEXT,
a.session_id,
a.status,
a.command,
a.cpu_time,
a.total_elapsed_time
FROM sys.dm_exec_requests a
CROSS APPLY sys.dm_exec_sql_text(sql_handle) AS b

By using above query, we can find a list of queries. If we want to kill any query. We can use below command-


KILL session_id