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
 General SQL Server Forums
 New to SQL Server Programming
 Autogenerating Number.

Author  Topic 

chiragkhabaria
Master Smack Fu Yak Hacker

1907 Posts

Posted - 2006-07-08 : 08:27:20
Hi all,

-- Table 1

Declare @TblFin Table
(
Empid int
)

-- Inserting the Data in the Table 1
Insert @TblFin
Select 1 Union All
Select 2 Union All
Select 3 Union All
Select 4 Union All
Select 5 Union All
Select 6 Union All
Select 7 Union All
Select 8 Union All
Select 9

-- Table 2
Declare @TblBgt Table
(
EmpID int,
PosID Int
)

--Inserting the records in the Table 2

Insert @TblBgt
Select 1,1 Union All
Select 2,2 Union All
Select 3,3 Union All
Select 4,4 Union All
Select -1,5 Union All
Select -1,6 Union All
Select -1,7

-- Now here i want insert all the missing empids in the my second table
i.e. @TblBgt and PosID will be MAx(PosID)+1

So The Expected output when i do
Select * from @TblBgt

1 1
2 2
3 3
4 4
-1 5
-1 6
-1 7
5 8
6 9
7 10
8 11
9 12



I know this that i can do this using the Temp Tables but i dont want
to use the Temp Table, is there any way to directly bluk insert the records without using Temp Tables

And I cant make the PosID Column an Indentity since, the value can come from some differents ways also..


Thanks

Chirag

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2006-07-08 : 09:03:26
[code]insert into @TblBgt
select EmpID, (select max(PosID) from @TblBgt) +
(select count(*) from @TblFin x
where not exists (select * from @TblBgt y where y.EmpID = x.EmpID)
and x.EmpID <= f.EmpID)

from @TblFin f
where not exists (select * from @TblBgt x where x.EmpID = f.EmpID)[/code]


KH

Go to Top of Page

chiragkhabaria
Master Smack Fu Yak Hacker

1907 Posts

Posted - 2006-07-09 : 02:39:10
aha.. Thanks Tan

Chirag
Go to Top of Page
   

- Advertisement -