Solved

Re-factoring "Update" via View to "Updatee" using temp table from view ...

Posted on 2013-06-21
5
415 Views
Last Modified: 2013-06-29
Hi,
I have an update statement that updates a table via a view (view and update code beloe ..
also the modified update statement that I wrote to update using a temp table ...

The problem is that the update I wrote does not return same # of recs.


USE [DBSync]
GO
--
--ALTER VIEW [dbo].[PBX_ASSIGN_ACCOUNT_CREATED]
--AS
--SELECT     dbo.ReportDate.Today, dbo.PhoneData.CallStartDttm, dbo.PhoneData.cuskey AS PBX_ACCOUNT, dbo.PhoneData.AccountOn AS PBX_ACCOUNTON,
--                      dbo.PhoneData.Account_Ae AS PBX_ACCOUNTAE, dbo.PhoneIndex1.AccountKey AS PHX_ACCOUNTON, dbo.PhoneIndex1.Cuskey AS PHX_ACCOUNT,
--                      dbo.PhoneIndex1.AccountBy AS PHX_ACCOUNTBY, dbo.PhoneData.LinkType
--FROM         dbo.PhoneData INNER JOIN
--                      dbo.ReportDate ON dbo.PhoneData.CallStartDttm > dbo.ReportDate.dttmstart INNER JOIN
--                      dbo.PhoneIndex1 ON dbo.PhoneData.phone = dbo.PhoneIndex1.PhoneNo AND dbo.PhoneData.CallStartKey = dbo.PhoneIndex1.AccountKey
--WHERE     (dbo.ReportDate.Today = 1)

--
--
USE [DBSync]
GO
--
--
--
IF OBJECT_ID('tempdb..#PBX_ASSIGN_ACCOUNT_CREATED') IS NOT NULL  
  DROP TABLE #PBX_ASSIGN_ACCOUNT_CREATED
--
select * into #PBX_ASSIGN_ACCOUNT_CREATED
      FROM (      SELECT      dbo.ReportDate.Today, dbo.PhoneData.CallStartDttm, dbo.PhoneData.cuskey AS PBX_ACCOUNT, dbo.PhoneData.AccountOn AS PBX_ACCOUNTON,
                              dbo.PhoneData.Account_Ae AS PBX_ACCOUNTAE, dbo.PhoneIndex1.AccountKey AS PHX_ACCOUNTON, dbo.PhoneIndex1.Cuskey AS PHX_ACCOUNT,
                              dbo.PhoneIndex1.AccountBy AS PHX_ACCOUNTBY, dbo.PhoneData.LinkType
                  FROM    dbo.PhoneData INNER JOIN
                              dbo.ReportDate ON dbo.PhoneData.CallStartDttm > dbo.ReportDate.dttmstart INNER JOIN
                              dbo.PhoneIndex1 ON dbo.PhoneData.phone = dbo.PhoneIndex1.PhoneNo AND dbo.PhoneData.CallStartKey = dbo.PhoneIndex1.AccountKey
                  WHERE     (dbo.ReportDate.Today = 1)) t
GO
--
--ORIGINAL
BEGIN TRAN
UPDATE PBX_ASSIGN_ACCOUNT_CREATED -- view
SET PBX_ACCOUNT=PHX_ACCOUNT, PBX_ACCOUNTON=PHX_ACCOUNTON, PBX_ACCOUNTAE=PHX_ACCOUNTBY
WHERE PBX_ACCOUNT IS NULL;
ROLLBACK TRAN
--GO
--
--MODIFIED
BEGIN TRAN
UPDATE P
SET P.cuskey = PAAC.PHX_ACCOUNT, P.AccountOn = PAAC.PHX_ACCOUNTON, P.Account_Ae = PAAC.PHX_ACCOUNTBY
from PHONEDATA P JOIN #PBX_ASSIGN_ACCOUNT_CREATED PAAC
ON P.cuskey = PAAC.PBX_ACCOUNT AND P.AccountOn = PAAC.PBX_ACCOUNTON AND P.Account_Ae = PAAC.PBX_ACCOUNTAE
WHERE P.cuskey IS NULL;
ROLLBACK TRAN
GO
--
0
Comment
Question by:DBA2000
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
5 Comments
 
LVL 16

Expert Comment

by:Surendra Nath
ID: 39266766
re-write your last part of the code as below, that gives you same number of lines as expected, but I feel this may not be what you will need in the end though.

--MODIFIED
BEGIN TRAN
UPDATE P
SET P.cuskey = PAAC.PHX_ACCOUNT, P.AccountOn = PAAC.PHX_ACCOUNTON, P.Account_Ae = PAAC.PHX_ACCOUNTBY 
from PHONEDATA P 
JOIN #PBX_ASSIGN_ACCOUNT_CREATED PAAC
ON P.cuskey IS NULL;
ROLLBACK TRAN
GO

Open in new window

0
 

Author Comment

by:DBA2000
ID: 39272020
Hi, Neo_jarvis
... I just got a chance to test this; the two updates don't return same # of recs.
The original returns three records, the modified, 0.
0
 

Author Comment

by:DBA2000
ID: 39272312
... actually, my bad.  The orginal returns 3 records, the version you gave executes indefinitly ... and when cancelling ... hangs!
0
 

Accepted Solution

by:
DBA2000 earned 0 total points
ID: 39273222
Problem solved.
Thank you for your help, Neo_jarvis

This is what I did:

I added the PKey of the table being updated in the SELECT statement that came from the view, and used to create the temp table,
then, I did the "ON" in the inner join with the temp table on the PKey of table being updated.
As below:


BEGIN TRAN
UPDATE PD -- (162 row(s) affected)
SET PD.Cuskey = PHX_ACCOUNT, PD.AccountOn = PAAC.PHX_ACCOUNTON, PD.Account_Ae = PAAC.PHX_ACCOUNTBY
FROM  dbo.PhoneData PD INNER JOIN #PBX_ASSIGN_ACCOUNT_CREATED PAAC
ON PD.PhoneId = PAAC.PHONEID
WHERE PD.Cuskey IS NULL;
ROLLBACK tran
GO

Thank you!
0
 

Author Closing Comment

by:DBA2000
ID: 39286424
... solved as out libed above.
0

Featured Post

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question