Showing posts with label determine. Show all posts
Showing posts with label determine. Show all posts

Thursday, March 29, 2012

database name

Is there a way to determine the current database the session is using? I want
to use this to determine if a script should proceed. It should only proceed
if the current database is not a system database.
I was thinking something like,
IF (select dbid < 6) THEN RAISERROR.
Thoughts? Thanks!
Check books online for db_name.
If you execute something like:
select db_name()
you will get the current database name.
-Sue
On Mon, 13 Sep 2004 16:41:12 -0700, "Bevo"
<Bevo@.discussions.microsoft.com> wrote:

>Is there a way to determine the current database the session is using? I want
>to use this to determine if a script should proceed. It should only proceed
>if the current database is not a system database.
>I was thinking something like,
>IF (select dbid < 6) THEN RAISERROR.
>Thoughts? Thanks!
|||if ( db_name != 'mydb') Raiserror ('Incorrect database',16,1)
Wayne Snyder, MCDBA, SQL Server MVP
Mariner, Charlotte, NC
www.mariner-usa.com
(Please respond only to the newsgroups.)
I support the Professional Association of SQL Server (PASS) and it's
community of SQL Server professionals.
www.sqlpass.org
"Bevo" <Bevo@.discussions.microsoft.com> wrote in message
news:4A5919FA-B5A6-4B2E-BF05-F681FBA08019@.microsoft.com...
> Is there a way to determine the current database the session is using? I
want
> to use this to determine if a script should proceed. It should only
proceed
> if the current database is not a system database.
> I was thinking something like,
> IF (select dbid < 6) THEN RAISERROR.
> Thoughts? Thanks!
|||I will not know the name of the database to be used, but I will know that it
is not a system database.
Can I be sure that all database ids greater than 6 are not system databases?
If so, the logic would be this:
if ( db_id(db_name()) < 6) Raiserror ('Must not be a system database',16,1)
"Wayne Snyder" wrote:

> if ( db_name != 'mydb') Raiserror ('Incorrect database',16,1)
>
> --
> Wayne Snyder, MCDBA, SQL Server MVP
> Mariner, Charlotte, NC
> www.mariner-usa.com
> (Please respond only to the newsgroups.)
> I support the Professional Association of SQL Server (PASS) and it's
> community of SQL Server professionals.
> www.sqlpass.org
> "Bevo" <Bevo@.discussions.microsoft.com> wrote in message
> news:4A5919FA-B5A6-4B2E-BF05-F681FBA08019@.microsoft.com...
> want
> proceed
>
>
|||There is no need for db_name().
e.g.
If (db_id()<6) Raiserror ('Must not be a system included database',16,1)
Howerver, it's best to be explicit because you would never know if there
would be more than 6 system included databases. Plus, the db_id for the 7th
might be >6. Thus,
If (db_name() in ('master','msdb','tempdb','model','Northwind','Pub s'))
Raiserror ('Must not be a system included database',16,1)
"Bevo" <Bevo@.discussions.microsoft.com> wrote in message
news:10284D82-E541-46F4-B3C3-C76AA021BA40@.microsoft.com...
> I will not know the name of the database to be used, but I will know that
it
> is not a system database.
> Can I be sure that all database ids greater than 6 are not system
databases?
> If so, the logic would be this:
> if ( db_id(db_name()) < 6) Raiserror ('Must not be a system
database',16,1)[vbcol=seagreen]
>
sql

database name

Is there a way to determine the current database the session is using? I want
to use this to determine if a script should proceed. It should only proceed
if the current database is not a system database.
I was thinking something like,
IF (select dbid < 6) THEN RAISERROR.
Thoughts' Thanks!Check books online for db_name.
If you execute something like:
select db_name()
you will get the current database name.
-Sue
On Mon, 13 Sep 2004 16:41:12 -0700, "Bevo"
<Bevo@.discussions.microsoft.com> wrote:
>Is there a way to determine the current database the session is using? I want
>to use this to determine if a script should proceed. It should only proceed
>if the current database is not a system database.
>I was thinking something like,
>IF (select dbid < 6) THEN RAISERROR.
>Thoughts' Thanks!|||if ( db_name != 'mydb') Raiserror ('Incorrect database',16,1)
Wayne Snyder, MCDBA, SQL Server MVP
Mariner, Charlotte, NC
www.mariner-usa.com
(Please respond only to the newsgroups.)
I support the Professional Association of SQL Server (PASS) and it's
community of SQL Server professionals.
www.sqlpass.org
"Bevo" <Bevo@.discussions.microsoft.com> wrote in message
news:4A5919FA-B5A6-4B2E-BF05-F681FBA08019@.microsoft.com...
> Is there a way to determine the current database the session is using? I
want
> to use this to determine if a script should proceed. It should only
proceed
> if the current database is not a system database.
> I was thinking something like,
> IF (select dbid < 6) THEN RAISERROR.
> Thoughts' Thanks!|||I will not know the name of the database to be used, but I will know that it
is not a system database.
Can I be sure that all database ids greater than 6 are not system databases?
If so, the logic would be this:
if ( db_id(db_name()) < 6) Raiserror ('Must not be a system database',16,1)
"Wayne Snyder" wrote:
> if ( db_name != 'mydb') Raiserror ('Incorrect database',16,1)
>
> --
> Wayne Snyder, MCDBA, SQL Server MVP
> Mariner, Charlotte, NC
> www.mariner-usa.com
> (Please respond only to the newsgroups.)
> I support the Professional Association of SQL Server (PASS) and it's
> community of SQL Server professionals.
> www.sqlpass.org
> "Bevo" <Bevo@.discussions.microsoft.com> wrote in message
> news:4A5919FA-B5A6-4B2E-BF05-F681FBA08019@.microsoft.com...
> > Is there a way to determine the current database the session is using? I
> want
> > to use this to determine if a script should proceed. It should only
> proceed
> > if the current database is not a system database.
> >
> > I was thinking something like,
> >
> > IF (select dbid < 6) THEN RAISERROR.
> >
> > Thoughts' Thanks!
>
>|||There is no need for db_name().
e.g.
If (db_id()<6) Raiserror ('Must not be a system included database',16,1)
Howerver, it's best to be explicit because you would never know if there
would be more than 6 system included databases. Plus, the db_id for the 7th
might be >6. Thus,
If (db_name() in ('master','msdb','tempdb','model','Northwind','Pubs'))
Raiserror ('Must not be a system included database',16,1)
"Bevo" <Bevo@.discussions.microsoft.com> wrote in message
news:10284D82-E541-46F4-B3C3-C76AA021BA40@.microsoft.com...
> I will not know the name of the database to be used, but I will know that
it
> is not a system database.
> Can I be sure that all database ids greater than 6 are not system
databases?
> If so, the logic would be this:
> if ( db_id(db_name()) < 6) Raiserror ('Must not be a system
database',16,1)
>
> >

Saturday, February 25, 2012

Database Mail Problems / Job Notifications...

I've read an extensive bit about this problem in several forums and so far none of the information has helped me to determine if there is a bug in SQL 2005 x64 regardless of version or SP or if I just don't know how to do this correctly.

I can send a test email from db mail but when a job fails it does not send an email to an operator. I have monitored the IIS logs to see if it is trying to send and it is not. Database mail is enabled in Surface AC and in SQL Agent [one odd thing is that the 'test' button is only available on the SQL Agent Alerts page if I use SQL Mail]. No specific error happens (in the mail log, sql agent log, or application event log) when I try to send mail from a failed job. Mail accounts and credentials are valid. Please note that in the steps listed below, I have restarted the SQL Agent Service each time I make a change.

I've tried deleting and recreating the operators (they are enabled). I've tried using a mail server that uses anonymous authentication as well (I changed this back to one that uses basic authentication). The profiles were made default and public. (Though this shouldn't ever have to be done, as the account is a sysadmin) the SQLServer2005SQLAgentUser was explicitly added to the msdb DatabaseMail user role.

This query works, so I know that the job is not trying to execute the sp:

USE msdb ;

GO

EXEC dbo.sp_notify_operator

@.profile_name = N'staging monitor',

@.name = N'Charles Evans',

@.subject = N'Test Notification',

@.body = N'Successfully emailed profile.' ;

GO

This is happening in SQL 2005 x64 EE and x64 DE SP1 and SP2 respectively. Any thoughts on what else this could be? The same setup works fine in SQL 2005 SE x86 SP1.

Check the following article...

How to configure SQL Server Agent to send job status notifications and alert notifications in SQL Server 2005 64-bit editions

http://support.microsoft.com/kb/908360

|||So the only way is to use SQL Mail, instead of database mail?|||

SQL Server Agent is not integrated with Database Mail or with SQL Mail in Microsoft SQL Server 2005 64-bit editions.

Please read the article... you can setup mail using SMTP where SQL mail uses MAPI ...

|||

In SP0 this is true - according to the article:

"In SQL Server 2005 Service Pack 1 (SP1), you can use SQL Server Agent to interact with Database Mail as the mailing system on 64-bit operating systems."

I've made this work in our production system now with SP-1 EE x64, there was a permissions issue. However, things are not working in our staging environment SP-2 DE x64 with the same setup and account settings/permissions.

I'll keep posted if I figure out why not in staging.

Tuesday, February 14, 2012

Database last accessed

Hello All,
Is there a way to figure out/track when was the last time/date a
particular database was accessed by users? I am trying to determine
which databases haven't been used/accessed by users in a while so I can
find out if they are still needed or not. My environment is SQL 2000
SP3 with Windows 2000 as the OS. Any help will be appreciated.
Thanks,
Raziq.
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!Hi,
First option will be enable Profiler , otherwise write triggers to identify
the last accessed database.
Thanks
Hari
MCDBA
"Raziq Shekha" <raziq_shekha@.anadarko.com> wrote in message
news:eiZlyPr2DHA.1700@.TK2MSFTNGP12.phx.gbl...
> Hello All,
> Is there a way to figure out/track when was the last time/date a
> particular database was accessed by users? I am trying to determine
> which databases haven't been used/accessed by users in a while so I can
> find out if they are still needed or not. My environment is SQL 2000
> SP3 with Windows 2000 as the OS. Any help will be appreciated.
> Thanks,
> Raziq.
>
> *** Sent via Developersdex http://www.developersdex.com ***
> Don't just participate in USENET...get rewarded for it!