Microsoft SQL Server 2005: Create alias for database - sql-server-2005

How can I create alias name to my database in SQL Server 2005?
For example: DB1 and its alias DB2, it's same DB, but with two names.
Or can I do replication, mirroring, syncing or anything other inside server from one DB to another?

You can do replication from one database to another database on the same machine. You can also copy data directly without having to create an alias. For instance if you had a table named Users in DB2 and a Users table in DB1 and they are the same schema you could easily just do
INSERT INTO DB1..Users
select * from DB2..Users
Now, a synonym would allow you to use a table from DB2 as if it was a table in DB1 so for instance if you have a table named Products in DB2 you could do
use DB1
GO
CREATE SYNONYM [dbo].[Products] FOR [DB2].[dbo].[Products]
GO
-- Now the following would give you the same result
select * from DB2..Products
select * from Products
For more information on synonyms see here

Related

PostgreSQL, Update from Insert into sql

I have 2 db, I have to update a table called "my_table" from db 1 to db 2.
Is there a way to dump the table from db1 and use that sql to update the table on the db2?
All I have is a sql file with INSERT INTO but I need an UPDATE
How could I do it?
Using DB link: dblink executes a query (usually a SELECT, but it can be any SQL statement that returns rows) in a remote database.
you can refer this official link(https://www.postgresql.org/docs/current/contrib-dblink-function.html) for DB link creation

Deleted an Oracle table, now cannot create a table with the same name

I am on Oracle 12c, and deleted a base table WIP_DISCRETE_JOBS. We had created a backup table like:
CREATE TABLE WDJ_BKP AS (SELECT * FROM WIP_DISCRETE_JOBS)
DROP TABLE WIP_DISCRETE_JOBS;
COMMIT;
Now when I try to restore from backup table it gives error.
CREATE TABLE WIP_DISCRETE_JOBS AS (SELECT * FROM WDJ_BKP)
ORA-00955 name is already used by existing object.
But if we query ALL_OBJECTS with WIP_DISCRETE_JOBS no rows are returned.
What is the problem?
We finally resolved this issue. Problem: Oracle 11gR2 onwards, database has editions, which is type of versions. You have a current edition. You need to query ALL_OBJECTS or DBA_OBJECTS after setting the edition
ALTER SESSION SET EDITION=<EDITION NAME>
to see objects in other editions.
All sessions can be queried from DBA_EDITIONS
There were some editions having a synonym for this table with the same name. After we deleted the synonyms by setting to each edition, we were able to create the table.
DROP TABLE WIP_DISCRETE_JOBS purge;

Oracle update table with the value from another database

In one db i have table PRODUCTS with columns NAME and TECHNICAL_NAME.
In second database I have table TEMP_PRODUCTS with columns NAME (that corresponds to column NAME of the table PRODUCTS from the 1st db) and TECHNICAL_NAME that is null and that should be updated with the corresponding TECHNICAL_NAME values from 1st db.
So I should do UPDATE table TEMP_PRODUCTS by using JOIN with columns NAME?
I would like to avoid solution with exporting table from 1st bd and importing it to the 2nd db.
How can I do this?
By Creating the DB Link only we can access the one DB objects from another DB.
CREATE DATABASE LINK db1_link
CONNECT TO <User Name> IDENTIFIED BY <pwd>
USING 'db2';
-- db2 means Service Name for exp products db
Then Update Statement
UPDATE temp_products tp
SET technical_name=
(SELECT technical_name FROM products#db1_link p
WHERE tp.name = p.name)
Assuming:
both NAME columns are UNIQUE;
you have a database link in db2 pointing to db1.
UPDATE temp_products tp
SET technical_name=
(SELECT technical_name FROM products#db1 p
WHERE tp=name=p.name)
You need to execute the below query in db2 ,where temp_product table lies,and db link db1 on the same database db2,to connect db1.
MERGE temp_products tp
USING products#db1 pp
ON(tp.name = pp.name)
WHEN MATCHED THEN
UPDATE SET tp.technical_name = pp.technical_name;
You need to create a db link in database db2,so that you can connect to db1 ,to acces product table.
Please find the syntax for creating db link
CREATE [PUBLIC] DATABASE LINK <link_name>
CONNECT TO <user_name>
IDENTIFIED BY <password>
USING '<service_name>';

Copy tables from one server to another in DB2

SELECT *
FROM table1 X, table2 C, table3 M, table4 XSDT
WHERE X.CATID= C.CATID
AND M.MEMID= X.MEMID
AND XSDT.SHIPDISC= X.SHIPDISC;
Say I want to run this query on the HOST db (external) and get its data and copy it to a local DB2 database.
Is there a way to do so in DB2?
I know teradata has fastload... but I'm not sure about db2 or how I would go about doing so.
Please keep in mind I do not have dba-level privileges.
Solution to this: http://publib.boulder.ibm.com/infocenter/db2luw/v9/index.jsp?topic=%2Fcom.ibm.db2.udb.admin.doc%2Fdoc%2Fr0002079.htm
If you want to do this with SQL, then you would use something like the following SQL:
create table schema2.table1;
insert into schema2.table1
select * from schema1.table1;
Since you're joining tables, you would have to define the local table in your CREATE TABLE SQL and list the columns in your INSERT as well as your SELECT.
You can do a DB2 backup of the tables, and restore them to your local schema.
You can do a DB2 export of the tables, and use DB2 import to create them on your local schema.
You can use the DB2 db2move utility.

SQL Server 2008 - permission denied in database 'master'

I am new to SQL Server and I wanted to create my first table there.
create table Employee
(
ID smallint not null
)
I use SQL Server 2008 R2 and Windows Authentication.
when I execute , it says :
CREATE TABLE permission denied in database 'master'.
Thanks!
Seems you're trying to create the table in master database where you may not have permission to create table. However, to create your target database please follow below steps:
a. At your SQLQuery editor choose your target database (Available Database drop down list) and execute your sql query.
Or
b. Try with below statement:
USE YourTargetDatabaseName
GO
CREATE TABLE Employee ( ID SMALLINT NOT NULL)
GO
I don't think you want to create a table in the master database.
Did you create a new database first? If so, use this:
USE [MyNewDatabaseName]
GO
create table Employee ( ID smallint not null )
GO