How to create Sql Synonym or "Alias" for Database Name? - sql

I'm using ms sql 2008 and trying to create a database name that references another database. For example 'Dev', 'Test', 'Demo' would be database names that i could reference from my multiple config files, but each name would point to another database such as 'db20080101' or 'db20080114'.
[Edit]Some of the configs are for applications that i control the code and some aren't (ex. MS Reporting service datasource file configs)[/Edit]
It seems that sqlserver only supports synonyms for View,Table,Sproc, or Function. And Alias' are for table and column names.
Is there a way to do this that i missed in the docs?
Any one have any suggestions on a workaround?

use 3 part notation and alias up to the table, example
select * from tempdb.dbo.sysobjects a
join master.dbo.sysobjects b on a.id = b.id

There is a way to simulate this using a linked server. This assumes you have two SQL servers with the same set of databases one for development/test and one live.
Open SQL Server Management Studio on your development/test server
Right click Server Objects > Linked Servers
Select New Linked Server...
Select the General page
Specify alias name in Linked server field - this would normally be the name of your live server
Select SQL Native Client as the provider
Enter sql_server for Product Name
In Data Source specify the name of the development server
Add Security and Server Options to taste
Click OK
The above is for SQL Server 2005 but should be similar for 2008
Once you've done that you can write SQL like this:
SELECT * FROM liveservername.databasename.dbo.tablename
Now when your scripts are run on the development server with the linked server back to itself they will work correctly pulling data from the development server and when the exact same scripts are run on the live server they will work normally.

I've done something similar to this using another config file.
The new config file maps your generic name to all of the information needed to connect to that database (db name, user name, password, etc.) and then your connection function takes your generic name as an argument.
db.config:
DEV_DB_NAME = db20080101
DEV_DB_USER = dev_user
DEV_DB_PASS = dev_pass
TEST_DB_NAME = db20070101
TEST_DB_USER = test_user
TEST_DB_PASS = test_pass
connection code:
db_connection get_connection(string prefix) {
db_connection db_conn = new db_connection;
string db_name = get_config_value(config_path, prefix + "_DB_NAME");
string db_user = get_config_value(config_path, prefix + "_DB_USER");
string db_pass = get_config_value(config_path, prefix + "_DB_PASS");
db_conn.connect(db_name, db_user, db_pass);
return db_conn;
}
Then you just call get_connection() with your db alias as the argument.

I know this probably will not help in all situations, but you still have the option of using views. You can insert, delete, update, select into a view as long as it has a proper identity key (Primary Key). If you point it to another database, you should drop and recreate to get the different schema (in case you're working between production and test while making changes to the schema in test and/or production.
Synonyms are useful for when you're going to another database and have a 3 or 4 part name, but when you want to make it so you can have a set name, a linked server will also work which will let you use a fixed name if the table names are the same in both databases and you're just pointing between prod and test.

Related

What is the # sign in SQL used for other than parameters?

For a query which looks something like
select * from cus_query.ca_activity_vw#drifter
I imagine cus_query is the schema and the view's name is ca_activity_vw. But what is drifter ?
I quote the Oracle documentation:
If the identifier names an object on a remote database, you must reference it with its remote name. The syntax is:
simple_identifier_name#link_to_remote_database
If the identifier is declared in a PL/SQL unit on a remote database, you must reference it with its qualified remote name. The syntax is:
unit_name.simple_identifier_name#link_to_remote_database
From this page : http://docs.oracle.com/cd/E11882_01/appdev.112/e25519/fundamentals.htm#LNPLS99945
drifter is a so-called database link - it allows to transparently query tables that reside on a different database (usually Oracle, but possibly some other RDBMS, e.g. via Database gateway).
To see the definition for the database link, you can use this query (this requires DBA privileges):
select * from dba_db_links

How to manage a HSQLDB in JBoss AS7.1.x

It's my first try to use EJB3.1 entities with a JBoss AS7.1.1 server. I figured out that the HSQLDB is no longer included in the version 7 of JBoss. First I added the hsqldb.jar through the Administration Console --> Deployments --> Manage Deployments. After that I added a new Data Source through Profil --> Connector -> Datasources
My first example code works fine:
[...]
InitialContext ic = new InitialContext();
DataSource ds = (DataSource) ic.lookup("java:/DefaultDS");
con = ds.getConnection();
stmt = con.createStatement();
stmt.execute("drop table timers;");
stmt.execute("Create table timers(id char(10));");
stmt.execute("INSERT INTO timers (id) VALUES (20)");
stmt.execute("INSERT INTO timers (id) VALUES (21)");
ResultSet number = stmt.executeQuery("select * from timers");
[...]
My question is how I can manage (=create, drop and update new tables) the DB which is created in the folder jobss\standalone\data\hypersonic. At the moment I have no "overview" which tables are created, the structure of them and the data.
Does someone have a tip or a tutorial for me which deal with the problem? Thank you.
In my case it was easier as I thought at the beginning. I needed to manage the DB which is stored in the AS 7.1.x Server. Because of the missing JMX-Console I wasn't able to get access to that DB over the administration. I've added the Datasource and the Manage Deployments as described in the first post.
To manage such a DB you can use the 'HSQLDB Database Manager', select 'HSQL Database Engine Standalone' as the type and 'jdbc:hsqldb:file:«MY_PATH_TO_DB_FOLDER_IN_JBOSS»' for the URL. Now I can manage the DB outside the server and EJB environment.
Thanks you fredt for your help and your inspirations.
You always query INFORMATION_SCHEMA tables to find out which tables already exist. Once you know a table exists, you can execute a query that you know does not take a long time, in order to find out about the state of the data.
For example, the first query will show you if there is a 'TIMERS' table, and the second query will show if there is any data in the table:
SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'TIMERS'
SELECT * FROM TIMERS LIMIT 1
Try always to define a primary key on each table in order to speed up queries.
To access a database embedded in an app server, you should add the org.hsqldb.server.Servlet class as a servlet then connect to the app server using a database management tool and a URL such as jdbc:hsqldb:http:<your servlet url here>

Invalid object name 'PetDatabase.Sales'

Im trying to run the following to import a large volume of sales data in a text file into a database. When i run the following i get the error: "Invalid object name 'PetDatabase.Sales'
BULK INSERT PetDatabase.Sales
FROM 'C:\Temp\P1.txt'
WITH
(
FORMATFILE = 'C:\Temp\PetSales.Fmt'
);
Can anyone see whats causing my problem? I do have the tables within a folder; however, when i tried PetsDatabase.Tables.Sales it made no difference.
Ignore this answer. It was written when the question was tagged with mysql. Leaving the answer here to keep the comments.
--
Try using LOAD DATA INFILE instead.
http://dev.mysql.com/doc/refman/5.1/en/load-data.html
Make sure PetDatabase.Sales exists in your text file.
Swap for whichever row and field terminator delimiters you're using. Here I'm using delimiters from a comma separated file
BULK INSERT PetDatabase
FROM 'c:\temp\p1.txt'
WITH
(
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)
GO
--Check the content of the table.
SELECT *
FROM PetDatabase
GO
--Drop the table to clean up database.
SELECT *
FROM PetDatabase
GO
Also, make sure the following doesn't apply to you:
If a SQL Server user is logged in using Windows Authentication, the user can read only the files accessible to the user account, independent of the security profile of the SQL Server process.
When executing the BULK INSERT statement by using sqlcmd or osql, from one computer, inserting data into SQL Server on a second computer, and specifying a data_file on third computer by using a UNC path, you may receive a 4861 error.
To resolve this error, use SQL Server Authentication and specify a SQL Server login that uses the security profile of the SQL Server process account, or configure Windows to enable security account delegation.
Is PetDatabase is schema name or database name?
If it is database name, then include schema name also like this if your schema name is dbo.
PetDatabase.dbo.Sales

Find and replace particular string in a column

I have a table in the database where it contains some of the user name. When I copy the database files from one machine to other machine, I need to manually update this table. The table contains the username in the following format
<domain name>\Username
the domain mostly the local machine name (the user exists within the system). What I am trying to write a simple SQL Query to find a pattern (machine name) and replace with a new one.
I am not so proficient with SQL queries. Can you share a sample snippet? I am using SQL Server 2008
UPDATE table_that_contains_users
SET field_user = replace( field_user, 'OLDDOMAIN\', 'NEWDOMAIN\')
is that?
Try this statement:
UPDATE your_table SET machine_name = REPLACE(machine_name, machine_name, 'Your New Value')

Connect to Database server-side

I've essentially made a MVC application following the tutorial at http://www.asp.net/mvc/tutorials/getting-started-with-mvc3-part1-cs
I can upload it to a server and have the main page run just fine.. but running a different page that interacts with a database brings up the error
" Invalid object name 'dbo.Lyrics'. "
Now I can connect to the database that I'm trying to use (on the server) remotely using management studio just fine.. its called Lyrics and the table is Default.Lyrics ..
The connection string I'm using is "connectionString="Data Source=74.86.97.85;Initial Catalog=Lyrics;User Id=Default;Password=****;""
So my question is .. why is my application trying to use an object with the name "dbo.Lyrics" when my entire application doesn't have that text in it? How can I solve this?
I know that the dbo prefix means DataBase Owner.. and its like a public table.. but since I'm specifying a User ID shouldn't it look for tables with my ID as the prefix?
dbo at the beginning of an object name is a schema. Schemas partition the objects in your database. dbo is simply the default schema.
So, if you have an object named Lyrics, then it's really dbo.Lyrics.