| Author |
Topic |
|
esthera
Master Smack Fu Yak Hacker
1410 Posts |
Posted - 2007-03-07 : 12:38:28
|
| how can i generate a 7 digit random number? |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2007-03-07 : 13:29:57
|
| SELECT CAST(RAND()*10000000 AS INT)Causing trouble since 1980blog: http://weblogs.sqlteam.com/mladenp |
 |
|
|
snSQL
Master Smack Fu Yak Hacker
1837 Posts |
Posted - 2007-03-07 : 13:31:10
|
| SELECT cast(round((8999998 * Rand() + 1000000), 0) as int) |
 |
|
|
esthera
Master Smack Fu Yak Hacker
1410 Posts |
Posted - 2007-03-07 : 13:36:07
|
| thanks... |
 |
|
|
esthera
Master Smack Fu Yak Hacker
1410 Posts |
Posted - 2007-03-07 : 13:40:36
|
| one more thing if you can help (thanks)i need to add to this sp to check the customer table and if any records exist with p= the new generated p then it should regenerate -- how can i do this?(it needs to be a random 7 digit unique number) |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
snSQL
Master Smack Fu Yak Hacker
1837 Posts |
Posted - 2007-03-07 : 13:58:10
|
| DECLARE @NewNumber intWHILE @NewNumber IS NULLBEGIN SET @NewNumber = cast(round((8999998 * Rand() + 1000000), 0) as int) IF EXISTS (SELECT * FROM Customer WHERE p = @NewNumber) SET @NewNumber = NULL --Not unique ELSE --Use @NewNumber, it is uniqueEND--Can use --SELECT @NewNumber --here if you want to return the number that was just inserted |
 |
|
|
esthera
Master Smack Fu Yak Hacker
1410 Posts |
Posted - 2007-03-07 : 14:09:31
|
| ok so my revised code isALTER PROCEDURE [dbo].[spnewp] -- Add the parameters for the stored procedure here (@p int output)ASBEGIN SET NOCOUNT ON;WHILE @p IS NULLBEGINSET @p = cast(round((8999998 * Rand() + 1000000), 0) as int)IF EXISTS (SELECT * FROM customers WHERE p = @p)SET @p = NULL --Not uniqueELSE--Use @NewNumber, it is uniqueENDselect @p i'm getting error Msg 156, Level 15, State 1, Procedure spnewp, Line 24Incorrect syntax near the keyword 'select'.Msg 102, Level 15, State 1, Procedure spnewp, Line 26Incorrect syntax near 'OFF'. |
 |
|
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
|
|
esthera
Master Smack Fu Yak Hacker
1410 Posts |
Posted - 2007-03-07 : 14:45:18
|
| but what's wrong in my syntax above? |
 |
|
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2007-03-07 : 14:55:23
|
quote: Originally posted by esthera but what's wrong in my syntax above?
It's incorrect.CODO ERGO SUM |
 |
|
|
esthera
Master Smack Fu Yak Hacker
1410 Posts |
Posted - 2007-03-07 : 15:00:25
|
| what is wrong with it? the selecting of random number worked but within the stored procedure there is an error |
 |
|
|
snSQL
Master Smack Fu Yak Hacker
1837 Posts |
Posted - 2007-03-07 : 16:02:33
|
| You're missing the END keyword at the end of the procedure, you have a BEGIN at the beginning, but no matching END. |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2007-03-07 : 17:38:18
|
| You want the requirements to also make sense?CODO ERGO SUM |
 |
|
|
esthera
Master Smack Fu Yak Hacker
1410 Posts |
Posted - 2007-03-08 : 00:41:00
|
| can you please explain me how to do this?this works SELECT cast(round((8999998 * Rand() + 1000000), 0) as int)why doesn't it work in the sp? |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-03-08 : 00:44:45
|
"why doesn't it work in the sp?"How can we help you if you did not post the full SP with the error message.You have this errorquote: Msg 156, Level 15, State 1, Procedure spnewp, Line 24Incorrect syntax near the keyword 'select'.Msg 102, Level 15, State 1, Procedure spnewp, Line 26Incorrect syntax near 'OFF'.
But there isn't any key word OFF in the partial SP that you posted. KH |
 |
|
|
|