Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

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

Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
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…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

671 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