Hello,
I am new to Sql and attempting to perform something that is considerably over my head.
I have created an inventory system for our webservers that gathers information then inputs into sql tables.
For instance table dbo.Disk has server disk specific information such as deviceid, total size, and volume name.
I gather information save a local csv copy to the servers, then execute a powershell script to create a staging table on the sql server, upload the data then merge the rows into a master table.
Server1 will create a table called dbo.Server1Disk then merge information from there to dbo.Disk
I am able to insert and update however i had difficulty that when i attempted to delete it removed all rows not specific to that server.
I think i have it working but every time i run the query it says the number of rows in the source table affected but the outcome looks correct in the dbo.Disk
For instance Server1 has 5 disks, everytime i run the below query it states 5 rows affected.
Please help me ensure this is done correct or how i can make it best practice.
Thank you
Merge into dbo.Disk as tab1
using(select * from dbo.Server1Disk) as tab2
on tab1.UniqueServerID=tab2.UniqueServerID
and tab1.DeviceID=tab2.DeviceID
when matched then
update set
tab1.DeviceID=tab2.DeviceID,
tab1.DriveType=tab2.DriveType,
tab1.VolumeName=tab2.VolumeName,
tab1.TotalSize=tab2.TotalSize,
tab1.InventoryDate=tab2.InventoryDate
when not matched by TARGET
then insert
values(tab2.UniqueServerID,tab2.DeviceID,tab2.DriveType,tab2.VolumeName,tab2.TotalSize,tab2.InventoryDate)
WHEN NOT MATCHED BY SOURCE
AND tab1.UniqueServerID IN(SELECT UniqueServerID FROM dbo.Server1Disk) THEN
DELETE;