Relation between tables in different databases using SQL Server 2005 - sql-server

is there any way to add a relation between 2 tables in different databases ?
For example:
db1.dbo.table1 field id = db2.dbo.table2 field id
the nearest thing that i´m looking for is something like:
CREATE TRIGGER RELATIONAL on IDCLIENT
FOR INSERT
As
IF (SELECT COUNT(*) FROM table1) = 1
INSERT INTO db2.dbo.table2(ID)
SELECT ID FROM table1

You can create a synonym for the other table in another db, and then you can reference it by its synonym name.

Related

Database Index when SQL statement includes "IN" clause

I have SQL statement which takes really a lot of time to execute and I really had to improve it somehow.
select * from table where ID=1 and GROUP in
(select group from groupteam where
department= 'marketing' )
My question is if I should create index on columns ID and GROUP would it help?
Or if not should I create index on second table on column DEPARTMENT?
Or I should create two indexes for both tables?
First table has 249003.
Second table has in total 900 rows while query in that table returns only 2 rows.
That is why I am surprised that response is so slow.
Thank you
You can also use EXISTS, depending on your database like so:
select * from table t
where id = 1
and exists (
select 1 from groupteam
where department = 'marketing'
and group = t.group
)
Create a composite index on individual indexes on groupteam's department and group
Create a composite index or individual indexes on table's id and group
Do an explain/analyze depending on your database to review how indexes are being used by your database engine.
Try a join instead:
select * from table t
JOIN groupteam gt
ON d.group = gt.group
where ID=1 AND gt.department= 'marketing'
Index on table group and id column and table groupteam group column would help too.

How I can save all the names of my tables into a new table from sql?

I have a huge database in Sql-Server and I need to get all the names of the tables into one new table that I have made. This can be done?
I appreciate your help.
The new table has the fields ID, TableName, Status. Id is the identity and status for now will be 1, not null
Use this query below to get all tables name from your database
SELECT name FROM sys.tables
Then you can do a insert query like -
insert into newtable(name) select * from sys.tables

how to update table(new_DB) from old table(old_DB)

What I have:
1 table(table is in both DB's)
2 databases(currently used + archived from last year(old))
"ID" is the primary key for the table.
my issue:
archived database table has rows in it that is not present in the currently used database table. Can anyone tell me how I go about updating the currently used database table from the old database table(i.e. insert * unique rows from old database table into new database table)
It sounds simple enough but wanted some advice before proceeding as I DO NOT want duplicate rows, I just want to throw the rows in the old table(that IS NOT present in the currently used database table) into the new one(copy only is fine).
I hope I explained clearly enough.
Insert rows from the new table only if row with same id not exists in old table:
insert into old_table select * from new_table nt
where not exists (select 1 from old_table
where id = nt.id)
(Specifying columns, both inserted and selected, is nice - but I'm lazy here...)
You can usually address tables from other databases by prefixing the database name: new_db.foo_table or old_db.foo_table. This way you can look for rows in the old table that have no duplicates in the new table:
select *
from old_db.foo_table as old_foo
where not exists (
select 1
from new_db.foo_table as new_foo
where new_foo.key_field = old_foo.key_field
-- add more comparisons as needed
);
Then you can use the insert into new_db.foo_table select ... syntax to put the records into the new table.
Use LEFT JOIN filtering NULLs in target table. I think it will be faster
INSERT INTO NEW_TABLE
SELECT ot.* FROM OLD_TABLE ot
LEFT JOIN NEW_TABLE nt on ot.ID = nt.ID
WHERE nt.ID IS NULL

Create view across multiple databases

I have two databases; 1 is a live database for daily data input and the other is an archival DB for older data.
How can I create a view which gets data from both databases?
Three tables are involve... database1.dbo.table and database1.dbo.tran1 in same database, and database_archived.dbo.table1:
Create VIEW [dbo].[VW_Table_ALL]
AS
SELECT * FROM database1.dbo.table1
UNION ALL
SELECT * FROM database_archived.dbo.table1 as Data INNER JOIN
database1.dbo.tran1 as Tran ON Data.Tran_id = Tran.Tran_Id
GO
Not sure if you need a UNION or a JOIN, but in either case you can just use a three-part name for the object in the other database:
USE database1;
GO
CREATE VIEW dbo.MyView
AS
SELECT columns FROM dbo.LocalTable
UNION ALL
SELECT columns FROM database2.dbo.RemoteTable;
GO

SQL Server: How to set column alias from another table during select?

I have 2 tables
Table 1:
DB1, DB2, DB3, DB4, DB5, Some other identifiers
Table 2:
EnumText, EnumValue, Some other identifiers
What I want to do is to:-
Select data from Table 1
During Select, change the alias of columns DB1 to DB5 using the Data in Table 2's EnumText column where EnumValue will contain DB1 to DB5.
A sample command would be:
Select
DB1 as [Select EnumText from Table2 where EnumValue='DB1'],
DB2, DB3
from Table1
This command won't work but I hope you people got the idea.
Thank you in advance.
You can't have dynamic aliases without dynamic SQL. If you did change the resultset column names, how do you access them via name? (I wouldn't use index because it will break)
So, you can add a fixed column to describe it. And read that
Select
DB1, foo.EnumText AS DB1EnumText,
DB2,
DB3
from
Table1
CROSS JOIN
(Select EnumText from Table2 where EnumValue='DB1') foo

Resources