?
Solved

selecting random values from table in SQL Server 2000

Posted on 2007-12-03
11
Medium Priority
?
242 Views
Last Modified: 2013-11-26
Hello,
I want to select random values from the table in sql server database.

i.e on button click we should display a value from table xyz and if we click the button again,the value should be changed and it should be some other value from the same table xyz.

and if user is satisfied with the displayed value,on button click(add button) we should insert that value in table abc and delete that particular value from table xyz.

please help!!!!

0
Comment
Question by:tag_k
  • 4
  • 4
  • 2
  • +1
11 Comments
 
LVL 75

Assisted Solution

by:Aneesh Retnakaran
Aneesh Retnakaran earned 800 total points
ID: 20401228

select * from xyz  order by newid()
0
 
LVL 6

Expert Comment

by:Rajesh_mj
ID: 20401239
SELECT TOP 1 someColumn
    FROM someTable
    ORDER BY NEWID()

To Read: http://databases.aspfaq.com/database/how-do-i-retrieve-a-random-record.html
0
 

Author Comment

by:tag_k
ID: 20401247
thank you
can you even say where to add delete statement.i want to delete the selected value from table xyz.
on button click i will be insterting the selected random value to table abc and at same time i should delete that selected random value,so that it is not displayed again as it is already used.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 20401256
Once you gets the value, you can use the DELETE statement for deleting that value from the table

DELETE FROM TableName  WHERE PrimaryKeyOfYourTable = 'PrimaryKeyValue'
0
 
LVL 25

Expert Comment

by:imitchie
ID: 20401257
set nocount on
declare @ID int
SELECT TOP 1 @ID = ID FROM XYZ ORDER BY NEWID()
DELETE XYZ WHERE ID = @ID
INSERT INTO ABC (ID, VALUE) SELECT ID, VALUE FROM XYZ WHERE ID = @ID
set nocount off

SELECT * FROM XYZ WHERE ID = @ID
0
 
LVL 25

Expert Comment

by:imitchie
ID: 20401265
commented:

set nocount on   -- for speed, and to suppress notification which may confuse some programs
declare @ID int
SELECT TOP 1 @ID = ID FROM XYZ ORDER BY NEWID()   -- get a random ID
INSERT INTO ABC (ID, VALUE) SELECT ID, VALUE FROM XYZ WHERE ID = @ID   -- move the ID to table ABC
DELETE XYZ WHERE ID = @ID   -- remove from the table XYZ, because we've used it
set nocount off   -- turn back on for final reporting (1 record only)

SELECT * FROM XYZ WHERE ID = @ID   -- this is the result returned
0
 

Author Comment

by:tag_k
ID: 20401282
I am this code ,any modifications to this code.this for inserting and deleting the value selected randomly.

on button_click
{
SqlDataReader read;
SqlConnection connection = new SqlConnection("Server=local;DataBase=comt; uid=sa;pwd=conn");
string strInsert="INSERT into xyz Values productid='" + TextBox1.Text + "',productprice='" + TextBox2.Text + "'";
SqlCommand command = new SqlCommand(strInsert,connection);
connection.Open();
command.ExecuteNonQuery();
}

i did not add delete command yet.whts the problem with insert command.
thank you.
0
 
LVL 25

Expert Comment

by:imitchie
ID: 20401293
string strInsert="INSERT into xyz (productid, productprice) Values ('" + TextBox1.Text + "','" + TextBox2.Text + "')";
0
 

Author Comment

by:tag_k
ID: 20401313
thank you imitchie,
where can i add delete  statement in above code.
0
 
LVL 25

Accepted Solution

by:
imitchie earned 1200 total points
ID: 20401326
>> insert that value in table abc and delete that particular value from table xyz.

SqlDataReader read;
SqlConnection connection = new SqlConnection("Server=local;DataBase=comt; uid=sa;pwd=conn");
string strInsert="INSERT into abc (productid, productprice) Values ('" + TextBox1.Text + "','" + TextBox2.Text + "')";
SqlCommand command = new SqlCommand(strInsert,connection);
connection.Open();
command.ExecuteNonQuery();
string strDelete="DELETE from xyz where productid='" + TextBox1.Text + "'";
command = new SqlCommand(strDelete,connection);
command.ExecuteNonQuery();
}
0
 

Author Comment

by:tag_k
ID: 20401340
Thank you all
0

Featured Post

NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Suggested Courses

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question