Can I query a database property without specifying the database name directly? - sql-server

I'm trying to query the maximum size of a SQL Azure database using code from this answer:
SELECT CONVERT(BIGINT, DATABASEPROPERTYEX('DatabaseOfInterestName', 'MaxSizeInBytes')) / 1024
The problem is I need to pass the database name there. Since I need this code in a Windows Azure application which has different configurations - for production use, for automatic build and for testing - and each configuration would use its own database I'll have to pass the database name into that SQL query and that's some extra wiring in my code.
Is there a way to tell DATABASEPROPERTYEX() to query the property "from the current database" without specifying the database name explicitly?

Would the DB_NAME() function do your job?
SELECT CONVERT(BIGINT, DATABASEPROPERTYEX(DB_NAME(), 'MaxSizeInBytes')) / 1024

No and yes. No, you can not get DATABASEPROPERTYEX to use the current database.
BUt there is SQL to get the current database ;)
http://blog.sqlauthority.com/2008/02/12/sql-server-get-current-database-name/
The answer is:
SELECT DB_NAME() AS DataBaseName
;)
Use DB_NAME() as input to DATABASEPROPERTYEX.

Related

DB Link from Oracle to MS Server

I am new to this forum and already searched for 45 minutes to find a solution for my problem. I hope you can help me.
A Gateway to a remote Microsoft SQL Database was installed on a Oracle Server (Oracle 12c). The tsnnames.ora file was appropiately set up.
For the connection, I created a database link (In the Oracle DB) as follows:
CREATE DATABASE LINK TEL CONNECT TO "fb_B2C" IDENTIFIED BY "passwort" USING 'dg1msql';
When I now execute the Select statement:
SELECT "name" FROM "sys"."databases"#TEL
it shows me the according databases. Among others, I can see the AB_Colors database.
Now, I want to select a view in the AB_Colors database.
Due to the fact I can connect to this database via Excel, I know that in the database AB_Colors, there are 10 Views(A,B,C,..). I would like to select the View C from the database AB_Colors via the DB LInk.
Owner of the View is b2b.
How do i need to formulate the select statement to do it?
I already tried different writings:
SELECT * FROM b2b.C#TEL;
SELECT * FROM "AB_colors"."b2b"."C"#TEL;
SELECT * FROM [AB_Colors].[b2b].[C]#TEL;
The common error message is: View/Table does not exist
I highly appreciate your help,
Fedja
This is the correct format
SELECT * FROM "b2b"."C"#TEL;
The issue maybe because the database you want to select from is not the one specified in the gateway for dg1msql. You can't add the database name to the query so you must specify it in the gateway connection.
This is defined in
$ORACLE_HOME/dg4msql/admin/initdg4msql.ora
where you should check HS_FDS_CONNECT_INFO

Call SQL Server functions via Oracle database link

Is there a way to execute the following statement from Oracle using a database link to a SQL Server instance without having to create a view on the target instance?
select db_name(), ##SERVERNAME
I have tried
"select db_name(), ##SERVERNAME"#DbLink
but that did not succeed.
Any help would be greatly appreciated
Here:
select dbo.FunctionName#dblink('value') from dual;
But you need to tell the dblink which functions must be available through the dblink. See the manual at: http://docs.oracle.com/cd/E11882_01/server.112/e11050/admin.htm#i1007467

how can I enumerate the databases of a sql server

Given an instance of SQL server, what's the best way to enumerate the databases?
In SQL Server 2000+:
select * from sysdatabases
In SQL Server 2005+:
select * from sys.databases
The difference is subtle and barely worth mentioning for a one-liner like this. But depending on how much you're going to be accessing the system catalog, you may get some use out of this article:
Querying the SQL Server System Catalog
You can also execute sp_helpdb without an argument to get basic information about all databases. (Pass in a database name as an argument to get more detailed information about that database).
Try this:
SELECT [name] FROM sys.sysdatabases
You could also get this from exec sp_databases;
These commands will work from SQL 2000+.
EXECUTE master.sys.sp_MSforeachdb 'USE [?]; EXEC sp_spaceused'

On SQL Server 2008, how to find out when was any database offline/online?

I have to include one report in my application showing offline/online activity of few databases on SQL Server 2008.
Could you please suggest how can I collect teh same information from sql server?
SELECT DATABASEPROPERTYEX('YOURDATABASE', 'Status')
DatabaseStatus_DATABASEPROPERTYEX
GO
SELECT state_desc DatabaseStatus_sysDatabase
FROM sys.databases
WHERE name = 'YOURDATABASE'
GO
This will tell you the status of the database.
In order to find out when your database was taken OFFLINE, you can use the SQL that I posted before, or the easiest way is to check the Event Viewer and it will tell you when the Database was taken OFFLINE. I have just tested this on my local machine and SQL Server writes out an Information message to the Application log.
You can also use below query to check database status.
SELECT Name, state_desc FROM sys.databases

How can I get the name of the database server from within a stored procedure?

I'm making complete connection strings inside my procedure and would like to inject the name of the database server in them. Is there any way that I can detect the name from inside or am I doomed to passing it in?
Use SERVERPROPERTY:
SERVERPROPERTY('MachineName'): name of the SQL Server host name, cluster aware
SERVERPROPERTY('ComputerNamePhysicalNetBIOS'): name of physical machine name. In a cluster, is the name of the current active node. On a standalone instalation, is identical with MachineName
SERVERPROPERTY('InstanceName'): name of the current SQL Server instance. NULL for default.
One thing I'd recomend against is the dreaded ##SERVERNAME. This property is notorious for getting out of sync with reality after a machine rename. I've seen way too many apps burned by this problem to place any trust on it. The correct rename procedure is in BOL, but few use it properly: How to: Rename a Computer that Hosts a Stand-Alone Instance of SQL Server.
So for MS SQL Server you could:
SELECT ##SERVERNAME
For MySQL, it's
SELECT variable_value as servername
FROM information_schema.global_variables
WHERE variable_name = 'hostname';
For Oracle, its:
SELECT global_name FROM global_name

Resources