Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2013-06-21
5
Medium Priority
?
421 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

971 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