database mail problem

What to do if database mail is not working.

Mail was not being sent. First I queried

SELECT * FROM msdb.dbo.sysmail_event_log

and saw in the description field

“The mail could not be sent to the recipients because of the mail server failure. (Sending Mail using Account 1 (2009-03-31T12:24:10). Exception Message: Could not connect to mail server. (A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond [ip address]:[port number]). )”

Running

EXEC msdb.dbo.sysmail_help_status_sp ;

returned “inactive”

Some websites suggested that the people trying to send mail need to be in the msdb role DatabaseMailUserRole, so I checked that

EXEC msdb.sys.sp_helprolemember ‘DatabaseMailUserRole’ ;

but then I remembered I am a sysadmin and am not able to send mail.

Then I practically ran this

select name , is_broker_enabled from sys.databases

and this and then could send mail:

alter database msdb set single_user with rollback immediate
alter database msdb set enable_broker
alter database msdb set MULTI_USER

These procedures may be helpful too:

exec msdb..sysmail_help_principalprofile_sp
EXEC msdb.dbo.sysmail_help_queue_sp @queue_type = ‘mail’ ;
exec msdb..sysmail_stop_sp
exec msdb..sysmail_start_sp

Except the jobs still could not send mail. For some reason sql server agent did not have database mail set up. After pointing and clicking to get that together I thought I was done, except now I got this error:

The mail could not be sent to the recipients because of the mail server failure. (Sending Mail using Account 1 (2009-07-06T14:10:00). Exception Message: Cannot send mails to mail server. (The specified string is not in the form required for an e-mail address.). )

This error was because the operators were defined with mapi names and not full email addresses.

Whew done!

Posted in DBA | Leave a comment

sql traces

I was asked to trace department’s access to a certain sql server here.

To create a trace that is run by sql server, open profiler and define your trace. Then, while it is not running, you can script it by going to the File-Script Trace menu and save the sql.

Either make a stored procedure which contains the sql or just paste in into a job step. Have your job run when sql server agent starts or set your procedure as a startup procedure with sp_procoption.

Once the trace is running you can check on it with:
SELECT * FROM ::fn_trace_getinfo ([the trace id])

Stop it with:
exec msdb..sp_trace_setstatus [the trace id] , 0 — stop the trace
exec msdb..sp_trace_setstatus [the trace id] , 2 — close and delete the trace defination

I also added the following so if there was an existing output file it would be renamed.
exec master..xp_cmdshell ‘ren [path to your trace output] “%date:~10,4%%date:~4,2%%date:~7,2%_%time:~0,2%%time:~3,2%%time:~6,2%-[some common name]”‘

Also, I increased sp_trace_create’s parameters @options to rollover to a new file and @maxfilesize to a much larger file size just in case.

Later I will import the results into sql server and total the cpu, duration, and reads and writes by workstation.

Posted in DBA | Leave a comment

Using SQL DMO to read DTS Packages

There is a column name and defination change and lots of code has to be examined. The developers were worried about looking through the many DTS packages. Happily the contents can be queried using sql-dmo.

The short of it is looping through all the DTS packages, one at a time setting a reference to is with the DTS.Package2 object, and then looping through the tasks and the task’s properties.

I made a table to insert each package’s, task’s, and property’s values, including the server too and then was able to query for the column in questio. Woo-hoo!

This came in handy for looking for code within view, procedures, etc:
exec sp_msforeachdb ‘use [?] ; select distinct db_name(), name from sysobjects so join syscomments sc on so.id = sc.id where sc.text like ”%<>%”’

Posted in SQL DMO | Tagged | Leave a comment

Finding a job

One of my friends asked me how I found a job so fast after such a long vacation. Here is what I wrote to her:

  • Does your resume shout out that you meet the job poster’s qualifications requirements? 
    Here is my resume; do a search for “sql server dba” at dice.com or some other site and see how I basically show how I’ve done what they’re asking for. Of course I am not a 100% match for every post…. but you get the picture.
  • Are you doing the “linked in” thing and looking there too?
    Read this for some ideas.
  • Are you really taking honest time every day to do EVERYTHING you can to get a job? This is a serious question that you have to answer by taking some time and being sincere with yourself.
    For example, here is one thing you can do: volunteer at a place where you’d like to work for a day or two a week. Or offering to work for cheap for a week so they can see how much they need you if the interview doesn’t go well.
  • Do a search on “interview questions” and be an expert at interviewing. This REALLY REALLY helped me when I got a call back (I also searched for interview questions for my field and studied hard; people dig small facts).

Good luck to everyone with their search.

Posted in DBA, Employment | Leave a comment

using sp_msforeachdb

I really like sp_msforeachdb. For example, here is the sql to my regular maintenance jobs:

  • exec sp_MSforeachdb ‘DBCC CHECKDB(?) with NO_INFOMSGS, ALL_ERRORMSGS’
  • exec sp_msforeachdb ‘if db_id(”?”) <> db_id(”master”) and db_id(”?”) <> db_id(”tempdb”) begin use [?];exec sp_dbreindex_or_indexdefrag;end’
  • exec sp_msforeachdb ‘if db_id(”?”) <> db_id(”master”) and db_id(”?”) <> db_id(”tempdb”) begin use [?];exec sp_updatestats;end’

Yesterday I was reducing the number of vlfs and to check I’d covered all databases I ran:

  • sp_msforeachdb ‘use ?;select db_name();dbcc loginfo’

See what I mean?

The only hitch is if your database name has a reserved character – here there are some with a dash – you’ll need to do [?] instead of just ?.

Posted in Day to day, DBA | Leave a comment

Concatenating rows

We decided to use the data driven subscriptions for SSRS. This means storing all kinds of info in a database and then having to present it to the report server as subscription parameters. Specifically we have to concatenate a bunch of emails representing subscribers into a single email column which is then mapped for a data driven subscription. I did this using CTEs. I saw a bunch of examples but this site had them all in one handy place.

Posted in SQL, SSRS | Tagged | Leave a comment

Removing a startup stored procedure

In my combing through of the sql servers I found a startup stored procedure on one of the sql servers here. I didn’t recognize the tables it created or its name, so I asked around: no one else knew anything either. How to gracefully deal with this? Simply remove its startup property creates a timebomb. I decided to alert the production services manager, who gets all the issue emails, send what to do if there is a complaint and I am not available, and then drop what the procedure creates and hold my breath.

use tempdb
go
drop table <<table name>>
drop table <<table name>>
 
— remove it as a startup procedure
use master
go
exec sp_procoption @procname = ‘<<proc name>>’, @optionname = ‘startup’, @optionvalue = ‘false’
go
 
— shows that there are no startup procedures
select name from sysobjects where type = ‘p’ and OBJECTPROPERTY(id, ‘ExecIsStartup’) = 1

— So, if people complain, then run 
exec master..<<proc name>>
exec sp_procoption @procname = ‘<<proc name>>’, @optionname = ‘startup’, @optionvalue = ‘true’

Posted in DBA | Leave a comment

Cleaning up sql job ownership

We have a lot of jobs owned by many different accounts due to the currently lax security. Before cleaning up the local admins group and removing all sysadmin role members it is important to make sure the jobs that are currently running with elevated privileges remain running with elevated privileges. Something like this site explains it well enough.

select sj.name , sl.name
from msdb..sysjobs sj join master..syslogins sl on sj.owner_sid = sl.sid
where sl.name <> ‘sa’

How to generate the sql to make those jobs be owned by sa:

select ‘exec sp_update_job @job_name = ”’ + sj.name + ”’ , @owner_login_name = ”sa”’
from msdb..sysjobs sj join master..syslogins sl on sj.owner_sid = sl.sid
where sl.name <> ‘sa’

Posted in Day to day, DBA | Tagged | Leave a comment

SQL Server 2000 sp4 and Memory

I configured 3 of the development sql servers to use the available memory last night. 2 had 8gb; 1 had 4. The one with 4 just required adding the /3GB and a restart; for the other two I modified the boot.ini, restarted the server, and then ran sp_configure to set the max memory and to enable awe. They only claimed 3gb of the available memory though.

After some searching I found hotfix 899761 is needed for sql server 2000 sp4.  If the sql server version is 8.00.039 and it isn’t using all the available memory, apply the hotfix and get to 8.00.040. How long has sp4 been out and where have I been? A story for another time.

I’ve been referring to this article for years to find what combo of PAE and 3GB to use.

The first time I did this on Windows Server 2003 I remember crapping in my pants because I couldn’t figure out why sql server didn’t grab the memory like it did with Windows 2000.

Posted in DBA | Leave a comment

SQL Blogs I like

http://www.sqlskills.com/BLOGS/PAUL/

http://www.sqlskills.com/BLOGS/KIMBERLY/

http://sqlfool.com/

http://sqlinthewild.co.za/

Posted in Day to day, DBA | Leave a comment