473,396 Members | 1,996 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

get DB name from within stored procedure?

Bas
Hi,
Using SQL 2k SP3

I want to know if there's a way to retrieve the name of the database a
stored proc is run in.

For example:

CREATE PROCEDURE HelloWorld
AS
PRINT 'Hello world, this proc is run in: ' + CAST(GetDatabaseName AS
nvarchar)
GO

GetDatabaseName here is fictitious ofcourse , what would be the right
command? I can't seem to find it.

Regards,

Bas
----
Okay, okay, i'll explay what I really want with it, I want something like
below (yes it's quick 'n' dirty), but without having to pass the db name:

CREATE PROCEDURE CheckDBProperties
@DBName nvarchar(1000),
@Error nvarchar(1000) OUTPUT
AS

DECLARE @Result sql_variant
SELECT @Result = DATABASEPROPERTYEX(@DBName, 'IsAnsiNullDefault')
IF @Result <> 1
SET @Error = @Error + 'IsAnsiNullDefault has a wrong setting: ' +
CAST(@Result AS nvarchar(1)) + CHAR(13) + CHAR(10)

SELECT @Result = DATABASEPROPERTYEX(@DBName, 'IsAnsiNullsEnabled')
IF @Result <> 1
SET @Error = @Error + 'IsAnsiNullsEnabled has a wrong setting: ' +
CAST(@Result AS nvarchar(1)) + CHAR(13) + CHAR(10)

SELECT @Result = DATABASEPROPERTYEX(@DBName, 'IsAnsiPaddingEnabled')
IF @Result <> 1
SET @Error = @Error + 'IsAnsiPaddingEnabled has a wrong setting: ' +
CAST(@Result AS nvarchar(1)) + CHAR(13) + CHAR(10)
GO

DECLARE @FLEP nvarchar(1000)
SET @FLEP = ''
EXEC CheckDBProperties 'MyDB', @FLEP OUTPUT
PRINT @FLEP
Jul 20 '05 #1
2 8075

"Bas" <nomailplease> wrote in message
news:40***********************@dreader8.news.xs4al l.nl...
Hi,
Using SQL 2k SP3

I want to know if there's a way to retrieve the name of the database a
stored proc is run in.

For example:

CREATE PROCEDURE HelloWorld
AS
PRINT 'Hello world, this proc is run in: ' + CAST(GetDatabaseName AS
nvarchar)
GO

GetDatabaseName here is fictitious ofcourse , what would be the right
command? I can't seem to find it.

Regards,

Bas
----
Okay, okay, i'll explay what I really want with it, I want something like
below (yes it's quick 'n' dirty), but without having to pass the db name:

CREATE PROCEDURE CheckDBProperties
@DBName nvarchar(1000),
@Error nvarchar(1000) OUTPUT
AS

DECLARE @Result sql_variant
SELECT @Result = DATABASEPROPERTYEX(@DBName, 'IsAnsiNullDefault')
IF @Result <> 1
SET @Error = @Error + 'IsAnsiNullDefault has a wrong setting: ' +
CAST(@Result AS nvarchar(1)) + CHAR(13) + CHAR(10)

SELECT @Result = DATABASEPROPERTYEX(@DBName, 'IsAnsiNullsEnabled')
IF @Result <> 1
SET @Error = @Error + 'IsAnsiNullsEnabled has a wrong setting: ' +
CAST(@Result AS nvarchar(1)) + CHAR(13) + CHAR(10)

SELECT @Result = DATABASEPROPERTYEX(@DBName, 'IsAnsiPaddingEnabled')
IF @Result <> 1
SET @Error = @Error + 'IsAnsiPaddingEnabled has a wrong setting: ' +
CAST(@Result AS nvarchar(1)) + CHAR(13) + CHAR(10)
GO

DECLARE @FLEP nvarchar(1000)
SET @FLEP = ''
EXEC CheckDBProperties 'MyDB', @FLEP OUTPUT
PRINT @FLEP


select db_name()

But why not try something like this:

select name,
case databasepropertyex(name, 'IsAnsiNullDefault') when 1 then 'Pass'
else 'Fail' end as 'IsAnsiNullDefault',
case databasepropertyex(name, 'IsAnsiNullsEnabled') when 1 then 'Pass'
else 'Fail' end as 'IsAnsiNullsEnabled',
case databasepropertyex(name, 'IsAnsiPaddingEnabled') when 1 then 'Pass'
else 'Fail' end as 'IsAnsiPaddingEnabled'
from master.dbo.sysdatabases
where -- name = db_name() and /* If you really want to do one DB only */
(databasepropertyex(name, 'IsAnsiNullDefault') <> 1
or databasepropertyex(name, 'IsAnsiPaddingEnabled') <> 1
or databasepropertyex(name, 'IsAnsiPaddingEnabled') <> 1)
order by
name

Returning a complete result set makes it easier to quickly see the
exceptions, and you can easily put it in an .xls, parse it with an ADO
ResultSet object etc.

Simon
Jul 20 '05 #2
Bas
"Simon Hayes" <sq*@hayes.ch> wrote in message
news:40**********@news.bluewin.ch...

"Bas" <nomailplease> wrote in message
news:40***********************@dreader8.news.xs4al l.nl...
I want to know if there's a way to retrieve the name of the database a
stored proc is run in.
select db_name()

But why not try something like this:

select name,
case databasepropertyex(name, 'IsAnsiNullDefault') when 1 then 'Pass'
else 'Fail' end as 'IsAnsiNullDefault',
case databasepropertyex(name, 'IsAnsiNullsEnabled') when 1 then 'Pass'
else 'Fail' end as 'IsAnsiNullsEnabled',
case databasepropertyex(name, 'IsAnsiPaddingEnabled') when 1 then 'Pass' else 'Fail' end as 'IsAnsiPaddingEnabled'
from master.dbo.sysdatabases
where -- name = db_name() and /* If you really want to do one DB only */
(databasepropertyex(name, 'IsAnsiNullDefault') <> 1
or databasepropertyex(name, 'IsAnsiPaddingEnabled') <> 1
or databasepropertyex(name, 'IsAnsiPaddingEnabled') <> 1)
order by
name

Returning a complete result set makes it easier to quickly see the
exceptions, and you can easily put it in an .xls, parse it with an ADO
ResultSet object etc.


Thanks for the swift response. I'll toy around with your suggestion because
it's cleaner. I want to check DB settings using ADO at startup of my app,
because a missing ARITHABORT ON cost me a couple of hours bughunting today
:(
Cheers,

Bas
Jul 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Mariusz | last post by:
I want to write function to call another function which name is parameter to first function. Other parameters should be passed to called function. If I call it function('f1',10) it should call...
1
by: Rick | last post by:
I have the following stored procedure in SQL 2000 and would like to diplay the database name where data is drawn from. I'm using 4 databases db1, db2, db3, db4 which all have the same table...
6
by: dharmadam | last post by:
Is it possible to pass a column name or the order of the column name in the DB2 table table function. For example, I want to update the address of a person by passing one of the address column name...
1
by: Rob Richardson | last post by:
Greetings! I have a SQL Server stored procedure that takes one parameter, named @key. I am trying to run that stored procedure from a VB app. When I create a Parameter object and give it the...
4
by: Robert E. Flaherty | last post by:
I know that you can call SQL Server from a web service but from within a stored procedure can you call a web service?
1
by: benfly08 | last post by:
Hi, guys. I just wonder whether a space within column name is allowed in SQL Server 2000(i.e. "Item Number" as column name). In a stored procedure i saw: Create table #temp (ExpiryDate...
0
by: Riaaaa | last post by:
Hi frdz, I have created the form for entering the company details with its general information in asp.net C# 2005. I want to put the validation that the user cannot enter the same name...
1
by: rshivaraman | last post by:
Hi All : A couple of tables have been identified to be deleted. My job is to find if it is at all used. On searching the web, i found a proc to search for a string within all databases in a...
6
by: antmail | last post by:
Hi guys, I have spend several days now trying to solve this problem inside a stored procedure. I want to call a procedure providing 3 variables. The variables area used to select the appropriate...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.