Solved

selecting random values from table in SQL Server 2000

Posted on 2007-12-03
11
228 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
Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

 
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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

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

Suggested Solutions

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
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.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

911 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

20 Experts available now in Live!

Get 1:1 Help Now