Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8903487
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T01:52:54+00:00 2026-06-15T01:52:54+00:00

I Want To Create A Stored procedure That return A Random Number Between (11111,99999)

  • 0

I Want To Create A Stored procedure That return A Random Number Between (11111,99999)

Provided that the Number Should Not Exist In The Table

I use This complicated Function to Do that But I Need To Convert it to Stored Procedure

    Function GiveRandomStudentNumber() As String
s:
        Dim rnd As New Random
        Dim st_num As String = rnd.Next(11111, 99999)
        Dim cmd As New SqlCommand("select count(0) from student where st_num = " & st_num,con)
        dd.con.Open()
        Dim count As Integer = cmd.ExecuteScalar()
        dd.con.Close()
        If count <> 0 Then
            GoTo s
        Else
            Return st_num
        End If
    End Function

this Function Is Works But I need To Convert it To Stored Procedure ..

Thanks In Advance …

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T01:52:55+00:00Added an answer on June 15, 2026 at 1:52 am
    CREATE PROCEDURE [dbo].[Select_RandomNumber] 
    (
    @Lower INT, --11111-- The lowest random number
    @Upper INT --99999-- The highest random number
    )
    AS
    BEGIN
    
        IF NOT (@Lower < @Upper) RETURN -1
    
        --TODO: If all the numbers between Lower and Upper are in the table,
        --you should return from here
        --RETURN -2
    
        DECLARE @Random INT;
        SELECT @Random = ROUND(((@Upper - @Lower -1) * RAND() + @Lower), 0)
    
        WHILE  EXISTS (SELECT * FROM YourTable WHERE randCol = @Random)
        BEGIN
    
            SELECT @Random = ROUND(((@Upper - @Lower -1) * RAND() + @Lower), 0)
        END
    
        SELECT @Random
    END
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create stored procedure that call recursion function, each time it will
I want to create a stored procedure (on SQL Server 2005) that fetches a
I want to create a stored procedure that takes the name of a table
I want to create a stored procedure that returns the results of a search
I want to create generic function that will need only parameter as Stored procedure
I have a stored procedure in which i want to create a user defined
I want to write a stored procedure like this Create Proc dbo.GetApplicantsByIDs as Select
I have information stored in a database that I want to use to create
I have a stored procedure that needs to return something from one of two
I have a stored Procedure that do some operation and return 1 or 0

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.