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
 SQL Server 2000 Forums
 SQL Server Development (2000)
 how to all fields of a row into new record by just changing one or two key fileds

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2002-02-18 : 21:12:07
Yadwinder writes "if we have a table with say 32 fields. and we just want to insert another record into that table by changing two key field and all other field of an existing record whose key field is known to us."

Spyder
SQLTeam Author

75 Posts

Posted - 2002-02-19 : 04:23:28
Not sure I completely understand the wording of the question but if I have you right, you want to insert a new row with non-key values that are the same as those of an existing row? If so you could do this with an INSERT SELECT statement -- here's a small example that you should be able to expand to your situation:

CREATE TABLE mytable (key1 INT, key2 INT, col1 CHAR(1), col2 CHAR(2))

INSERT mytable VALUES (1,2,'a','b')
INSERT mytable VALUES (2,3,'b','c')
INSERT mytable VALUES (3,4,'c','d')

--The four lines below are all one INSERT SELECT statement:
INSERT mytable (key1, key2, col1, col2)
SELECT 4, 5, col1, col2
FROM mytable
WHERE key1 = 1 AND key2 = 2

key1 key2 col1 col2
----------- ----------- ---- ----
1 2 a b
2 3 b c
3 4 c d
---------------------------------
4 5 a b

Go to Top of Page
   

- Advertisement -