Question : SQL 2008: how to copy schema or data from one table to another which ALREADY exists?

ENVIRONMENT: This is on SQL 2008, which appears to be VERY DIFFERENT from SQL 2005.

STATUS: One table has the desired schema, but no data.  The other table has the data, but undesirable schema.

GOAL:  
(1) how do I get the schema from the empty table, over onto the full table,
~or~
(2) how do I get the data, from the full table, over to the empty table?

ACTIONS TAKEN: I have ensured all the same columns and data types now match on both tables (and the primary key as well).

PROBLEM:  Insert into won't work and returns the error "There is already an object named ... in the database."

Answer : SQL 2008: how to copy schema or data from one table to another which ALREADY exists?

try this,

It uses column A only, and used Find to quickly find and delete macthing rows (one or more) in OpenItaly

The fast find code is based on  http://www.experts-exchange.com/A_29119.html

Cheers

Dave
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim ws As Worksheet
    Dim rng1 As Range
    Dim rng2 As Range
    Dim cel1 As Range

    If Application.Intersect(Columns("A"), Target) Is Nothing Then Exit Sub

    With Application
        .ScreenUpdating = False
    End With

    Set ws = Sheets("OpenItaly")
    Set rng1 = ws.Range(ws.[a2], ws.Cells(Rows.Count, "A").End(xlUp))
    Set cel1 = rng1.Find(Target.Value, , xlValues, xlWhole, xlByRows, , False)
    If Not cel1 Is Nothing Then
        Set rng2 = cel1
        strFirstAddress = cel1.Address
        Do
            Set cel1 = rng1.FindNext(cel1)
            Set rng2 = Union(rng2.EntireRow, cel1)
        Loop While strFirstAddress <> cel1.Address
    End If

    If Not rng2 Is Nothing Then rng2.EntireRow.Delete
    Application.ScreenUpdating = True
 
End Sub
Random Solutions  
 
programming4us programming4us