Skip to content

How to Generate Random Number

magesh manavalan

Asked Feb 12, 2012, 6:43 PM EST2 answers
Share
LinkedInX
How to Generate Random Number using Asp.Net with SQL Server.

Please Tell me with Examples.

For Example EmpNO:100 , Random Number 12876 for each Employee different Random Numbers.


Thank&Regs

Manavalan

2 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • Suthish Nair
    0
    Why not setting the column as Unique Key and set the Incremental value.
    0
  • Senthilkumar
    0
    Hi,

    Its not good practice to generate the random number on the ID column. It should  be auto generated number. SQL Server helps it make easier.

    when you create the table you have to do like this.

    create table Employee
    (
      EmployeeID INT PRIMARY KEY IDENTITY(1,1)
    , EmployeeName VARCHAR(100),
    etc..,
    )

    What it does, it will not allow you to insert the value on the identity column until unless you set identity off.

    When you try to insert the record, it will start from 1 and incremented by one (first value is starting and second one is incremental value).

    Even when you delete the record it will not affect. But when you truncate the table then it will reset the value.
    0