SELECT within a CURSOR

-1

I have the following cursor:

The idea is to consult a list of "Demands", the interesting thing is that some are created by users and others automatically by the system, within the list you want to know which are created by user and which by the system.

The cursor works correctly when executed.

However, when reading the query from my CODE BESIDE the list only contains one result, Demandad = 3.

It is clear to me that from SQL SERVER the results of the query are being shown separately, probably because I invoke the SELECT within the CURSOR, then the question is

What can I do so that these results are not thrown separately and so read my complete list?

    
asked by Ivan.Enriquez 21.03.2017 в 07:16
source

2 answers

0

I adhere to the request that you paste the Script in text and not a bitmap. Anyway, I make the following comments:

  • You have a problem with the variables, they will be updated when a certain condition is given, if then the variables are not returned they will continue to "drag" the previous values, in any case you would have to add an ELSE and assign them NULL or '' .
  • Your specific question was if you could return a single "resultset", in the way the Script is not armed, eventually what you could do is replace the SELECT inside a cursor with an INSERT on a temporary table to go accumulating all the results, and outside the cursor you do a final SELECT but of the temporary table.
  • Now, the most important Why a cursor? , so I saw quietly you can solve the logic with a single SELECT to DemandStore with a LEFT JOIN to USER.

Greetings ..

    
answered by 21.03.2017 в 15:27
0

In the end the solution was as simple as it was proposed by Patricio Moracho, a LEFT JOIN was enough.

  

SELECT DemandStore. *, DemandType.SpeedTypeName,   ISNULL (User.UserNoUmployed, '0') AS NoEmployee,   ISNULL (User.UserName, 'This page was created automatically   by the system. ') AS Name, ISNULL (User.ApPaternoUser,' ') AS   ApPaterno, ISNULL (User.ApMaternoUser, '') AS ApMaterno FROM   DemandStore INNER JOIN DemandType On DemandStore.DemandTypeId   = DemandType.DemandTypeId LEFT JOIN User ON DemandStore.UserId = User.UserId WHERE   DemandStore.SuccessId = 2

    
answered by 21.03.2017 в 19:18