Question

Hello SQL Server experts,

I have a strange issue that I'm hoping to get some feedback on potential solutions for: I am working with an application which requires an ODBC connection to an existing SQL database. However, the database contains a dash ("-") in the name, which causes a syntax error in the application (obviously not an outstanding development effort, but there is nothing I can do about this).

As a temporary solution to the problem, I created a copy of the database and renamed it without the dash. This target database is only used for specific tasks and is not updated often, however it does update every few months, which means that I periodically have to go in and refresh it. I was expecting to have a solution from the developer by now, but they are not cooperating.

My question is: Is there some solution to this issue which wouldn't involve me having to refresh my copy of the database? I should mention that renaming the original database is not an option. Should I just setup a SQL Job to do a periodic restore of the target database into my copy?

Is a Synonym a possible solution?

Thanks!

EDIT: A couple of people have pointed out the use of square brackets -- this is not an option. The application GUI gives me an error when I try to use the brackets in the DB Name field.

Was it helpful?

Solution

Major hack alert - make synonyms not for a database, but everything in it!

If your app only needs access to the objects supported by synonyms, you could potentially create another DB with a better name and fill it with synonyms of the original DB's objects:

create database MyDatabase;
go
use MyDatabase;
go
create synonym [dbo].[MyTable1] for [My-Database].[dbo].[MyTable1];
create synonym [dbo].[MyView2] for [My-Database].[dbo].[MyView2];
create synonym [dbo].[MyProc3] for [My-Database].[dbo].[MyProc3];
...

Here is a bit of generation script to help with this:

declare @OldDbName sysname = 'My-Database'
select 'create synonym ' + quotename(schema_name(schema_id)) + '.' + quotename(name) + ' for '
    + quotename(@OldDbName) + '.' + quotename(schema_name(schema_id)) + '.' + quotename(name) + ';'
from sys.objects
where schema_name(schema_id) not in ('sys')
    and type in ('AF', 'FN', 'FS', 'FT', 'IF', 'P', 'PC', 'RF', 'TF', 'U', 'V', 'X')

OTHER TIPS

You don't provide the context where the ODBC Connection is used but you know you can use the database name enclosed with square brackets [My-Database] in SQL or connection strings when the database or tables contain dashes?

I'm not sure if this is directly related to your particular context, as you've not specified this... However, You should try wrapping your database name in Square Brackets as explained here;

http://joseph.randomnetworks.com/2006/09/29/sql-server-escaping-the-database-name/

To escape the name of a database that contains spaces, hyphens (“-”), or any other exceptional characters, the database name must be enclosed in brackets, as is shown in the example, below. This technique must also be applied when selecting a database name that is also a reserved word (such as “primary”).

$conn = mssql_connect('SQLSERVERHOST', 'username', 'password');
   mssql_select_db('[database-name]', $conn);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top