August 22, 2007 at 2:16 pm
How would I select a row in a table, change its key value and insert it into the same table as a new row?
August 22, 2007 at 2:49 pm
you mean
Insert...Select?
Examples below
--TestTable
Create table #Mytable (pk int identity,
Foo varchar(25),
Bar varchar(25))
--Insert TestData
insert into #Mytable (Foo, Bar)
Values('One', 'Mississippi')
insert into #Mytable (Foo, Bar)
Values('Two', 'Mississippi')
insert into #Mytable (Foo, Bar)
Values('Three', 'Mississippi')
-- Can Modify several ways.
insert into #Mytable (Foo, Bar)
select Foo, Bar
From #Mytable
where pk = 2
select *
from #Mytable
Results
1 One Mississippi
2 Two Mississippi
3 Three Mississippi
4 Two Mississippi
insert into #mytable (Foo, Bar)
select 'Four', 'Mississippi'
From #Mytable
where pk = 3
1 One Mississippi
2 Two Mississippi
3 Three Mississippi
4 Two Mississippi
5 Four Mississippi
BOL
August 22, 2007 at 2:50 pm
insert table1 (col1, col, col3)
select 40000 + col1, col2 / 2.0, col3
from table1
where col4 < 33123
N 56°04'39.16"
E 12°55'05.25"
August 22, 2007 at 5:29 pm
Peter's example is *almost* what I need: I have a part master table where the part id is the PK, plus many columns. I created a "Generic Part" row which I would like to be able to copy to a new row when I need to create a new part (to save me the trouble of having to set a lot of default values each time for many of the columns), but
1) Changing the part id, or PK, to a different id, say "Part 123"
2) Not having to specify all the other columns separately, since there are so many of them
August 23, 2007 at 8:27 am
A few comments:
insert <table>
select <new primary key here>,* from <default table>
I find this a bit ugly, since the minute anything changes in either table this will fall apart, but here's how you would do it if you want to go that way. in order to not specify field names, you have to make sure that the select returns the same number of fields, in the same order, so just pay some attention to that when building the default table structure.
----------------------------------------------------------------------------------
Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?
August 23, 2007 at 11:22 am
A few additional comments:
Tomm Carr
--
Version Normal Form -- http://groups.google.com/group/vrdbms
August 23, 2007 at 12:30 pm
Hey, PDM is the name of the game,here: I got started with computers in the late seventies when, as a new accountant, I swore to myself that there was no way in hell I was going to do all this repetitive, mind-numbing work manually for the rest of my career...
Anyway, thanks to all for your input, it will be put to good use.
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply