Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

MySQL/SQLite

I want to insert a randomly generated number (of 9 positions) into multiple rows BUT they need to be the same for all rows matched in the query.

update products set tag_seed=( SELECT ABS(RANDOM() % 999999999) ) where [...];

Partialy works... Each row will have a different random number. I need them to be same.

share|improve this question

1 Answer 1

up vote 0 down vote accepted

This is logical since you will generate a new random for every update query. The easiest solution is to generate a random integer, store it in a local variable and use that variable in your queries:

SET @rand := (SELECT ABS(RAND() * 1000000000));
update products set tag_seed=@rand where [...];
share|improve this answer
    
Thanks. Is it possible in SQLite? –  user590584 Jun 9 '13 at 0:34
    
SQLite does not support local variables as far as i know, you could store the random in a variable of your program though, or use a temp table with one field in which you temporarily store your value. Then you can fetch it in your update query using a subquery. –  Glenn Vandamme Jun 9 '13 at 10:19

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.