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.
| Author |
Topic |
|
lucsky8
Posting Yak Master
105 Posts |
Posted - 2008-05-20 : 09:51:30
|
| Hi, i need to create a stored procedure that will take data from one table and insert it into one tabe.I have 3 tablestblTest is the table that i need to take the informations from.intD1 to intD4 is a scoreI don't know if iexplain my sefl correctly tks in advance!tblTest 1 , 2 , 'A', 'B', 0 , 1 ,2 , 4i need thistblResultat1, 2, 'A','B'tblResultatXReponse1,1,0,02,1,1,13,1,2,24,1,4,3tblTestintTestId autonumberintCoursIdstrCodeAbsent1strCodeAbsent2intD1intD2intD3intD4tblResultatintResultatsId autonumberintCoursIdstrCodeAbsent1strCodeAbsent2tblResultatXReponseintResultatXReponseId autonumberintResultatsIdintReponseDescintOrdre |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2008-05-20 : 10:11:10
|
Still not sure what you want. Check out this topic for a sample of "how to" ask a question. Your question may end up in the "how not to" category How to ask a question on SQL Team and get a quick answer - i.e. the information you need to provide:http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=74221Be One with the OptimizerTG |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-05-20 : 10:15:27
|
| [code]DECLARE @Temp table(intResultatsId int,intCoursId int,strCodeAbsent1 varchar(10),strCodeAbsent2 varchar(10))INSERT INTO tblResultat (intCoursId,strCodeAbsent1,strCodeAbsent2)OUTPUT INSERTED.intResultatsId,INSERTED.intCoursId,INSERTED.strCodeAbsent1,INSERTED.strCodeAbsent2INTO @TempSELECT intCoursId,strCodeAbsent1,strCodeAbsent2FROM tblTestINSERT INTO tblResultatXReponse (intResultatsId,intReponseDesc,intOrdre)SELECT tmp.intResultatsId,t.ReponseDesc,t.OrderFROM (SELECT intCoursId,strCodeAbsent1,strCodeAbsent2,intD1 AS ReponseDesc,0 as Order FROM tblTest UNION ALL SELECT intCoursId,strCodeAbsent1,strCodeAbsent2,intD2,1 FROM tblTest UNION ALL SELECT intCoursId,strCodeAbsent1,strCodeAbsent2,intD3,2 FROM tblTest UNION ALL SELECT intCoursId,strCodeAbsent1,strCodeAbsent2,intD4,3 FROM tblTest)tINNER JOIN @Temp tmpON tmp.intCoursId = t.intCoursIdAND t.strCodeAbsent1=tmp.strCodeAbsent1AND t.strCodeAbsent2=tmp.strCodeAbsent2[/code] |
 |
|
|
lucsky8
Posting Yak Master
105 Posts |
Posted - 2008-05-20 : 10:16:57
|
Hi tks for your reponse i will try it!quote: Originally posted by visakh16
DECLARE @Temp table(intResultatsId int,intCoursId int,strCodeAbsent1 varchar(10),strCodeAbsent2 varchar(10))INSERT INTO tblResultat (intCoursId,strCodeAbsent1,strCodeAbsent2)OUTPUT INSERTED.intResultatsId,INSERTED.intCoursId,INSERTED.strCodeAbsent1,INSERTED.strCodeAbsent2INTO @TempSELECT intCoursId,strCodeAbsent1,strCodeAbsent2FROM tblTestINSERT INTO tblResultatXReponse (intResultatsId,intReponseDesc,intOrdre)SELECT tmp.intResultatsId,t.ReponseDesc,t.OrderFROM (SELECT intCoursId,strCodeAbsent1,strCodeAbsent2,intD1 AS ReponseDesc,0 as Order FROM tblTest UNION ALL SELECT intCoursId,strCodeAbsent1,strCodeAbsent2,intD2,1 FROM tblTest UNION ALL SELECT intCoursId,strCodeAbsent1,strCodeAbsent2,intD3,2 FROM tblTest UNION ALL SELECT intCoursId,strCodeAbsent1,strCodeAbsent2,intD4,3 FROM tblTest)tINNER JOIN @Temp tmpON tmp.intCoursId = t.intCoursIdAND t.strCodeAbsent1=tmp.strCodeAbsent1AND t.strCodeAbsent2=tmp.strCodeAbsent2
|
 |
|
|
lucsky8
Posting Yak Master
105 Posts |
Posted - 2008-05-20 : 13:56:14
|
| Thanks i had to change a few thing put it work perfectly!! Tks tks again save me a lot of trouble!Only one last question when you create a temp table, to you need to destroy it or?? |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-05-20 : 14:10:42
|
quote: Originally posted by lucsky8 Thanks i had to change a few thing put it work perfectly!! Tks tks again save me a lot of trouble!Only one last question when you create a temp table, to you need to destroy it or??
Nope. the temporary table (# table) is automatically destroyed upon ending the connection. But you may destroy it manually usingDROP TABLE #temponce you've done with it inorder to free resources early once after use.What i've used is a table variable @table. No need to destroy it explicitly too. It is cleaned up automatically upon end of query batch/procedure.More deatils here:-http://databases.aspfaq.com/database/should-i-use-a-temp-table-or-a-table-variable.html |
 |
|
|
|
|
|
|
|