Click here to Skip to main content
15,916,215 members
Home / Discussions / Database
   

Database

 
GeneralRe: how do i acess ms sql database using pocket pc Pin
AlexeiXX318-May-09 16:41
AlexeiXX318-May-09 16:41 
GeneralRe: how do i acess ms sql database using pocket pc Pin
ChiSmile22-May-09 16:23
ChiSmile22-May-09 16:23 
GeneralRe: how do i acess ms sql database using pocket pc Pin
AlexeiXX322-May-09 17:40
AlexeiXX322-May-09 17:40 
GeneralRe: how do i acess ms sql database using pocket pc Pin
ChiSmile24-May-09 12:23
ChiSmile24-May-09 12:23 
AnswerRe: how do i acess ms sql database using pocket pc Pin
Luc Pattyn16-May-09 10:02
sitebuilderLuc Pattyn16-May-09 10:02 
GeneralRe: how do i acess ms sql database using pocket pc Pin
ChiSmile16-May-09 16:02
ChiSmile16-May-09 16:02 
QuestionNew_Id() in SQL Server 2005 Pin
Isaac Gordon15-May-09 19:30
Isaac Gordon15-May-09 19:30 
AnswerRe: New_Id() in SQL Server 2005 [modified] Pin
Niladri_Biswas4-Jul-09 21:08
Niladri_Biswas4-Jul-09 21:08 
newid() doesn't actually generate random numbers, it generates GUIDs, which have a random part to them, and serve their purpose well for random ordering.

NEWID()
--------
- NEWID() generates GUID's instead of random numbers which is of type in a uniqueidentifier.

Try this:
SELECT NEWID() AS 'NEWID'


Output:
NEWID
-----
259ED1EE-AE35-41D2-9FCA-9FA9538AE5FF


- NEWID is guaranteed to be unique

RAND()
------
- Returns float value between 0 to 1.

Try this:

SELECT RAND() AS 'RANDOM NUMBER'


Output:

RANDOM NUMBER
------------
0.0431138996610296


- RAND() is not guaranteed to be unique

- SQL Server estimation plan says, sorting with NewId() is more time
consuming than Rand(). Rand() number generation is based on time factor
If we sort records with Rand(),the records will get same rand number and
result will be the same as previous.
e.g.

SELECT RAND() RANDOMCOLUMNS,COL1 
  FROM MYTABLE 
  ORDER BY RANDOMCOLUMNS


Output:

RANDOMCOLUMNS COL1
--------------------------------------------------------
0.227066653916067 1
0.227066653916067 2
0.227066653916067 3
0.227066653916067 4

As you can make out that the "RANDOMCOLUMNS" values are same through out.
Because random generation factor does not change during record selection.

Whereas, the same query with NEWID() will give the following

RANDOMCOLUMNS COL1
--------------------------------------------------------
B953F9A9-BE22-4651-8669-5CC6A5269911 4
50293D75-4139-4A75-883F-81DAA91ED4E6 1
0A9B1D56-68FD-4060-AA6B-89791DAF8924 3
4C3A2765-4EA2-493B-8AA9-EBA414D7AAC2 2

As you can make out that, unique GUID's has been created in this case.

So in a simple query, doing a simple "ORDER BY RAND()" will not randomly
order the results at all.In this context,NEWID() is much better, though
not more efficient.

For more information on "Generate random sets in SQL Server with NewID()", you can go through this tutorial

http://articles.techrepublic.com.com/5100-10878_11-6089823.html#[^] by by Arthur Fuller.

More on RAND()
http://msdn.microsoft.com/en-us/library/ms177610.aspx[^]

and NEWID()
1)
http://msdn.microsoft.com/en-us/library/aa276822(SQL.80).aspx[^]

2)http://www.sqlmag.com/Article/ArticleID/97032/sql_server_97032.html[^]

3)http://www.sqlmag.com/Article/ArticleID/99807/sql_server_99807.html[^]

N.B.~RAND() sorts on the constant (per SELECT)
NEWID() sorts on a dynamic (per row)

That is why if you look carefully, the COL1 values has been changed in
the 2nd case while it is same in the first.


For information about how are GUIDs sorted by SQL Server, look here

http://sqlblog.com/blogs/alberto_ferrari/archive/2007/08/31/how-are-guids-sorted-by-sql-server.aspx[^]

Hope this helps.
Smile | :)

Niladri Biswas

modified on Sunday, July 5, 2009 8:04 AM

QuestionRecursive call to stored procedure Pin
ksarchana15-May-09 16:25
ksarchana15-May-09 16:25 
AnswerRe: Recursive call to stored procedure Pin
Mycroft Holmes15-May-09 18:28
professionalMycroft Holmes15-May-09 18:28 
GeneralRe: Recursive call to stored procedure Pin
Niladri_Biswas7-Jun-09 0:28
Niladri_Biswas7-Jun-09 0:28 
GeneralRe: Recursive call to stored procedure Pin
Mycroft Holmes7-Jun-09 2:57
professionalMycroft Holmes7-Jun-09 2:57 
Questionlocked transaction Pin
Morad SAJID15-May-09 10:32
Morad SAJID15-May-09 10:32 
AnswerRe: locked transaction Pin
Henry Minute15-May-09 12:02
Henry Minute15-May-09 12:02 
AnswerRe: locked transaction Pin
David Mujica16-May-09 13:47
David Mujica16-May-09 13:47 
QuestionProblem with between function with date timepicker Pin
nazimghori15-May-09 8:18
nazimghori15-May-09 8:18 
AnswerRe: Problem with between function with date timepicker Pin
Blue_Boy15-May-09 10:26
Blue_Boy15-May-09 10:26 
AnswerRe: Problem with between function with date timepicker Pin
AlexeiXX316-May-09 9:54
AlexeiXX316-May-09 9:54 
QuestionSyntax for alter table Pin
hrishiS15-May-09 3:12
hrishiS15-May-09 3:12 
AnswerRedesign your database Pin
David Mujica15-May-09 3:41
David Mujica15-May-09 3:41 
GeneralRe: Redesign your database Pin
hrishiS15-May-09 3:56
hrishiS15-May-09 3:56 
GeneralRe: Redesign your database Pin
David Mujica15-May-09 4:07
David Mujica15-May-09 4:07 
QuestionWithout rollback Pin
Rock Star.15-May-09 1:32
Rock Star.15-May-09 1:32 
AnswerRe: Without rollback Pin
Henry Minute15-May-09 1:46
Henry Minute15-May-09 1:46 
GeneralRe: Without rollback Pin
Rock Star.15-May-09 3:15
Rock Star.15-May-09 3:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.