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
 Transact-SQL (2000)
 Insert row from one table as fields in another table

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2007-03-05 : 10:45:11
Cezary writes "Hello:

The table below T1 is the table that I want to copy to another table T2. What I would like to do is to copy all records from T1 where NO=7 to EMAIL in T2, NO=49 to NAME in T2 and NO=58 to CODE in T2. Please keep in mind that ID=193 in T1 belongs to one person and therefore should be in one row. Please advise. Thank you in advance!


-----T1--------

ID-----NO-----VALUE
----------------------------------
193----7------sample@email.com
193----49-----db:Tom Sample
193----58-----12345
194----7------name@email.com
194----49-----db:LMrksa
194----58-----67891

------T2-------

ID-----NAME----CODE-----EMAIL
---------------------------------
Null---Null----Null-----Null"

snSQL
Master Smack Fu Yak Hacker

1837 Posts

Posted - 2007-03-05 : 14:28:43
[code]INSERT T2
SELECT a.ID,
(SELECT VALUE FROM T1 b WHERE b.ID = a.ID AND b.NO = 49) AS [NAME],
(SELECT VALUE FROM T1 c WHERE c.ID = a.ID AND c.NO = 58) AS [CODE],
(SELECT VALUE FROM T1 d WHERE d.ID = a.ID AND d.NO = 7) AS [EMAIL]
FROM T1 a[/code]
Go to Top of Page
   

- Advertisement -