Quantcast
Channel: ฟอรัม Getting started with SQL Server
Viewing all articles
Browse latest Browse all 8428

SQL Verification changes

$
0
0

I have a table that has records removed and the underlying index order no longer matches the order of the numbers in the ID field. As soon as a new record is added, SQL studio shifts it somewhere else according to the original row orders.

I have reordered the master table and now want to assign the new ID to replace the old ID value in another table.

I have done all this on a spreadsheet but the table that needs updating has 30,000 or more rows and changing things manually is bound to bring in errors.

So I need to read table 2, ID field and if this ID matches the old ID in master table, replace it with the new ID.

The tables belong to an online game whereby you have approx. 1600 items (master table) and each player can have any number of these items (table 2).
If the ID change is inaccurate, player items will be transformed into something else.

I was kindly given some code below

~~~~

UPDATE ChildTable SET NewId = MasterTable.NewId
FROM ChildTable
INNER JOIN MasterTable ON ChildTable.OldId = MasterTable.OldId

~~~~

which I converted to use my actual table names etc. as below.

~~~~

UPDATE RES_STDITEMS SET NEWID = IDTABLE.NEWID
FROM RES_STDITEMS
INNER JOIN IDTABLE ON RES_STDITEMS.ID = IDTABLE.oldID

~~~~

After accepting the verification in MS Management Studio 2014, it gives me this.

~~~~

UPDATE       RES_STDITEMS
SET                NEWID = IDTABLE.NEWID
FROM            RES_STDITEMS AS RES_STDITEMS_1 INNER JOIN
                         IDTABLE ON RES_STDITEMS_1.ID = IDTABLE.oldID CROSS JOIN
                         RES_STDITEMS

~~~~

The result is all new IDs become 433. This is the new ID for old ID number 1.

I'm only a basic level SQL user so be gentle.

Thanks.


Viewing all articles
Browse latest Browse all 8428

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>