# SQL Copy Row Data

**URL:** <https://forum.xojo.com/t/sql-copy-row-data/80305>\
**Category:** Windows\
**Created:** [May 14, 2024, 5:48pm UTC](https://forum.xojo.com/t/sql-copy-row-data/80305 "2024-05-14T17:48:43Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Don\_Lyttle](https://forum.xojo.com/user_avatar/forum.xojo.com/don_lyttle/32/324_2.png) [@Don\_Lyttle](https://forum.xojo.com/u/Don_Lyttle)\
**Post date:** [May 14, 2024, 5:48pm UTC](https://forum.xojo.com/t/sql-copy-row-data/80305/1 "2024-05-14T17:48:43Z")

</div>

I’ve been wrestling for a full day now trying to copy all data (except primary key pKey) from CasesListboxColumn = 4 data (see database pic below) to CasesListboxColumn = 2 data (present, but not shown in the image below). I’ve tried a bunch of INSERT TO variations but without any luck. On a few of them, I got a SQL error, while the others ran without error but without any data transfer.

Help me, Obi Wan … you’re my last hope! 😉

Xojo 2016 r1  
Windows 10

 ![SampleDBpic2](https://forum.xojo.com/uploads/default/original/3X/8/8/88a15e008a6d6c1751b7f878a782a44eb6ae44c7.jpeg)

---

<div class="post-metadata">

**Author:** ![William\_Reynolds](https://forum.xojo.com/user_avatar/forum.xojo.com/william_reynolds/32/565_2.png) [@William\_Reynolds](https://forum.xojo.com/u/William_Reynolds)\
**Post date:** [May 14, 2024, 5:54pm UTC](https://forum.xojo.com/t/sql-copy-row-data/80305/3 "2024-05-14T17:54:17Z")

</div>

I’m no Obi Wan, but I’m willing to (try to) help! Are you saying you are SELECTing all the data from this table WHERE ‘CasesListboxColumn = 2’, and wanting to insert those found records into another table of similar design?

---

<div class="post-metadata">

**Author:** ![Don\_Lyttle](https://forum.xojo.com/user_avatar/forum.xojo.com/don_lyttle/32/324_2.png) [@Don\_Lyttle](https://forum.xojo.com/u/Don_Lyttle)\
**Post date:** [May 14, 2024, 5:56pm UTC](https://forum.xojo.com/t/sql-copy-row-data/80305/4 "2024-05-14T17:56:06Z")

</div>

Thx for the response, William. I am trying to take all data where CasesListboxRow = 4 and copy it to records where CasesListboxRow = 2.

---

<div class="post-metadata">

**Author:** ![AlbertoD](https://forum.xojo.com/letter_avatar_proxy/v4/letter/a/dbc845/32.png) [@AlbertoD](https://forum.xojo.com/u/AlbertoD)\
**Post date:** [May 14, 2024, 5:57pm UTC](https://forum.xojo.com/t/sql-copy-row-data/80305/5 "2024-05-14T17:57:54Z")

</div>

> [@Don\_Lyttle](#):
>
> I am trying to take all data where CasesListboxRow = 4 and copy it to records where CasesListboxRow = 2.

Copy where? another table? same table?

---

<div class="post-metadata">

**Author:** ![Don\_Lyttle](https://forum.xojo.com/user_avatar/forum.xojo.com/don_lyttle/32/324_2.png) [@Don\_Lyttle](https://forum.xojo.com/u/Don_Lyttle)\
**Post date:** [May 14, 2024, 5:59pm UTC](https://forum.xojo.com/t/sql-copy-row-data/80305/6 "2024-05-14T17:59:44Z")

</div>

Let me be a bit clearer on that:

I’m taking the data from every record that has CasesListboxRow = 4 and copying it to every record that has CasesListboxRow = 2.

Maybe an easier way of saying it is … I’m trying to clone CasesListboxRow = 4 to CasesListboxRow = 2.

---

<div class="post-metadata">

**Author:** ![Don\_Lyttle](https://forum.xojo.com/user_avatar/forum.xojo.com/don_lyttle/32/324_2.png) [@Don\_Lyttle](https://forum.xojo.com/u/Don_Lyttle)\
**Post date:** [May 14, 2024, 6:01pm UTC](https://forum.xojo.com/t/sql-copy-row-data/80305/7 "2024-05-14T18:01:17Z")

</div>

My bad, Alberto. Copying to the same table. I’ve already inserted the same number of blank records for CasesListboxRows 2 and 3.

---

<div class="post-metadata">

**Author:** ![Kem\_Tekinay](https://forum.xojo.com/user_avatar/forum.xojo.com/kem_tekinay/32/154_2.png) [@Kem\_Tekinay](https://forum.xojo.com/u/Kem_Tekinay)\
**Post date:** [May 14, 2024, 6:04pm UTC](https://forum.xojo.com/t/sql-copy-row-data/80305/8 "2024-05-14T18:04:12Z")

</div>

I’m afraid I’m still unclear. Do you mean you want to duplicate the records that currently have `CasesListboxRows = 4` to new records with all the same data except a new id and with `CasesListboxRows = 2`?

---

<div class="post-metadata">

**Author:** ![Don\_Lyttle](https://forum.xojo.com/user_avatar/forum.xojo.com/don_lyttle/32/324_2.png) [@Don\_Lyttle](https://forum.xojo.com/u/Don_Lyttle)\
**Post date:** [May 14, 2024, 6:05pm UTC](https://forum.xojo.com/t/sql-copy-row-data/80305/9 "2024-05-14T18:05:43Z")

</div>

**EXACTLY!!!** … Give that man a cigar 😉

---

<div class="post-metadata">

**Author:** ![Kem\_Tekinay](https://forum.xojo.com/user_avatar/forum.xojo.com/kem_tekinay/32/154_2.png) [@Kem\_Tekinay](https://forum.xojo.com/u/Kem_Tekinay)\
**Post date:** [May 14, 2024, 6:08pm UTC](https://forum.xojo.com/t/sql-copy-row-data/80305/10 "2024-05-14T18:08:17Z")

</div>

Something like this should do it:

```auto
INSERT INTO table_name (
  CasesListboxRow,
  CasesListBoxColumn,
  <etc>
) 
SELECT
  2,
  CasesListboxColumn,
  <etc>
FROM
  table_name
WHERE
  CasesListboxRow = 4;

```

(Not tested.)

---

<div class="post-metadata">

**Author:** ![Don\_Lyttle](https://forum.xojo.com/user_avatar/forum.xojo.com/don_lyttle/32/324_2.png) [@Don\_Lyttle](https://forum.xojo.com/u/Don_Lyttle)\
**Post date:** [May 14, 2024, 6:14pm UTC](https://forum.xojo.com/t/sql-copy-row-data/80305/11 "2024-05-14T18:14:15Z")

</div>

Wow! That’s incredibly close to one variation I tried (but as you and I both know, being “close” with a SQL statement won’t get you anywhere.)

Let me go give it a try now … I’ll report the result.

Thx, Kem … you’ve always been the “gift that keeps on giving” on this forum!

---

<div class="post-metadata">

**Author:** ![Kem\_Tekinay](https://forum.xojo.com/user_avatar/forum.xojo.com/kem_tekinay/32/154_2.png) [@Kem\_Tekinay](https://forum.xojo.com/u/Kem_Tekinay)\
**Post date:** [May 14, 2024, 6:15pm UTC](https://forum.xojo.com/t/sql-copy-row-data/80305/12 "2024-05-14T18:15:05Z")

</div>

You’re too kind.

Absolutely right, of course, but still…

---

<div class="post-metadata">

**Author:** ![Don\_Lyttle](https://forum.xojo.com/user_avatar/forum.xojo.com/don_lyttle/32/324_2.png) [@Don\_Lyttle](https://forum.xojo.com/u/Don_Lyttle)\
**Post date:** [May 14, 2024, 6:16pm UTC](https://forum.xojo.com/t/sql-copy-row-data/80305/13 "2024-05-14T18:16:22Z")

</div>

🤣 🤣 🤣 You’re too much!

---

<div class="post-metadata">

**Author:** ![Don\_Lyttle](https://forum.xojo.com/user_avatar/forum.xojo.com/don_lyttle/32/324_2.png) [@Don\_Lyttle](https://forum.xojo.com/u/Don_Lyttle)\
**Post date:** [May 14, 2024, 6:30pm UTC](https://forum.xojo.com/t/sql-copy-row-data/80305/14 "2024-05-14T18:30:09Z")

</div>

OK, here’s what I used for code (I don’t see the code blocks in the response box … what am I missing?) Unfortunately the code as shown below didn’t copy anything …

sql = "INSERT INTO CasesListboxContent (ProposalID, LineupID, CasesListBoxColumn,CellEditControl, CellDataSource) " + \_  
"SELECT 2, ProposalID, LineupID, CasesListBoxColumn,CellEditControl, CellDataSource " + \_  
"FROM CasesListboxContent " + \_  
“WHERE CasesListboxRow = 4”

---

<div class="post-metadata">

**Author:** ![Kem\_Tekinay](https://forum.xojo.com/user_avatar/forum.xojo.com/kem_tekinay/32/154_2.png) [@Kem\_Tekinay](https://forum.xojo.com/u/Kem_Tekinay)\
**Post date:** [May 14, 2024, 6:31pm UTC](https://forum.xojo.com/t/sql-copy-row-data/80305/15 "2024-05-14T18:31:57Z")

</div>

You missed `CasesListboxRow` as the first column in the INSERT.

---

<div class="post-metadata">

**Author:** ![Don\_Lyttle](https://forum.xojo.com/user_avatar/forum.xojo.com/don_lyttle/32/324_2.png) [@Don\_Lyttle](https://forum.xojo.com/u/Don_Lyttle)\
**Post date:** [May 14, 2024, 6:32pm UTC](https://forum.xojo.com/t/sql-copy-row-data/80305/16 "2024-05-14T18:32:37Z")

</div>

Also, I’m not quite sure about the “SELECT 2,” part ???

---

<div class="post-metadata">

**Author:** ![Don\_Lyttle](https://forum.xojo.com/user_avatar/forum.xojo.com/don_lyttle/32/324_2.png) [@Don\_Lyttle](https://forum.xojo.com/u/Don_Lyttle)\
**Post date:** [May 14, 2024, 6:38pm UTC](https://forum.xojo.com/t/sql-copy-row-data/80305/17 "2024-05-14T18:38:56Z")

</div>

Where’s the code blocks in the response box??? Am I blind?

---

<div class="post-metadata">

**Author:** ![Kem\_Tekinay](https://forum.xojo.com/user_avatar/forum.xojo.com/kem_tekinay/32/154_2.png) [@Kem\_Tekinay](https://forum.xojo.com/u/Kem_Tekinay)\
**Post date:** [May 14, 2024, 6:39pm UTC](https://forum.xojo.com/t/sql-copy-row-data/80305/18 "2024-05-14T18:39:17Z")

</div>

The columns in the SELECT must match the listed columns in the INSERT.

So

```auto
INSERT INTO table (
  col1,
  col2,
  col3
)
SELECT
  2, -- static value into col1
  col2,
  col3
FROM
  table
WHERE
  col1 = some_other_value

```

---

<div class="post-metadata">

**Author:** ![Kem\_Tekinay](https://forum.xojo.com/user_avatar/forum.xojo.com/kem_tekinay/32/154_2.png) [@Kem\_Tekinay](https://forum.xojo.com/u/Kem_Tekinay)\
**Post date:** [May 14, 2024, 6:39pm UTC](https://forum.xojo.com/t/sql-copy-row-data/80305/19 "2024-05-14T18:39:53Z")

</div>

Use three backticks on their own lines before and after the code.

---

<div class="post-metadata">

**Author:** ![Don\_Lyttle](https://forum.xojo.com/user_avatar/forum.xojo.com/don_lyttle/32/324_2.png) [@Don\_Lyttle](https://forum.xojo.com/u/Don_Lyttle)\
**Post date:** [May 14, 2024, 6:45pm UTC](https://forum.xojo.com/t/sql-copy-row-data/80305/20 "2024-05-14T18:45:20Z")

</div>

OK … tried this (putting CasesListboxRow in the Destination and Source) with no luck …

```auto
  sql = "INSERT INTO CasesListboxContent (ProposalID, LineupID, CasesListboxRow, CasesListBoxColumn,CellEditControl, CellDataSource) " + _
  "SELECT 2, ProposalID, LineupID, CasesListboxRow, CasesListBoxColumn,CellEditControl, CellDataSource " + _
  "FROM CasesListboxContent " + _
  "WHERE CasesListboxRow = 4"
```

---

<div class="post-metadata">

**Author:** ![AlbertoD](https://forum.xojo.com/letter_avatar_proxy/v4/letter/a/dbc845/32.png) [@AlbertoD](https://forum.xojo.com/u/AlbertoD)\
**Post date:** [May 14, 2024, 6:45pm UTC](https://forum.xojo.com/t/sql-copy-row-data/80305/21 "2024-05-14T18:45:52Z")

</div>

Not tested but this is what Kem is saying:

```auto
sql = "INSERT INTO CasesListboxContent (CasesListboxRow, ProposalID, LineupID, CasesListBoxColumn,CellEditControl, CellDataSource) " + _
"SELECT 2, ProposalID, LineupID, CasesListBoxColumn,CellEditControl, CellDataSource " + _
"FROM CasesListboxContent " + _
"WHERE CasesListboxRow = 4"

```

As CasesListboxRow is the first of the Insert and 2 is the first of Select, it will put 2 on the copied records that the select searches for 4. Hope this makes sense.

[Next page](https://forum.xojo.com/t/sql-copy-row-data/80305.md?page=2)
