Hi,
I am in a situation where our developer is on leave (annual leave for a month), and I have to add a control to my website, which is in aspx apges.
To start with i have created a table in my SQL database. this table has records with one-liners from various movies, and the movie title. The tabel have 3 columns, i.e. ID, Liners, MTitle.
So i want to get random records on the page when ever its refreshed. I am totally non-coder/programmer guy.
I have got a SQL statement from the internet " SELECT TOP 1 * FROM <table name> ORDER By NEWID() "
So would anyone please help me out with it, as is it correct, how can i apply in the aspx pages.
Thank you.
Hi,
If you want to get random records from the database via a single SQL statememt, you may try the method below:
Assuming there is a unique identifier for each row, and that there is at least one record in the table, retrieving a random record can be quite easy. This method will work in SQL Server 7.0 and above (running on Windows 2000), but this could be a performance problem on larger tables / resultsets:
SELECT TOP 1 someColumn FROM someTable ORDER BY NEWID()
If you are not running on Windows 2000, then the following code will work if your table has a unique identifier (e.g. IDENTITY) column:
SELECT TOP 1 someColumn FROM someTable ORDER BY RAND((1000*IDColumn)*DATEPART(millisecond, GETDATE()))
Note that both of these methods also allow you to select 10 or 50 or 5000 random records, simply by altering the TOP parameter
Hope it helps.
No comments:
Post a Comment