Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Help with update

Author  Topic 

harlingtonthewizard
Constraint Violating Yak Guru

352 Posts

Posted - 2009-06-24 : 21:08:26
I am tring to update a table 'CommunicationsDetails' from a query and it is currently not working. I import multiple tables from a CSV file into a temp table. I then need to update the db tables with the data from the temp table. At the bottom is what I have so far.

Here are my tables in the db.

CommunicationsDetails

CommunicationID MultiCommTYpe CommMethod DefaultMultiCommConfig PhoneNumber IPAddr Port1 Port2
1 PSTN PSTN_DEFAULT False NULL 2000 554
2 ISDN ISDN64 False NULL 2000 554
3 ISDN ISDN128 False NULL 2000 554
4 Network Network_DEFAULT True 172.20.201.5 2000 554
5 PSTN PSTN_DEFAULT False NULL 2000 554
6 ISDN ISDN64 False NULL 2000 554
7 ISDN ISDN128 False NULL 2000 554
8 Network Network_DEFAULT True 172.20.201.7 2000 554
9 PSTN PSTN_DEFAULT False NULL 2000 554
10 ISDN ISDN64 False NULL 2000 554
11 ISDN ISDN128 False NULL 2000 554
12 Network Network_DEFAULT True 172.20.201.8 2000 554


SiteCommunications

SiteID CommunicationsID
2 1
2 2
2 3
2 4
3 5
3 6
3 7
3 8
4 9
4 10
4 11
4 12


SiteDetails

SiteID DeviceID SiteName TransmitterType
1 SystemMonitorSite SystemMonitor SystemMonitor
2 2015 Bravo V3100
3 2017 Delta V3100
4 2018 Echo V3100


This select will return what seems like the correct rows as shown below. I then need to update DefaultMultiCommConfig = uDefaultMultiCommConfig PhoneNumber = uPhoneNumber IPAddr = uIPAddr Port1 = uPort1 Port2 = uPort2 for each row. The unique fields are SiteName, MultiCommType and CommMethod as there will be four CommMethods for each site.

SELECT *
FROM (
SELECT DISTINCT SiteDetails.SiteName, #u.SiteName as uSiteName, CommunicationDetails.MultiCommType, #u.MultiCommType as uMultiCommType, CommunicationDetails.CommMethod, #u.CommMethod as uCommMethod, CommunicationDetails.DefaultMultiCommConfig, #u.DefaultMultiCommConfig as uDefaultMultiCommConfig, CommunicationDetails.PhoneNumber, #u.PhoneNumber as uPhoneNumber, CommunicationDetails.IPAddr, #u.IPAddr as uIPAddr, CommunicationDetails.Port1, #u.Port1 as uPort1, CommunicationDetails.Port2, #u.Port2 as uPort2
FROM CommunicationDetails INNER JOIN
SiteCommunications ON CommunicationDetails.CommunicationID = SiteCommunications.CommunicationID INNER JOIN
SiteDetails ON SiteCommunications.SiteID = SiteDetails.SiteID INNER JOIN
#u ON SiteDetails.SiteName = #u.SiteName AND CommunicationDetails.MultiCommType = #u.MultiCommType AND CommunicationDetails.CommMethod = #u.CommMethod) as d
WHERE SiteName = uSiteName AND MultiCommType = uMultiCommType AND CommMethod = uCommMethod


SiteName uSiteName MultiCommTYpe uMultiCommTYpe CommMethod uCommMethod DefaultMultiCommConfig uDefaultMultiCommConfig PhoneNumber uPhoneNumber IPAddr uIPAddr Port1 uPort1 Port2 uPort2

Bravo Bravo ISDN ISDN ISDN128 ISDN128 0 0 NULL NULL 2000 2001 554 554
Bravo Bravo ISDN ISDN ISDN64 ISDN64 0 0 NULL NULL 2000 2001 554 554
Bravo Bravo Network Network Network_DEFAULT Network_DEFAULT 1 1 172.20.201.5 172.20.201.50 2000 2001 554 554
Bravo Bravo PSTN PSTN PSTN_DEFAULT PSTN_DEFAULT 0 0 100 NULL NULL 2000 2001 554 554
Delta Delta ISDN ISDN ISDN128 ISDN128 0 0 NULL NULL 2000 2000 554 554
Delta Delta ISDN ISDN ISDN64 ISDN64 0 0 NULL NULL 2000 2000 554 554
Delta Delta Network Network Network_DEFAULT Network_DEFAULT 1 1 NULL 172.20.201.7 172.20.201.70 2000 2000 554 554
Delta Delta PSTN PSTN PSTN_DEFAULT PSTN_DEFAULT 0 0 NULL NULL 2000 2000 554 554
Echo Echo ISDN ISDN ISDN128 ISDN128 0 0 NULL NULL 2000 2000 554 554
Echo Echo ISDN ISDN ISDN64 ISDN64 0 0 NULL NULL 2000 2000 554 554
Echo Echo Network Network Network_DEFAULT Network_DEFAULT 1 1 NULL 172.20.201.8 172.20.201.80 2000 2000 554 554
Echo Echo PSTN PSTN PSTN_DEFAULT PSTN_DEFAULT 0 0 NULL NULL 2000 2000 554 554



IF I replace the select with the update like this I get the table as below which is incorrect:

UPDATE CommunicationDetails SET CommunicationDetails.MultiCommType = d.uMultiCommType, CommunicationDetails.CommMethod = d.uCommMethod, CommunicationDetails.DefaultMultiCommConfig = d.uDefaultMultiCommConfig, CommunicationDetails.PhoneNumber = d.uPhoneNumber, CommunicationDetails.IPAddr = d.uIPAddr, CommunicationDetails.Port1 = d.uPort1, CommunicationDetails.Port2 = d.uPort2
FROM (
SELECT DISTINCT SiteDetails.SiteName, #u.SiteName as uSiteName, CommunicationDetails.MultiCommType, #u.MultiCommType as uMultiCommType, CommunicationDetails.CommMethod, #u.CommMethod as uCommMethod, CommunicationDetails.DefaultMultiCommConfig, #u.DefaultMultiCommConfig as uDefaultMultiCommConfig, CommunicationDetails.PhoneNumber, #u.PhoneNumber as uPhoneNumber, CommunicationDetails.IPAddr, #u.IPAddr as uIPAddr, CommunicationDetails.Port1, #u.Port1 as uPort1, CommunicationDetails.Port2, #u.Port2 as uPort2
FROM CommunicationDetails INNER JOIN
SiteCommunications ON CommunicationDetails.CommunicationID = SiteCommunications.CommunicationID INNER JOIN
SiteDetails ON SiteCommunications.SiteID = SiteDetails.SiteID INNER JOIN
#u ON SiteDetails.SiteName = #u.SiteName AND CommunicationDetails.MultiCommType = #u.MultiCommType AND CommunicationDetails.CommMethod = #u.CommMethod) as d
WHERE d.SiteName = d.uSiteName AND d.MultiCommType = d.uMultiCommType AND d.CommMethod = d.uCommMethod


CommunicationsDetails (incorrect)

CommunicationID MultiCommTYpe CommMethod DefaultMultiCommConfig PhoneNumber IPAddr Port1 Port2
1 ISDN ISDN128 False NULL 2001 554
2 PSTN PSTN_DEFAULT False NULL 2000 554
3 ISDN ISDN128 False NULL 2001 554
4 PSTN PSTN_DEFAULT False NULL 2000 554
5 ISDN ISDN128 False NULL 2001 554
6 PSTN PSTN_DEFAULT False NULL 2000 554
7 ISDN ISDN128 False NULL 2001 554
8 PSTN PSTN_DEFAULT False NULL 2000 554
9 ISDN ISDN128 False NULL 2001 554
10 PSTN PSTN_DEFAULT False NULL 2000 554
11 ISDN ISDN128 False NULL 2001 554
12 PSTN PSTN_DEFAULT False NULL 2000 554

CommunicationsDetails (should be)

CommunicationID MultiCommTYpe CommMethod DefaultMultiCommConfig PhoneNumber IPAddr Port1 Port2
1 PSTN PSTN_DEFAULT False 100 2001 554
2 ISDN ISDN64 False NULL 2001 554
3 ISDN ISDN128 False NULL 2001 554
4 Network Network_DEFAULT True 172.20.201.50 2001 554
5 PSTN PSTN_DEFAULT False NULL 2000 554
6 ISDN ISDN64 False NULL 2000 554
7 ISDN ISDN128 False NULL 2000 554
8 Network Network_DEFAULT True 172.20.201.70 2000 554
9 PSTN PSTN_DEFAULT False NULL 2000 554
10 ISDN ISDN64 False NULL 2000 554
11 ISDN ISDN128 False NULL 2000 554
12 Network Network_DEFAULT True 172.20.201.80 2000 554
   

- Advertisement -