Random Number SKU Generator

Поиск
Список
Период
Сортировка
От Peter Dabrowski
Тема Random Number SKU Generator
Дата
Msg-id trinity-b2dae1a6-807e-42bc-bf5f-ee782c9221ea-1423820766030@3capp-mailcom-lxa08
обсуждение исходный текст
Ответы Re: Random Number SKU Generator  (Roxanne Reid-Bennett <rox@tara-lu.com>)
Список pgsql-novice
I use a Luhn algorithm to generete random number, inserted below sql code to column 'SKU"

 

ALTER TABLE m_product ALTER sku SET DEFAULT ltrim(to_char(luhn_generate(round(random()*10000)::int), '00000'))

 

as a result on record saving random number is generated, but my problem is the numbers are not unique.

 

Somone sugest that I  should write a function to encapsulate "ltrim(to_char(luhn_generate(

round(random()*10000)::int), '00000'))"  and at the same time, check if the value is already used in
thetable. 

 

in pseudo code it would look something like this:

 

generateMProductSKU(){
    skuGen=""
    needToGenerate = true
    while(needToGenerate){
      skuGen = ltrim(to_char(luhn_generate(round(random()*10000)::int), '00000'))
      if((Select count(*) from M_Product where sku =skuGen) = 0
         needToGenerate =  false
   }
   return skuGen
}

 

Culd somebody help me tu structure code into right sql format so it could be inserted into database.

 

Thank you very much.

Peter

В списке pgsql-novice по дате отправления:

Предыдущее
От: Chuck Roberts
Дата:
Сообщение: How to create tables and copy records in blocks?
Следующее
От: Roxanne Reid-Bennett
Дата:
Сообщение: Re: Random Number SKU Generator