Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 how to copy some data into same table?

Author  Topic 

allan8964
Posting Yak Master

249 Posts

Posted - 2013-09-11 : 17:58:54
Hi guys,

I know it's easy but just can't figure it out:
There are some data in a table with TypeID=23 and I need copy all rows with TypeID=23 to the same table, I mean append at the end, and make the copied rows TypeId=24 for some tests purpose. How can I do this? Thanks in advance.

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2013-09-11 : 18:26:43
do you mean something like:
INSERT
dbo.TableA
(
Col1,
Col2,
...
)
SELECT
Col1,
Col2,
...
FROM
dbo.TableA
WHERE
TypeID = 23
Go to Top of Page

allan8964
Posting Yak Master

249 Posts

Posted - 2013-09-11 : 21:49:26
Thanks Lamprey, this is what I need. But after that all the TypeId copied will be 23. So I changed little bit by

Select 'XXX', Col2 from ...

Then all copied rows have TypeId = 'XXX' then I just update that to 24.
I never knew insert can be used to the same table, thanks again.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-09-12 : 15:53:47
quote:
Originally posted by allan8964

Thanks Lamprey, this is what I need. But after that all the TypeId copied will be 23. So I changed little bit by

Select 'XXX', Col2 from ...

Then all copied rows have TypeId = 'XXX' then I just update that to 24.
I never knew insert can be used to the same table, thanks again.


no need of insert and update

you could do it in one step (insert alone)

like

INSERT
dbo.TableA
(
TypeID,
Col1,
Col2,
...
)
SELECT
24,
Col1,
Col2,
...
FROM
dbo.TableA
WHERE
TypeID = 23



------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -