Convert Rowset variables to scalar value

前端 未结 2 1742
遇见更好的自我
遇见更好的自我 2020-12-19 17:08

Is it possible to convert rowset variables to scalar value for eg.

@maxKnownId =
    SELECT MAX(Id) AS maxID
    FROM @PrevDayLog;

DECLARE @max int = @maxKn         


        
相关标签:
2条回答
  • 2020-12-19 17:29

    There is no implicit conversion of a single-cell rowset to a scalar value in U-SQL (yet).

    What are you interested in using the value for?

    Most of the time you can write your U-SQL expression in a way that you do not need the scalar variable. E.g., if you want to use the value in a condition in another query, you could just use the single value rowset in a join with the other query (and with the right statistics, I am pretty sure that the optimizer would turn it into a broadcast join).

    If you feel you cannot easily write the expression without the rowset to a scalar, please let us know via http://aka.ms/adlfeedback by providing your scenario.

    0 讨论(0)
  • Thanks for input, below is the business cases -

    We have catalog data coming from source for which we need to generate unique ids. With ROW_NUMBER() OVER() AS Id method we can generate unique id. But while merging new records it changes ids of existing records also and causes issues with relational data

    Below is simple solutions

    //get max id from existing catalog
    
    @maxId =
        SELECT (int)MAX(Id) AS lastId
        FROM @ExistingCat;
    
    //because @maxId is not scalar, we will do CROSS JOIN so that maxId is repeated for every record.
    //ROW_NUMBER() always starts from 1, we can generate next Id with maxId+ROW_NUMBER()
    
    @newRecordsWithId =
        SELECT (int)lastId + (int)ROW_NUMBER() OVER() AS Id,
               CatalogItemName
        FROM @newRecords CROSS JOIN @maxId;
    
    0 讨论(0)
提交回复
热议问题