Moving data in SQL Server using where table1.jobid = table 2.jobid

Go To StackoverFlow.com

1

Just wondering if someone could help me with a very simple SQL query.

I need to transfer data from table1 to table2 where table1.jobID = table2.jobID

Should I use join ?

I would just like to fill in a NULL column with data from table1, using jobID to match.

Thanks in Advance

AC

2012-04-05 01:47
by Mikey3Strings
I tried this but i failed: INSERT INTO [DocPrep].[dbo].[PreviousJobsTEST] (dValuationDate)

FROM [DocPrep].[dbo].[Jobs]

WHERE [DocPrep].[dbo].[Jobs] (iJobID) = [DocPrep].[dbo].[PreviousJobsTEST] (iJobID - Mikey3Strings 2012-04-05 01:49

Welcome to Stack Overflow! I always like to welcome new members and let them know that appreciation is shown via upvotes and accepted answers (check next to answer). Please see the [faq] for more info, especially the [ask - Justin Pihony 2012-04-05 01:57


0

With the limited knowledge it sounds like all you need is a simple UPDATE:

UPDATE PreviousJobsTEST
SET dValuationDate = Jobs.dValuationDate
FROM PreviousJobsTEST
    JOIN Jobs
        ON PreviousJobsTEST.iJobID= Jobs.iJobID

If you only want it updated if it is null, then you can do this:

UPDATE PreviousJobsTEST
SET dValuationDate = CASE WHEN PreviousJobsTEST.dValuationDate IS NULL THEN Jobs.dValuationDate ELSE PreviousJobsTEST.dValuationDate END
FROM PreviousJobsTEST
    JOIN Jobs
        ON PreviousJobsTEST.iJobID= Jobs.iJobID

OR, based on your comment, you could do what you have listed, you just need to turn it into a SELECT behind the insert:

INSERT INTO [DocPrep].[dbo].[PreviousJobsTEST] (dValuationDate) 
SELECT [DocPrep].[dbo].[Jobs].[dValuationDate]
FROM [DocPrep].[dbo].[PreviousJobsTEST]
    JOIN [DocPrep].[dbo].[Jobs] 
        ON [DocPrep].[dbo].[Jobs].[iJobID] = [DocPrep].[dbo].[PreviousJobsTEST].[iJobID]
2012-04-05 01:50
by Justin Pihony
Im getting:

Msg 209, Level 16, State 1, Line 2 Ambiguous column name 'dValuationDate' - Mikey3Strings 2012-04-05 01:58

@AtomicCockroach I have updated my answe - Justin Pihony 2012-04-05 01:59
Im trying the first update claus - Mikey3Strings 2012-04-05 02:01
(48572 row(s) affected - Mikey3Strings 2012-04-05 02:02
Wait..... ummm not transfering - Mikey3Strings 2012-04-05 02:02
@AtomicCockroach Updated answer....however, are you trying to only update if the value being updated is null - Justin Pihony 2012-04-05 02:07
they are all null. I just added the column to PreviousJobsTES - Mikey3Strings 2012-04-05 02:12
OK GOT IT MATEY

THANK YOU

I USED

UPDATE [DocPrep].[dbo].[PreviousJobsTEST] SET dValuationDate = Jobs.dValuationDate FROM PreviousJobsTEST JOIN Jobs ON PreviousJobsTEST.iJobID= Jobs.iJobID

THE FIRST SUGGESTIO - Mikey3Strings 2012-04-05 02:15

The query created an extra 50,000 entries. I assume that it just dumped the values with mathcing. However, the ones that did not match created Null entries - Mikey3Strings 2012-04-05 02:19
@AtomicCockroach An update should not create any extra rows...That came from the insert attempt...You will need a case (as my 2nd update states) if you want to avoid NULL overrides - Justin Pihony 2012-04-05 03:14
U are absolutley right. Thanks again mat - Mikey3Strings 2012-04-05 03:28


0

You need to update when t1.column is null . and when t1.column is not null you should set it to itself.

SELECT *
INTO   #table1
FROM   (SELECT '1'  AS jobid,
               NULL AS column1
        UNION ALL
        SELECT '2',
               NULL
        UNION ALL
        SELECT '3',
               NULL
        UNION ALL
        SELECT '4',
               'A'
        UNION ALL
        SELECT '5',
               NULL) a

SELECT *
INTO   #table2
FROM   (SELECT '1' AS jobid,
               '1' AS column1
        UNION ALL
        SELECT '2',
               '2'
        UNION ALL
        SELECT '3',
               '3'
        UNION ALL
        SELECT '4',
               '4'
        UNION ALL
        SELECT '5',
               '5')b

UPDATE t1
SET    t1.column1 = ( CASE
                        WHEN t1.column1 IS NULL THEN t2.column1
                        ELSE t1.column1
                      END )
FROM   #table1 t1
       LEFT JOIN #table2 t2
         ON t1.jobid = t2.jobid

SELECT *
FROM   #table1  
2012-04-05 02:06
by shenhengbin
Ads