0

I have a table called driver and i want to update the drivers' position fields ('pos_x' and pos_y) with random numbers and what i did was once select the data from table (to see how many drivers do i have) then update their position then select the data again is there another way to do this thing?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
DeadlyDagger
  • 599
  • 4
  • 12
  • 28
  • 1
    Please don't put things like "SQL Server and ASP.NET C# : " in your titles. Those are tags and belong in the tags, not the title. – John Saunders Dec 04 '11 at 19:10

2 Answers2

2

If you create a class to hold the driver information, then you can eliminate the last step (selecting the data again).

The steps would be:

1) Read the data into a List.

2) Update the values in the List.

3) Write the data from the List to the database.

competent_tech
  • 44,465
  • 11
  • 90
  • 113
  • That's not gonna work cause i want the updated values after the update command. I should retrieve them again. – DeadlyDagger Dec 04 '11 at 20:49
  • @rNuǝɹɐ: Is there a reason that you need to extract them from the database again if they are updated in memory? If so, then the outline in your question is as efficient as you can get. Otherwise, what I have suggested will eliminate that step, making the process more efficient. – competent_tech Dec 05 '11 at 02:33
  • ooh that's right, sorry i didn't get you, thanks for your help. – DeadlyDagger Dec 05 '11 at 07:57
1

I want to update the drivers' position fields (pos_x and pos_y) with random numbers

You can do this quite easily just using SQL.

UPDATE Person
SET   Pos_X = ABS(CHECKSUM(NEWID())) % 1000
    , Pos_Y = ABS(CHECKSUM(NEWID())) % 1000

As this is all done on the SQL server it means you won't incur a network overhead shipping data back and forth. Of course you will need to select the result to work with afterwards.

Why ABS-CHECKSUM-NEWID? I tried it with T-SQL's RAND() function with less than satisfactory results!

Community
  • 1
  • 1
Stephen Turner
  • 7,125
  • 4
  • 51
  • 68