DECLARE RandomUser_CURSOR CURSORLOCAL FORWARD_ONLY KEYSET SCROLL_LOCKS FOR select * from TableA where @bCustom = 0 union all select * from TableB where @bCustom != 0 FOR UPDATE OPEN RandomUser_CURSOR FETCH NEXT FROM RandomUser_CURSOR INTO @Col1, @Col2, @Col3 WHILE @@FETCH_STATUS = 0 BEGIN
order bycase when @Orderby = 'Account' then Account when @Orderby = 'OrderDate' then cast(OrderDate as int)end