Query to transfer data from one database to another - sql-server

I have 2 database's and I need to transfer data from database 1 to database 2.
Only the data that in database 1 but not in database 2.
DB 1
----
MyTBL 1
-------
111
222
333
444
555
666
DB 2
----
MyTBL 2
-------
111
222
666
I need to transfer from 1 to 2.
It will looks like this:
DB 2
----
MyTBL 2
--------
111
222
333
444
555
666
I need SQL query, I work in SQL Server 2012
I tried a few things - but without success.

Guessing at your names:
INSERT DB2.dbo.MyTBL2
(DataColumn)
SELECT DataColumn
from DB1.dbo.MyTBL1
EXCEPT SELECT DataColumn
from DB2.dbo.MyTBL2
SELECT EXCEPT can be very powerful. More info at https://learn.microsoft.com/en-us/sql/t-sql/language-elements/set-operators-except-and-intersect-transact-sql?view=sql-server-ver15

If both databases are on the same instance, merge is a reasonable solution for this. Assuming the schema for all objects is dbo, run the merge statement inside db2:
merge dbo.MyTBL2 t2
using db1.dbo.MyTbl1 t1 on t1.yourcolumn = t2.yourcolumn
when not matched then
insert (yourcolumn) values (t1.yourcolumn);

Related

Access remaining records other than records in transaction SQL Server

I am trying to achieve a task in SQL Server. I'm sharing the sample problem as I couldn't share the entire task description.
Problem: we have a table called Person as follows:
Person_Id Person_Name Person_Age
--------- ----------- ----------
1 AAA 25
2 BBB 25
3 CCC 25
4 DDD 25
From that table, I want to use the Person_Id = 4 that is going to be kept inside a TRANSACTION.
Person_Id Person_Name Person_Age
--------- ----------- ----------
4 DDD 25
While performing the above transaction, user wants to access (INSERT, UPDATE, DELETE) all the other records (other than Person_Id = 4) which are in the table as below:
Person_Id Person_Name Person_Age
--------- ----------- ----------
1 AAA 25
2 BBB 25
3 CCC 25
What I tried:
I tried with NOLOCK, ROWLOCK but I couldn't achieve this. Kindly help me to achieve this scenario. I have also tried this link. As per this link, using
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
the SELECT query is fetching the Unmodified data. For example, If I am trying to UPDATE the record in a TRANSACTION and the record got updated but the TRANSACTION is busy with executing other statements.
Person_Id Person_Name Person_Age
--------- ----------- ----------
4 DDD 25
Now, when other connections are trying to SELECT the records in the table, then all other records along with the Record: Person_Id = 4 (With old value) will get returned.
SERIALIZABLE Specifies the following:
Statements cannot read data that has been modified but not yet
committed by other transactions.
From the above, When I am using SERIALIZABLE isolation, it still returns the OLDER Record of Person_Id = 4. This I don't want in this case.
I want to get all the other records, other than the records in a TRANSACTION.
In other words, If a record(s) is locked in a TRANSACTION, then that record(s) should not appear in any other SELECT STATEMENT EXECUTION with different connections.

Multiple updates in SQL Server

I have 500 records in Excel and it contains some columns which is same as the columns in SQL DB Table. I have old column value
as well as new column value in excel.
Example
Excel
Name Age OldEmployeeID NewEmployeeID
x 1 100 200
y 2 101 201
z 3 102 202
SqlTable
EmployeeTable
Name Age Department City EmployeeID
x 1 HR x 100
a 4 HR x 103
y 2 Admin x 101
b 5 Finance x 104
c 3 IT x 102
I want to update EmployeeID column in SQL Table as NewEmployeeID which is there in excel.
Can anyone suggest how to write a sql query to update the sql table.
Assuming the column is not an ID Column or having some other constraint you can:
Option 1:
Assuming your table is similar to the screenshot you have a concatenated column with this formula and drag it down and copy it directly into SQL.
=CONCATENATE("UPDATE dbo.EmployeeTable SET EmployeeID = ",D2," WHERE EmployeeID = ",C2,";")
Option 2:
You can import the Excel file into SQL and use a statement like the one below. However, be cautious if any of the old EmployeeIDs overlap the new EmployeeIDs. For Example if Jim has ID 100 and his new one is 500 and Jane's new ID is 100 and if you accidentally run it a second time, the Jane will also get an ID of 500.
UPDATE EmployeeTable SET EmployeeID = Excel.NewEmployeeID
FROM
EmployeeTable
JOIN ExcelTable
ON Excel.OldEmployeeID = EmployeeTable.EmployeeID
;

Database Link creates more than 1 session

We have two Oracle databases
1. 11.2.0.4
2. 12.1.0.2
We create a Dblink from_11_to_12.
I connect via sqlplus to both databases and issue the commands below, step-by-step, first at 12c , second at 11g and so on:
At 12c
SQL> select sid,serial# from v$session where username = 'REP'; --Step 1
no rows selected
SQL> select sid,serial# from v$session where username = 'REP'; --Step 3
SID SERIAL#
---------- ----------
11 53421
17 28325
37 22230
453 23175
462 48252
SQL> select sid,serial# from v$session where username = 'REP'; --Step 5
SID SERIAL#
---------- ----------
11 53421
17 28325
378 55283
453 23175
462 48252
SQL> select sid,serial# from v$session where username = 'REP'; --Step 7
SID SERIAL#
---------- ----------
11 53421
17 28325
378 2986
453 23175
at 11g
SQL> select id from table1#from_11_to_12 where rownum <1; --Step 2
no rows selected
SQL> commit; --Step 4
Commit complete.
SQL> alter session close database link from_11_to_12; --Step 6
Session altered.
As you can see even after Step 6, still there are session at remonte database.
And more than that, why the connection creates 5 sessions?
How can I close those remote sessions. We work with shared servers, so if many people connect at the same time there will be at least 4 sessions busy.

Need help on a SQL query

Here's the scenario:
I have 3 tables in a SQL Server 2008 database - SERVERS, InstalledPatches and Patchlist.
The SERVERS table has list of servers. InstalledPatches has list of servers and patches installed on them. Patchlist has list of all patches that SHOULD be installed on each server. All patches in PATCHLIST should be ideally installed on all servers in SERVERS table. I am trying to find the patches that are missing.
Sample data:
SERVERS
SERVERID SERVERNAME
-----------------------
1 ABC
.. ..
1500 XYZ
INSTALLEDPATCHES:
SERVERID PATCHID
-----------------
1 1
1 2
2 1
.. ..
1500 1
1500 2
PATCHLIST:
PATCHID PATCHNUMBER
---------------------
1 aaa
2 bbb
3 ccc
4 ddd
.. ..
15 ZZZ
Final report should indicate missing patches:
SERVERID MissingPATCHID
-------------------------
1 3
1 4
1 1500
2 3
2 4
2 1500
..
I have tried to use below query, but cant find all missing patches for each server.
SELECT
A.*
FROM
INSTALLEDPATCHES A
RIGHT OUTER JOIN
PATCHLIST B ON A.PATCHID = B.PATCHID
WHERE
A.PATCHID IS NULL
Any help would be really appreciated.
Thanks.
What about something like?
select s.SERVERID,
pl.PATCHID MissingPATCHID
from SERVERS s
cross join PATCHLIST pl
where not exists (select SERVERID,
PATCHID
from INSTALLEDPATCHES ip
where ip.SERVERID = s.SERVERID
and ip.PATCHID = pl.PATCHID)
I just created this SQLFiddle demo.
Try my query. It works now.
select s.serverid, p1.patchid as MissingPatchID
from [servers] as s
left join patchlist as p1
on 1=1
left join installedpatches as p2
on s.serverid = p2.serverid
and p1.patchid = p2.patchid
where p2.patchid is null

How to select a data slice for a specific date from a SQL SERVER database tables which track changes on the row level

All tables in the database has a Date column named EffectiveDate.
Data is imported into the database using a logic which detects and inserts changed records only.
Let us assume 5 imports happened between 1/1/2014 and 5/1/2014
So Table A has:
EffectiveDate id1 column1 column2
-------------- ---- -------- --------
01/01/2014 1 ABC 123
02/01/2014 1 ABC 999
05/01/2014 1 XXX 999
01/01/2014 2 CCCC 555
03/01/2014 2 CCCC 444
04/01/2014 2 DDDD 444
01/01/2014 3 xxxxx 333
and Table B has
EffectiveDate id2 column1 column2
-------------- ----- -------- --------
01/01/2014 1 ZZZZ AAAAA
03/01/2014 1 ZZZZ AABBB
01/01/2014 2 TTTT AAAAA
05/01/2014 2 TTTT AABBB
Now The task is to create 3 set of views for all tables:
The first set is to give the Effective data as of current date
The second set is to give latest data
The third set is to give the data changes after today date (just next changes not the latest)
Consideration:
All views should return only one row for each id with applicable effective date.
If effective date is not available then the maximum effective date in the table less then the requested effective date should be used.
I was able to come up with solution for the Effective and Latest views but not for the third set of views (Next changes)
Any idea how to address this?
You'll need to use the Row_Number function to get this. For each id, the first future row (whatever that means...) will have a row_number of 1.
with RowNumbers as
(select
id1,
effectivedate,
row_number() over (partition by id1 order by effectivedate) as RowNumber
from
a
where
effectivedate > getdate()
)
select
a.*
from
A
inner join RowNumbers
on a.id1 = Rownumbers.id1
and rownumbers.rownumber = 1
and a.effectivedate = rownumbers.effectivedate
SQL Fidldle

Resources