?
Solved

selecting random values from table in SQL Server 2000

Posted on 2007-12-03
11
Medium Priority
?
235 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Cloud Training Guides

FREE GUIDES: In-depth and hand-crafted Linux, AWS, OpenStack, DevOps, Azure, and Cloud training guides created by Linux Academy instructors and the community.

 
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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Suggested Courses

770 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