Solved

selecting random values from table in SQL Server 2000

Posted on 2007-12-03
11
229 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

778 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