0

I want to get random rows from a table in SQL. I'm trying to do this in C# but i see i will have a problem with IDs and I would need to make another column called row number to order IDs.

protected void ibtnPrintInventory_Click(object sender, ImageClickEventArgs e)
{

   Random rng = new Random((int)DateTime.Now.Ticks);

   var inv = Inventario.GetDefaultConfigs();
   int filtro = inv.Filtro;
   int localizacoes_max = inv.LocalizacoesMax;

   int userID = Utilizador.GetUserID();


   int armazemID = Armazem.GetArmazemByUserID(userID);

   // Total rows to apply random
   int localizacoes_armazem = Inventario.GetInventarioCount(armazemID);

   // Loop Flag
   int etiquetas_soma = 0;

   int[] localizacoes_array = new int[localizacoes_max];

   while(etiquetas_soma <= filtro)
   {
       for (int i = 0; i < localizacoes_max; i++)
       {
           localizacoes_array[i] = rng.Next(1, localizacoes_armazem);
       }
   }

}

I was wondering if this is possible to do directly in SQL, a query that returns a number of rows based on a variable from a table and selects random rows.

EDIT.

The objective is to get random rows from a table and sum those rows column values and if the sum is higher than the filter it will break the loop.

Also I have to use a parameter to how many rows i want to get

Jackal
  • 3,359
  • 4
  • 33
  • 78

0 Answers0