Display record of a query one at a time

0

I need to do a function where I read one by one the records of a query like: select * from alumnos to be able to use the data one at a time, and to modify them one at a time.

Therefore the idea was to put it in a while and go modifying them but I do not know how to go selecting one by one and without repeating them. All this in SQL 2008 language

    
asked by nehuen fortes 31.07.2018 в 22:59
source

2 answers

0

This would be an example of what you need to do, a cursor that traverses the tuples and a loop to iterate through them and do what you need

   SET NOCOUNT ON;  

    DECLARE @vendor_id int, @vendor_name nvarchar(50),  
        @message varchar(80), @product nvarchar(50);  

    PRINT '-------- Vendor Products Report --------';  

    DECLARE vendor_cursor CURSOR FOR    SELECT VendorID, Name   FROM Purchasing.Vendor   WHERE PreferredVendorStatus = 1   ORDER BY VendorID;  

    OPEN vendor_cursor  

    FETCH NEXT FROM vendor_cursor    INTO @vendor_id, @vendor_name  

    WHILE @@FETCH_STATUS = 0   BEGIN  
        PRINT ' '  
        SELECT @message = '----- Products From Vendor: ' +   
            @vendor_name  

        PRINT @message  

        -- Declare an inner cursor based     
        -- on vendor_id from the outer cursor.  

        DECLARE product_cursor CURSOR FOR   
        SELECT v.Name  
        FROM Purchasing.ProductVendor pv, Production.Product v  
        WHERE pv.ProductID = v.ProductID AND  
        pv.VendorID = @vendor_id  -- Variable value from the outer cursor  

        OPEN product_cursor  
        FETCH NEXT FROM product_cursor INTO @product  

        IF @@FETCH_STATUS <> 0   
            PRINT '         <<None>>'       

        WHILE @@FETCH_STATUS = 0  
        BEGIN  

            SELECT @message = '         ' + @product  
            PRINT @message  
            FETCH NEXT FROM product_cursor INTO @product  
            END  

        CLOSE product_cursor  
        DEALLOCATE product_cursor  
            -- Get the next vendor.  
        FETCH NEXT FROM vendor_cursor   
        INTO @vendor_id, @vendor_name   END    CLOSE vendor_cursor;   DEALLOCATE vendor_cursor;
    
answered by 01.08.2018 в 16:43
-1

If it's how I'm thinking, it would be grouping the select by the fields contained in the table and in that way the data is not repeated:

Select Codest,Nombre,Apellido from estudiantes
group by Codest,Nombre,Apellido

And with the application, of the language that is, you get that information and with a cycle (as you like) in the datatable or dataset you will edit each one of the records that you have.

    
answered by 01.08.2018 в 16:37