SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 how to copy some data into same table?
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

allan8964
Posting Yak Master

247 Posts

Posted - 09/11/2013 :  17:58:54  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

4361 Posts

Posted - 09/11/2013 :  18:26:43  Show Profile  Reply with Quote
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

247 Posts

Posted - 09/11/2013 :  21:49:26  Show Profile  Reply with Quote
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

India
52249 Posts

Posted - 09/12/2013 :  15:53:47  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000