| Author |
Topic |
|
chiragkhabaria
Master Smack Fu Yak Hacker
1907 Posts |
Posted - 2005-11-21 : 02:42:20
|
| Hi..!!I have 2 tables i want the query which returns me following records. Table 1 A B 1 A2 B3 CTable 2 A 123Result A B C1 A 42 B 53 C 6Column C is nothing but the maximum of the Col A of Table 2 and its incremented by 1 this can be done by cursor .. but i dont want to write cursor for this.. Any help .. Thanks.Sucess Comes to those who Believe in Beauty of their Dream.. |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
|
chiragkhabaria
Master Smack Fu Yak Hacker
1907 Posts |
Posted - 2005-11-21 : 03:35:48
|
well I got stuck over here.. Declare @ID int Select @ID = MAx(A) From TableBSelect A,B,IDENTITY(int,@ID,1) As PosID CInto #Temp From TableA Select * From #TempDrop Table #Temp This Code gives me error like this .. Incorrect syntax near '@ID'.Any clues.. Cant i specify any variable in Identity Function.. ???? Thanks.Sucess Comes to those who Believe in Beauty of their Dream.. |
 |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2005-11-21 : 04:43:22
|
no you can't.you can do this:use northwindgocreate table #temp (id int IDENTITY(1,1), employeeid int, customerid char(5))godeclare @start intset @start = 30DBCC CHECKIDENT(#Temp, RESEED, @start)goinsert Into #Temp (employeeid, customerid )Select top 10 employeeid, customerid From ordersgoselect * from #Tempgodrop table #temp Go with the flow & have fun! Else fight the flow |
 |
|
|
chiragkhabaria
Master Smack Fu Yak Hacker
1907 Posts |
Posted - 2005-11-21 : 05:31:16
|
| aha.. thanks a lot..I will check this out.. but for the time being i accomplised by dynamic sql.. Declare @ID int Select @ID = MAx(A) From TableBExec('Select A,B,IDENTITY(int,'+ @ID+ ',1) As PosID CInto #Temp From TableA ')Select * From #TempDrop Table #TempDo you Think any performance related issues in both ????Thanks.. !!Sucess Comes to those who Believe in Beauty of their Dream.. |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2005-11-21 : 06:55:42
|
| AfterExec('Select A,B,IDENTITY(int,'+ @ID+ ',1) As PosID CInto #Temp From TableA ')Are you able to get correct result when you this?Select * From #TempMadhivananFailing to plan is Planning to fail |
 |
|
|
chiragkhabaria
Master Smack Fu Yak Hacker
1907 Posts |
Posted - 2005-11-21 : 07:55:02
|
| opps i used global temp tables.. not the local temp tables..i used ##Temp.. and ya the result gave was perfect for me.. you can check out this code.. Create Table ##Tmp(A int,b Varchar(10))Create Table ##Tmp1(A int)Insert Into ##TmpSelect 1,'A' union AllSelect 2,'B' union AllSelect 3,'C' union AllSelect 4,'D'Insert Into ##Tmp1Select 1 union AllSelect 2 union AllSelect 3Declare @ID varchar(1) Select @ID =Max(A)+1 From ##Tmp1print @IDDeclare @Var Varchar(100)Set @Var = 'Select A,B, Identity(int,'+@ID+',1) As PosID Into ##Tmp3 From ##Tmp'print @varExec(@var)--Exec('Select A,B, Identity(int,' + @ID + '1) Into #Tmp3 From #Tmp')Select * From ##Tmp3Drop Table ##Tmp1Drop Table ##TmpDrop Table ##Tmp3This is just the test code..Sucess Comes to those who Believe in Beauty of their Dream.. |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2005-11-21 : 08:07:04
|
But It wont work if you use Local temp table as is your previous example MadhivananFailing to plan is Planning to fail |
 |
|
|
chiragkhabaria
Master Smack Fu Yak Hacker
1907 Posts |
Posted - 2005-11-21 : 08:12:36
|
quote: Originally posted by madhivanan But It wont work if you use Local temp table as is your previous example MadhivananFailing to plan is Planning to fail
yeah it didnt worked when i was trying out so i have to use global tables. Sucess Comes to those who Believe in Beauty of their Dream.. |
 |
|
|
|