Solved

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

Posted on 2013-06-21
5
404 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
  • 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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
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.
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…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.

773 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