Click here to Skip to main content
15,884,794 members
Articles / Database Development / SQL Server
Article

Complete Remote (Intra/Inter) SQL Server Manager

Rate me:
Please Sign up or sign in to vote.
3.00/5 (5 votes)
3 Feb 20042 min read 49.6K   21   6
SQL Server manager for remote monitering and managing.

Introduction

Database Administrators are often exposed to a situation where they want to query a table, check on the status of a server, check an error log, or run a DBCC command on a SQL Server box when they do not have access to the box directly. This often happens when they are off site or on vacation or they do not have remote clients etc., but have access to the Internet.

This article describes how you can query SQL Server using your email and get the results back as E-Mail attachments.

Pre-requisite

  1. SQLMail is configured and up and running on a box.
  2. Your SQLMail account can receive emails.
  3. Mail client (MS-Outlook) is open and running on the box where we are going to create all the procedures and jobs.

How to Configure?

Step 1: "Create Procedures"

Execute the below script on the SQL box where SQL-Mail is setup:

SQL
use master

go

CREATE procedure usp_osqlexecute 

@servername varchar(128) =@@servername,

@Databasename varchar(128)='Master',

@authentication varchar(128)=' -E ',

@Query varchar(2000) = 'sp_readerrorlog'

as

declare @formsql varchar(3000)

set @formsql = 'osql -S'+@servername +' 

 -d'+@Databasename+' -Q"'+@Query+ '"

' +@authentication + ' >c:\output.txt'

set @formsql ='master.dbo.xp_cmdshell '''+ @formsql+''''

print @formsql

exec (@formsql)

go


CREATE Procedure usp_readmail as

declare @message varchar(1000)

declare @message2 varchar(1000)

declare @start int

declare @len int

declare @sendmailto varchar(128)

declare @count int

create table #messages (Originator varchar(1000)

,date datetime,recipients varchar(1000),cclist varchar(300),

bcclist varchar(300),subject varchar(300),message ntext,

unread varchar(10),attachments varchar(1000),messageid varchar(1000),

type varchar(1000))

set @sendmailto=' '

insert #messages exec "xp_readmail"

set @count = (select count(*) from #messages where unread='true')

if @count>0

begin

set @message=(select convert(varchar(1000),message) as message from #messages 

where date = (select max(date) from #messages) and unread='true')

set @message2 = @message

set @start =charindex('<query>',@message)+7

set @len = charindex('</query>',@message)-@start

set @message= substring(@message,@start,@len)

exec (@message)

set @start =charindex('<email>',@message2)+7

set @len = charindex('</email>',@message2)-@start

set @sendmailto= substring(@message2,@start,@len)


exec master.dbo.xp_sendmail @recipients=@sendmailto,@attachments= 'c:\output.txt'

end

Step2: "Change Mail Client Options"

In your mail client, change the mail options to the settings displayed below:

Mail Settings

Step3: "Create Job"

Create a job that executes the above-created procedure (usp_readmail) every 1 minute.

Job Properties

Job Properties

Job Properties

How to query the server?

Let's assume you work for XYZcompany and your SQLMail email account is SQLMail@mysqlmail.com. Use any of your email accounts, for example, Yahoo!, Hotmail, your local broadband email, or your company's web-mail, and send email to SQLMail@sqlmails.com in the following format:

Mail query

In a few minutes, you will have results, similar as those displayed below, as an attachment in your email.

Query Resultin Mail

Query format

I use this query format because whenever you use any free email accounts, such as Yahoo! or Hotmail, there are additional text advertisements and signature attached to the email. By using this format, we can filter out the query and email accounts from those advertisements.

  1. By default, the procedure usp_osqlexecute will read the error log and send back the results as an attachment.

    Example:

    SQL
    <query>usp_osqlexecute 
      </query><email>jak@mymail.com</email>
  2. Want to send the results to many people?

    Example:

    SQL
    <query>usp_osqlexecute
    
    </query><email>jak@mymail.com;reachme@yahoo.com</email>
  3. Simple Queries

    Example:

    SQL
    <query>usp_osqlexecute @servername="SQL2k",
    
    @Query="Select name from sysobjects", @Databasename=Payroll 
         </query><email>jak@mymail.com</email>
  4. Need to read the error log of a different server?

    Example:

    SQL
    <query>usp_osqlexecute
    
    @servername="SQL2k\instance1"</query><email>jak@mymail.com</email>
  5. Want to run DBCC on a database?

    Example:

    SQL
    <query>usp_osqlexecute @Query = "DBCC Checkdb", @Databasename ="PAY" , 
    
    @Servername ="sql2k\instance1"</query><email>jak@mymail.com</email>
  6. Need to use SQL authentication instead of Windows authentication?

    Example:

    SQL
    <query>usp_osqlexecute @servername="SQL2k\instance1", @authentication=" -Usa 
    
    -Pyeahright "</query><email>jak@mymail.com</email>
  7. Need to delete a huge log table that you forgot to delete?

    Example:

    SQL
    <query>usp_osqlexecute
    
    @Query = "Delete from Logtable where date>=getdate()-10", 
    
    @Databasename ="MAK" , @Servername
    
    ="sql2k\instance1"</query><email>jak@mymail.com</email>

Note: you can limit the users accessing this feature by adding security such as RC4 encryption to the query format, and/or evaluate where the email is coming from, and/or an additional query tag (such as <user>JAK</User>) can be used to verify authentication.

Conclusion:

With this method, wherever you have access to the Internet, you can be in touch with your SQL Server boxes. Using the methods described in this article, you can do all of the work that you can to do on a query analyzer from a remote location with a little latency.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
Chaudhary is currently working as software engineer; His strong points are his passion and Hardworking, and commitment. He is even an Electronics and telecomm Engineer; Apart from Software his interests are Photography, Gardening, Electronics, he is hobbyist. Currently in his time he is working on some telecommunication communication system from past couple of years.

Comments and Discussions

 
GeneralRe: security... Pin
Chaudhary5-Feb-04 5:03
Chaudhary5-Feb-04 5:03 
Generalsecurity... Pin
l a u r e n4-Feb-04 10:01
l a u r e n4-Feb-04 10:01 
GeneralRe: security... Pin
dog_spawn4-Feb-04 11:02
dog_spawn4-Feb-04 11:02 
l a u r e n wrote:
falling off my chair (which i did when i read this article)

When reading new CP articles it is very difficult to stay upright and keep your sides nicely non-split. Anyway...

Also I want to know why I should stop connecting to SQL Servers normally. All the securtiy is already in place in Windows and SQL Server and you get to use a nice GUI, so what is the point of this article? I think it is interesting though.
GeneralRe: security... Pin
Chaudhary4-Feb-04 20:33
Chaudhary4-Feb-04 20:33 
GeneralRe: security... Pin
dog_spawn5-Feb-04 2:53
dog_spawn5-Feb-04 2:53 
GeneralRe: security... Pin
Daniel Turini5-Feb-04 0:49
Daniel Turini5-Feb-04 0:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.