Click here to Skip to main content
15,894,306 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to insert all unique random number into my column. With the below query all i am getting is same number.

SQL
INSERT INTO CurrentSessionVariableDetails
    (SessionId, FirstName, LastName, AddedForId, AddedById, CompanyId, ConnectionString, DataForYear, RoleName, ManagerName, CompanyLogoPath, IncludeProfileRatings, IncludeCompetency, ManagerId, LabelForCompetency, IsPerformanceRatingWithValue, IncludeDualObjRatingRev, IncludeSignOff, IsCompetencyRatingWithValue, IncludeObjectiveRating, RecordCPD, IncludeCPDSignOff)
    Select
    (SELECT CAST(RAND() * 1000000 AS INT)) AS [RandomNumber],
    e.FirstName
    ,      e.LastName
    ,      e.Id
    ,      null as AddedById
    ,      e.Company_Id
    ,      'tesstring' as ConnectionString
    ,      m.DataForyear
    ,      (Select Name
    from Role as r with (nolock)
    where r.Id=e.Role_Id
    ) as RoleName
    ,      (Select emp.FirstName + ' ' + emp.LastName
    from Employee as emp with (nolock)
    where emp.Id=e.ManagerId) as ManagerName
    ,      c.Logo_Path
    ,      c.IncludeProfileRatings
    ,      c.IncludeCompetency
    ,      e.ManagerId
    ,      c.labelforcompetency
    ,      c.IsPerformanceRatingWithValue
    ,      c.IncludeDualObjectiveRatingReview
    ,      c.includeSignOff
    ,      c.IsCompetencyRatingWithValue
    ,      c.IncludeObjectiveRating
    ,      e.RecordCPD
    ,      c.IncludeCPDSignedOff
    from       Employee         as e with (nolock)
    inner join MidYearAppraisal as m with (nolock) on e.Id = m.MidYear_AddedFor_Id
    inner join Company          as c with (nolock) on c.Id=e.Company_Id
    Where m.isPublished =1
Posted
Updated 2-Aug-15 20:52pm
v2

1 solution

If inserting unique number is your concern then
There could be two alternatives.
1. Add a new columns with Auto increment property and can also be used as a primary key for this table.
2. If you don't want to add another column use function ROW_NUMBER() along with ORDER clause. There are many articles on the net like OVER CLAUSE: MSDN[^]

How ever if you want to insert random numbers only, as per MSDN RAND Function SQL: MSDN[^] "Repetitive calls of RAND() with the same seed value return the same results." So there is a new function called
NEWID()
which can be used or else you can take your result to a variable and loop over it to return Random number using RAND() function. Hope this helps.

Regards,
Sujay C.
 
Share this answer
 
v2
Comments
Torakami 3-Aug-15 4:01am    
Thanks , NewID() is giving random , but is there any way from which i can get integer value ??
SujayC 3-Aug-15 7:14am    
There are way to get an integer value but I am not certain about their authenticity. As per my experience they have always failed to give me unique value. You can still give them a shot one example is like ABS(CHECKSUM(NEWID())) or SUBSTRING(CAST(ABS(CHECKSUM(NEWID())) AS VARCHAR),0,5) just to shorten the length of digits within the number.
Torakami 3-Aug-15 20:42pm    
That's cool , anyways my intention was to create some random thing , this will work for me , and i will shorten the length as well .. thanks man ..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900