Solved

Updating variable table

Posted on 2016-11-15
9
42 Views
Last Modified: 2016-12-28
I'm creating a basic stored procedure in SQL and I'm running into some issues when using a variable table.

Currently I have it setup like this:

                DECLARE @Results AS TABLE(Id int null, Username nvarchar(max) null, TeamId nvarchar(max) null,
                        Info nvarchar(max) null, HNumber int null, Notes nvarchar(max) null,
                        OutcomeStatus nvarchar(max), ReviewDate date, OutcomeDate date null, InvestigationResoluionId nvarchar(max) null,
                        Discriminator nvarchar(max) null, CloseReasonId nvarchar(max) null, ReferralTypeId nvarchar(max) null)


                        INSERT INTO @Results (Id, UserName, HitsNumber, Notes, OutcomeStatus, ReviewDate, OutcomeDate, TeamId, Discriminator, CloseReasonId, ReferralTypeId)

                SELECT Id, UserName, HNumber, Notes, OutcomeStatus, ReviewDate, OutcomeDate, TeamId, Discriminator, CloseReasonId, ReferralTypeId

                FROM Trackings
                      UPDATE @Results

                        SET OutcomeStatus =
                        
                        (CASE
                        WHEN OutcomeStatus = 0 THEN 'None'
                        WHEN OutcomeStatus = 1 THEN 'Referred'
                        WHEN OutcomeStatus = 2 THEN 'Closed'
                        END)

                        UPDATE @Results

                        Set TeamId = Teams.Description
                        FROM Teams
                        JOIN Trackings
                        ON Teams.Id = MedicTrackings.TeamId


The first part of the query using the case statement work correctly, however when using UPDATE, I'm not getting the correct results back for the team.  It seems to just alternate the first two values in the results. So I get 4 rows with alternating with the first two values, even though the Id's don't match up with this.
0
Comment
Question by:delmarvamonkey
[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
  • 5
  • 4
9 Comments
 
LVL 51

Expert Comment

by:Vitor Montalvão
ID: 41888087
Your 2nd UPDATE is quite strange:
UPDATE @Results
Set TeamId = Teams.Description
FROM Teams --> You need to join this table with @Results
      JOIN Trackings ON Teams.Id = MedicTrackings.TeamId --> Where MedicTrackings comes from? There is no alias with that name
0
 

Author Comment

by:delmarvamonkey
ID: 41888090
Sorry I meant:

 ON Teams.Id = Trackings.TeamId
0
 
LVL 51

Expert Comment

by:Vitor Montalvão
ID: 41888097
You still need to join Teams or Trackings table with @Results.
0
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 

Author Comment

by:delmarvamonkey
ID: 41888104
Is this correct syntax?

                        UPDATE @Results

                        Set TeamName = Teams.Description
                        FROM Teams
                        JOIN Trackings
                        ON Teams.Id = Trackings.TeamId
                        WHERE Trackings.TeamId = [@Results].TeamId
0
 
LVL 51

Expert Comment

by:Vitor Montalvão
ID: 41888147
Yes, that may work. Alternatively you can also use the following:
UPDATE R
Set R.TeamName = Teams.Description
FROM @Results R
	JOIN Teams ON R.TeamId = Teams.Id 
	JOIN Trackings ON Teams.Id = Trackings.TeamId

Open in new window

0
 
LVL 51

Expert Comment

by:Vitor Montalvão
ID: 41888150
Even I'm not sure why do you'll need Trackings table.
0
 

Author Comment

by:delmarvamonkey
ID: 41888189
The Teams table is a lookup table.

Could I just do a simple

WHERE Teams.Id = [@Results].TeamId?
0
 
LVL 51

Accepted Solution

by:
Vitor Montalvão earned 500 total points
ID: 41888197
Yes you can but that's is mostly a JOIN between the two tables (Teams and @Results).
There's no difference between them so just chose your flavor ;)
0
 

Author Comment

by:delmarvamonkey
ID: 41888201
Ok, thanks.
0

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

630 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