开发者

Copying select fields in one db table to another db table

I have two fields (Utter and Misery) in Table Massconfusion in database A that I need to move to two fields (also named Utter and Misery) in a table also called Massconfusion database B. There are two keys (Primary and subkey) that keep this data sorted correctly with the rest of the information in Database A an开发者_StackOverflow社区d B.

(basically we somehow lost most of the information in the two fields and are trying to get it from an old copy of our db and all of the easy methods of restoration have not worked.)

I am a total newbie at scripting in sql. So I am pleading, HELP! Thanks in advance.


UPDATE B.dbo.MassConfusion
SET Utter = (SELECT Utter FROM A.dbo.MassConfusion WHERE A.PrimeKey = B.PrimeKey)

UPDATE B.dbo.MassConfusion
SET Misery= (SELECT Misery FROM A.dbo.MassConfusion WHERE A.PrimeKey = B.PrimeKey)

You may be better off inserting into a new table depending on the number of records and how messed up they are, though....UPDATE can be slow and expensive depending on how many indexes you have, etc.


I wasn't clear on exactly what the primary key for your MassConfusion table was. The first version assumes that the primary key for MassConfusion is just Primary. If the primary key is actually a composite of Primary and SubKey, then use the second version.

Version 1: Primary key consists of one column

/* Just to make it clear that this is run from Database B */
Use B
go

update MCB
    set Utter = MCA.Utter,
        Misery = MCA.Misery
    from MassConfusion MCB
        inner join A.dbo.MassConfusion MCA
            on MCB.Primary = MCA.Primary

Version 2: Primary key is a composite of two columns

/* Just to make it clear that this is run from Database B */
Use B
go

update MCB
    set Utter = MCA.Utter,
        Misery = MCA.Misery
    from MassConfusion MCB
        inner join A.dbo.MassConfusion MCA
            on MCB.Primary = MCA.Primary
                and MCB.SubKey = MCA.SubKey
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜