Solved

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

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
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.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

762 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now