Solved

selecting random values from table in SQL Server 2000

Posted on 2007-12-03
11
227 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 200 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
 
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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 300 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
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…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.

747 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now