Home All Groups Group Topic Archive Search About

Problem copying data from column in same table

Author
20 Jan 2006 4:26 PM
Iris Faber
I have a column that I would like to copy data to.  The column definitions are exactly the same.  I tried creating an Insert Into query and changed the type to Insert Results.  Everytime I run this query, all the values in my column remain to be null.  Below is a sample of what I did:

INSERT INTO Customers
                      ([CustomerID1])
SELECT     [CustomerID]
FROM         Customers AS Customers_1

I got a message back '(116873 row(s) affected)'.  I don't know what it affected.

Thanks, Iris

Author
20 Jan 2006 4:44 PM
Jim Underwood
Your SQL, if I read it correctly, is creating a new row for every existing
row in your table, with only one column (CustomerID1) populated.  It is not
discerning between null values and non null values.  So, if you start with
the following data:

CustomerID, CustomerID1, Name
1, NULL, John
2, NULL, Sue
3, NULL, Tony

Run the SQL once and you will get this data in the table:

CustomerID, CustomerID1, Name
1, NULL, John
2, NULL, Sue
3, NULL, Tony
NULL, 1, NULL
NULL, 2, NULL
NULL, 3, NULL

Run the SQL again and you will get this data in the table:

CustomerID, CustomerID1, Name
1, NULL, John
2, NULL, Sue
3, NULL, Tony
NULL, 1, NULL
NULL, 2, NULL
NULL, 3, NULL
NULL, 1, NULL
NULL, 2, NULL
NULL, 3, NULL
NULL, NULL, NULL
NULL, NULL, NULL
NULL, NULL, NULL

If your SQL is doing this, then you don't have a key on your table, which is
your first problem.

I assume that you want to update the CustomerID1 field in the existing rows,
not insert a new row for every exiusting one.  The following SQL should do
this.

Update Customers set CustomerID1 = CustomerID

However, I cant think of a reason why you would want to do this.  You
probably have some database design issues to work out.  Once you get the
design down, you probably wont have a need to do this at all.


"Iris Faber" <Iris.Fa***@mid.state.ms.us> wrote in message
news:eVU%23k3dHGHA.1288@TK2MSFTNGP09.phx.gbl...
I have a column that I would like to copy data to.  The column definitions
are exactly the same.  I tried creating an Insert Into query and changed the
type to Insert Results.  Everytime I run this query, all the values in my
column remain to be null.  Below is a sample of what I did:

INSERT INTO Customers
                      ([CustomerID1])
SELECT     [CustomerID]
FROM         Customers AS Customers_1

I got a message back '(116873 row(s) affected)'.  I don't know what it
affected.

Thanks, Iris
Author
20 Jan 2006 5:10 PM
Iris Faber
Jim,

There is no key on this table because it is a table with historical records.  I pulled this data from a mainframe database. 

>>> Jim Underwood<james.underwoodATfallonclinic.com> 1/20/2006 10:44:23 AM >>>

Your SQL, if I read it correctly, is creating a new row for every existing
row in your table, with only one column (CustomerID1) populated.  It is not
discerning between null values and non null values.  So, if you start with
the following data:

CustomerID, CustomerID1, Name
1, NULL, John
2, NULL, Sue
3, NULL, Tony

Run the SQL once and you will get this data in the table:

CustomerID, CustomerID1, Name
1, NULL, John
2, NULL, Sue
3, NULL, Tony
NULL, 1, NULL
NULL, 2, NULL
NULL, 3, NULL

Run the SQL again and you will get this data in the table:

CustomerID, CustomerID1, Name
1, NULL, John
2, NULL, Sue
3, NULL, Tony
NULL, 1, NULL
NULL, 2, NULL
NULL, 3, NULL
NULL, 1, NULL
NULL, 2, NULL
NULL, 3, NULL
NULL, NULL, NULL
NULL, NULL, NULL
NULL, NULL, NULL

If your SQL is doing this, then you don't have a key on your table, which is
your first problem.

I assume that you want to update the CustomerID1 field in the existing rows,
not insert a new row for every exiusting one.  The following SQL should do
this.

Update Customers set CustomerID1 = CustomerID

However, I cant think of a reason why you would want to do this.  You
probably have some database design issues to work out.  Once you get the
design down, you probably wont have a need to do this at all.


"Iris Faber" <Iris.Fa***@mid.state.ms.us> wrote in message
news:eVU%23k3dHGHA.1288@TK2MSFTNGP09.phx.gbl...
I have a column that I would like to copy data to.  The column definitions
are exactly the same.  I tried creating an Insert Into query and changed the
type to Insert Results.  Everytime I run this query, all the values in my
column remain to be null.  Below is a sample of what I did:

INSERT INTO Customers
                      ([CustomerID1])
SELECT     [CustomerID]
FROM         Customers AS Customers_1

I got a message back '(116873 row(s) affected)'.  I don't know what it
affected.

Thanks, Iris
Author
20 Jan 2006 5:24 PM
Jim Underwood
At a minimum you should have certain fields defined as not null (CustomerID for example).  There should also be some way of identifying unique records, even in a history table (although not in an audit table).  Usually when I have worked with history tables they would have a date field that would be part of the primary key.  When multiple entries were allowed on the same date, some sort of sequence field was used to insure uniqueness.

"Iris Faber" <Iris.Fa***@mid.state.ms.us> wrote in message news:OYAFhQeHGHA.528@TK2MSFTNGP12.phx.gbl...
  Jim,

  There is no key on this table because it is a table with historical records.  I pulled this data from a mainframe database. 

  >>> Jim Underwood<james.underwoodATfallonclinic.com> 1/20/2006 10:44:23 AM >>>

  Your SQL, if I read it correctly, is creating a new row for every existing
  row in your table, with only one column (CustomerID1) populated.  It is not
  discerning between null values and non null values.  So, if you start with
  the following data:

  CustomerID, CustomerID1, Name
  1, NULL, John
  2, NULL, Sue
  3, NULL, Tony

  Run the SQL once and you will get this data in the table:

  CustomerID, CustomerID1, Name
  1, NULL, John
  2, NULL, Sue
  3, NULL, Tony
  NULL, 1, NULL
  NULL, 2, NULL
  NULL, 3, NULL

  Run the SQL again and you will get this data in the table:

  CustomerID, CustomerID1, Name
  1, NULL, John
  2, NULL, Sue
  3, NULL, Tony
  NULL, 1, NULL
  NULL, 2, NULL
  NULL, 3, NULL
  NULL, 1, NULL
  NULL, 2, NULL
  NULL, 3, NULL
  NULL, NULL, NULL
  NULL, NULL, NULL
  NULL, NULL, NULL

  If your SQL is doing this, then you don't have a key on your table, which is
  your first problem.

  I assume that you want to update the CustomerID1 field in the existing rows,
  not insert a new row for every exiusting one.  The following SQL should do
  this.

  Update Customers set CustomerID1 = CustomerID

  However, I cant think of a reason why you would want to do this.  You
  probably have some database design issues to work out.  Once you get the
  design down, you probably wont have a need to do this at all.


  "Iris Faber" <Iris.Fa***@mid.state.ms.us> wrote in message
  news:eVU%23k3dHGHA.1288@TK2MSFTNGP09.phx.gbl...
  I have a column that I would like to copy data to.  The column definitions
  are exactly the same.  I tried creating an Insert Into query and changed the
  type to Insert Results.  Everytime I run this query, all the values in my
  column remain to be null.  Below is a sample of what I did:

  INSERT INTO Customers
                        ([CustomerID1])
  SELECT     [CustomerID]
  FROM         Customers AS Customers_1

  I got a message back '(116873 row(s) affected)'.  I don't know what it
  affected.

  Thanks, Iris
Author
20 Jan 2006 5:31 PM
Iris Faber
Ok, I'm remembering this now.  It does have a transaction date and time field.  I'll set that as the unique key.   Thanks.

>>> Jim Underwood<james.underwoodATfallonclinic.com> 1/20/2006 11:24:26 AM >>>

At a minimum you should have certain fields defined as not null (CustomerID for example).  There should also be some way of identifying unique records, even in a history table (although not in an audit table).  Usually when I have worked with history tables they would have a date field that would be part of the primary key.  When multiple entries were allowed on the same date, some sort of sequence field was used to insure uniqueness.

"Iris Faber" <Iris.Fa***@mid.state.ms.us> wrote in message news:OYAFhQeHGHA..528@TK2MSFTNGP12.phx.gbl...
Jim,

There is no key on this table because it is a table with historical records.  I pulled this data from a mainframe database. 

>>> Jim Underwood<james.underwoodATfallonclinic.com> 1/20/2006 10:44:23 AM >>>

Your SQL, if I read it correctly, is creating a new row for every existing
row in your table, with only one column (CustomerID1) populated.  It is not
discerning between null values and non null values.  So, if you start with
the following data:

CustomerID, CustomerID1, Name
1, NULL, John
2, NULL, Sue
3, NULL, Tony

Run the SQL once and you will get this data in the table:

CustomerID, CustomerID1, Name
1, NULL, John
2, NULL, Sue
3, NULL, Tony
NULL, 1, NULL
NULL, 2, NULL
NULL, 3, NULL

Run the SQL again and you will get this data in the table:

CustomerID, CustomerID1, Name
1, NULL, John
2, NULL, Sue
3, NULL, Tony
NULL, 1, NULL
NULL, 2, NULL
NULL, 3, NULL
NULL, 1, NULL
NULL, 2, NULL
NULL, 3, NULL
NULL, NULL, NULL
NULL, NULL, NULL
NULL, NULL, NULL

If your SQL is doing this, then you don't have a key on your table, which is
your first problem.

I assume that you want to update the CustomerID1 field in the existing rows,
not insert a new row for every exiusting one.  The following SQL should do
this.

Update Customers set CustomerID1 = CustomerID

However, I cant think of a reason why you would want to do this.  You
probably have some database design issues to work out.  Once you get the
design down, you probably wont have a need to do this at all.


"Iris Faber" <Iris.Fa***@mid.state.ms.us> wrote in message
news:eVU%23k3dHGHA.1288@TK2MSFTNGP09.phx.gbl...
I have a column that I would like to copy data to.  The column definitions
are exactly the same.  I tried creating an Insert Into query and changed the
type to Insert Results.  Everytime I run this query, all the values in my
column remain to be null.  Below is a sample of what I did:

INSERT INTO Customers
                      ([CustomerID1])
SELECT     [CustomerID]
FROM         Customers AS Customers_1

I got a message back '(116873 row(s) affected)'.  I don't know what it
affected.

Thanks, Iris
Author
20 Jan 2006 4:46 PM
ML
This is normal as you're not "copying" the values, you're inserting new rows,
where only the new column is populated with data.

You need to UPDATE the new column with the values in the old column.

update <table>
           set <new column> = <old column>


ML

---
http://milambda.blogspot.com/
Author
20 Jan 2006 4:54 PM
Iris Faber
That's correct, I'd rather update.  I'll have to start back over since my test table now has all those null values in it like Jim mentioned.  Thanks.

>>> ML***@discussions.microsoft.com> 1/20/2006 10:46:02 AM >>>

This is normal as you're not "copying" the values, you're inserting new rows,
where only the new column is populated with data.

You need to UPDATE the new column with the values in the old column.

update <table>
           set <new column> = <old column>


ML

---
http://milambda.blogspot.com/
Author
20 Jan 2006 5:15 PM
Iris Faber
I did this and it shows (116873 row(s) affected).  What else could I possibly be doing wrong?

Iris

>>> ML***@discussions.microsoft.com> 1/20/2006 10:46:02 AM >>>

This is normal as you're not "copying" the values, you're inserting new rows,
where only the new column is populated with data.

You need to UPDATE the new column with the values in the old column.

update <table>
           set <new column> = <old column>


ML

---
http://milambda.blogspot.com/
Author
20 Jan 2006 5:20 PM
Jim Underwood
Thats what it should show.  You are updating every existing row, and populating CustomerID1 with the value from CustomerID.
  "Iris Faber" <Iris.Fa***@mid.state.ms.us> wrote in message news:ut408SeHGHA.3408@TK2MSFTNGP12.phx.gbl...
  I did this and it shows (116873 row(s) affected).  What else could I possibly be doing wrong?

  Iris

  >>> ML***@discussions.microsoft.com> 1/20/2006 10:46:02 AM >>>

  This is normal as you're not "copying" the values, you're inserting new rows,
  where only the new column is populated with data.

  You need to UPDATE the new column with the values in the old column.

  update <table>
             set <new column> = <old column>


  ML

  ---
  http://milambda.blogspot.com/
Author
20 Jan 2006 6:42 PM
ML
Aren't you trying to get a copy of ALL the values?


ML

---
http://milambda.blogspot.com/
Author
20 Jan 2006 7:57 PM
Iris Faber
The tables that I'm working with need some normalizing, so I'm backing up now.

>>> ML***@discussions.microsoft.com> 1/20/2006 12:42:02 PM >>>

Aren't you trying to get a copy of ALL the values?


ML

---
http://milambda.blogspot.com/
Author
20 Jan 2006 4:49 PM
Razvan Socol
Hello, Iris

I guess that you want to use something like this:
UPDATE Customers SET CustomerID1=CustomerID

However, if the INSERT that you wrote was executed sucessfully, I
suspect that you got a lot of rows with invalid or missing
informations, because the number of rows in the table was doubled (i.e.
you now may have 233566 rows in the table).

Razvan
Author
20 Jan 2006 4:54 PM
Iris Faber
That's correct :)

>>> Razvan Socol<rso***@gmail.com> 1/20/2006 10:49:01 AM >>>

Hello, Iris

I guess that you want to use something like this:
UPDATE Customers SET CustomerID1=CustomerID

However, if the INSERT that you wrote was executed sucessfully, I
suspect that you got a lot of rows with invalid or missing
informations, because the number of rows in the table was doubled (i.e.
you now may have 233566 rows in the table).

Razvan

AddThis Social Bookmark Button