Solved

Need an UPDATE statement that update's one row of a table based on another row in that same table.

Posted on 2013-01-30
3
417 Views
Last Modified: 2013-01-30
I've been given this table with over 70 fields in it (don't ask)..  Anyway, I've added a new row to it for testing purposes which is full of test data (It's like a template-row I guess you'd call it).  Now I'd like to use that row to spin-off copies of, into the same table (while just changing the primaryID).  This will allow me to test and test with fresh new test rows without having to create new ones manually....Uhhhhg.  Currently, I've got the fresh template-row done, and have entered a new row with a new ID (and the rest of the fields are NULL).  How can I update this null-row with data from the clean template-row?

I was hoping to do something like this:
UPDATE Table t1 SET t1.col1, t1.col2,...t1.col70
SELECT t2.col1, t2.col2,...t2.col70 FROM Table t2 WHERE id = myTemplateRowID
WHERE t1.id = myNullRowID

Open in new window

I thought I saw this sort of Update statement somewhere once (syntax isn't correct I know).
0
Comment
Question by:David L. Hansen
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 500 total points
ID: 38837100
Yep, you're pretty close:


UPDATE t1
SET t1.col1 = t2.col1, t1.col2 = t2.col2, ...t1.col70 = ...
FROM dbo.table t1
INNER JOIN dbo.table t2 ON
    t2.id = myTemplateRowID AND
    t1.id = myNullRowID
0
 
LVL 15

Author Comment

by:David L. Hansen
ID: 38837208
Perfect...thank you.
0
 
LVL 15

Author Closing Comment

by:David L. Hansen
ID: 38837209
sweet!
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question