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
Solved

Updating variable table

Posted on 2016-11-15
9
33 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
  • 5
  • 4
9 Comments
 
LVL 48

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 48

Expert Comment

by:Vitor Montalvão
ID: 41888097
You still need to join Teams or Trackings table with @Results.
0
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 

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 48

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 48

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 48

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how the fundamental information of how to create a table.

837 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