DeadlyDagger
DeadlyDagger

Reputation: 609

Update Multiple Records of Two Fields

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?

Upvotes: 0

Views: 266

Answers (2)

Stephen Turner
Stephen Turner

Reputation: 7314

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!

Upvotes: 1

competent_tech
competent_tech

Reputation: 44931

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.

Upvotes: 2

Related Questions