|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
What I need to do is re-populate a unique number into multiple fields,
Let me explain, An employee can appear in the first table only once but can be in the second table multiple times with multiple employee numbers .There is a field called TFN that is unique and we are using it to create a unique id called KRid so what I have done is created 2 tables namely TEST_TBL and TEST2_TBL . In TEST_TBL I am populating a KRid with a unique no being produced by the TFN field only once i.e 12345 being the resulting unique id number. If an employee has 2 employee numbers i.e empno 1 and empno 1000,only employee no 1 will have the unique KRid created but nothing for 1000 because the record already exists , so what has me stumped is that the TFN for employee empno 1 and the TFN for empno 1000 are the same. How do I get the KRid (12345 from empno 1) to populate empno 1000 in TEST2_TBL , The second table has all records in so I can group the second table by TFN id but how do I populate employee 1000 in the second table with the KRid 12345. Please help!!!!! Below are how the tables are set up and an example of the result. TABLE 1 if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[TEST_TBL]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table [dbo].[TEST_TBL] GO CREATE TABLE [dbo].[TEST_TBL] ( [Empl_Num] [char] (32) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [Surname] [char] (32) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [First_Name] [char] (32) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [Mid_Name] [char] (32) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [Hours_Day] [numeric](18, 2) NULL , [Hours_Wk] [numeric](18, 2) NULL , [KR_ID] [bigint] IDENTITY (1, 1) NOT NULL , [TFN] [char] (32) COLLATE Latin1_General_CI_AS NULL , [Date_Term] [datetime] NULL , [Empl_Type] [int] NULL , [Cost_Centre] [char] (32) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [Empl_Status] [int] NULL ) ON [PRIMARY] GO TABLE 2 if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[TEST2_TBL]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table [dbo].[TEST2_TBL] GO CREATE TABLE [dbo].[TEST2_TBL] ( [EmpNumber] [char] (32) COLLATE Latin1_General_CI_AS NULL , [TFN] [char] (32) COLLATE Latin1_General_CI_AS NULL , [KR_ID] [char] (10) COLLATE Latin1_General_CI_AS NULL , [EmpStatus] [int] NULL , [EmpType] [int] NULL , [CommonName] [char] (32) COLLATE Latin1_General_CI_AS NULL ) ON [PRIMARY] GO Query goes as follows for table 1: SELECT NPE000.EmpNumber, NPET00.RecordStatus, NPE000.KR_ID, NPE000.Surname, NPE000.FirstName, NPE000.SecondName, NPE000.Class, NPE000.DateEmployed, NPE000.DateOfBirth, NPE000.HoursPerDay, NPE000.HoursPerWeek, NPE000.PassportNo, NPE000.AwardCode, NPE000.EmailPayslipTo, NPE000.Location, NPE000.Grade, NPE000.DateTerminated, NPE000.EmploymentType, NPE000.DistCode, NPE000.EmpStatus, NPET00.TaxRefNo FROM NPE000 NPE000, NPET00 NPET00 WHERE NPET00.RecordStatus = 0 and NPET00.TaxRefNo <> ' 111111111' and NPET00.TaxRefNo <> ' 000000000' AND LENGTH(NPET00.TaxRefNo) >= 9 AND LENGTH(NPE000.KR_ID) >= 0 AND NPE000.EmpNumber = NPET00.EmpNumber Query goes as follows for table 2: SELECT NPE000.EmpNumber, NPE000.FirstName, NPE000.Surname, NPE000.Class, NPE000.Location, NPE000.EmploymentType, NPE000.EmpStatus, NPET00.TaxRefNo, NPE000.Paypoint, NPE000.KR_ID, FROM NPE000, NPET00 WHERE Recordstatus = 0 and (EmploymentType = 1 AND EmpStatus = 1 AND NPE000.EmpNumber = NPET00.EmpNumber From this you can see that in table 1 it will only create 1 KR_ID for only one employee number but in table 2 I am bringing through all employee records. In table 2 I can group by NPET00.TaxRefNo which will bring all NPET00.TaxRefNo's togeather. From that I would like to populate the other employee numbers with the unique KR_ID. Example:Table 1 000001,Jackson,James,Sam,7.6,38,12345,475431212 Example:Table 2 000001,Jackson,James,Sam,7.6,38,12345,475431212 001000,Jackson,James,Sam,7.6,38,,475431212 I hope this helps Thanks in advance |
|
#2
|
||||
|
||||
|
big kev needs to give more explicit examples, because that explanation is really confusing
|
|
#3
|
|||
|
|||
|
Quote:
Yes an example of the tables you have and the results you are trying to achieve. |
|
#4
|
|||
|
|||
|
Quote:
TABLE 1 if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[TEST_TBL]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table [dbo].[TEST_TBL] GO CREATE TABLE [dbo].[TEST_TBL] ( [Empl_Num] [char] (32) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [Surname] [char] (32) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [First_Name] [char] (32) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [Mid_Name] [char] (32) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [Hours_Day] [numeric](18, 2) NULL , [Hours_Wk] [numeric](18, 2) NULL , [KR_ID] [bigint] IDENTITY (1, 1) NOT NULL , [TFN] [char] (32) COLLATE Latin1_General_CI_AS NULL , [Date_Term] [datetime] NULL , [Empl_Type] [int] NULL , [Cost_Centre] [char] (32) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [Empl_Status] [int] NULL ) ON [PRIMARY] GO TABLE 2 if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[TEST2_TBL]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table [dbo].[TEST2_TBL] GO CREATE TABLE [dbo].[TEST2_TBL] ( [EmpNumber] [char] (32) COLLATE Latin1_General_CI_AS NULL , [TFN] [char] (32) COLLATE Latin1_General_CI_AS NULL , [KR_ID] [char] (10) COLLATE Latin1_General_CI_AS NULL , [EmpStatus] [int] NULL , [EmpType] [int] NULL , [CommonName] [char] (32) COLLATE Latin1_General_CI_AS NULL ) ON [PRIMARY] GO Query goes as follows for table 1: SELECT NPE000.EmpNumber, NPET00.RecordStatus, NPE000.KR_ID, NPE000.Surname, NPE000.FirstName, NPE000.SecondName, NPE000.Class, NPE000.DateEmployed, NPE000.DateOfBirth, NPE000.HoursPerDay, NPE000.HoursPerWeek, NPE000.PassportNo, NPE000.AwardCode, NPE000.EmailPayslipTo, NPE000.Location, NPE000.Grade, NPE000.DateTerminated, NPE000.EmploymentType, NPE000.DistCode, NPE000.EmpStatus, NPET00.TaxRefNo FROM NPE000 NPE000, NPET00 NPET00 WHERE NPET00.RecordStatus = 0 and NPET00.TaxRefNo <> ' 111111111' and NPET00.TaxRefNo <> ' 000000000' AND LENGTH(NPET00.TaxRefNo) >= 9 AND LENGTH(NPE000.KR_ID) >= 0 AND NPE000.EmpNumber = NPET00.EmpNumber Query goes as follows for table 2: SELECT NPE000.EmpNumber, NPE000.FirstName, NPE000.Surname, NPE000.Class, NPE000.Location, NPE000.EmploymentType, NPE000.EmpStatus, NPET00.TaxRefNo, NPE000.Paypoint, NPE000.KR_ID, FROM NPE000, NPET00 WHERE Recordstatus = 0 and (EmploymentType = 1 AND EmpStatus = 1 AND NPE000.EmpNumber = NPET00.EmpNumber From this you can see that in table 1 it will only create 1 KR_ID for only one employee number but in table 2 I am bringing through all employee records. In table 2 I can group by NPET00.TaxRefNo which will bring all NPET00.TaxRefNo's togeather. From that I would like to populate the other employee numbers with the unique KR_ID. Example:Table 1 000001,Jackson,James,Sam,7.6,38,12345,475431212 Example:Table 2 000001,Jackson,James,Sam,7.6,38,12345,475431212 001000,Jackson,James,Sam,7.6,38,,475431212 I hope this helps Thanks in advance |
|
#5
|
||||
|
||||
|
Quote:
Quote:
|
|
#6
|
|||
|
|||
|
Employee can have multiple employee numbers, example employee number 1 Joe Soap is a permanent part time employee for one company, employee number 1000 Joe Soap is the same employee but is a casual employee for another company so Joe Soap has 2 employee numbers. Joe has a TaxRefno that is 123456789(consistent)but this number is to long so we are using a unique id being created by the table([KR_ID] [bigint] IDENTITY (1, 1) NOT NULL ,).We only want to see one record in the first table for Joe Soap so when the table is populated it will give Joe Soap employee number 1 a unique id but when it gets to Joe Soap employee number 1000 it will not allocate anything because it has already allocated a number to Joe Soap employee number 1 and so will not insert employee number 1000 into table 1. It is using the TaxRefno as a reference.
In table 2 we are bringing all the records through so Joe Soap will come up twice, once as employee number 1 and then as employee number 1000.When this table is populated employee number 1 will have a unique id attached to the record but employee number 1000 will not so what I want to do is insert the unique id number that appears in employee number 1 into employee number 1000. I hope this makes more sense Thanks |
|
#7
|
||||
|
||||
|
Quote:
Code:
update table2
set KR_ID
= ( select KR_ID
from table2
where EmpNumber = 1 )
where EmpNumber = 1000
which one of the three records involved here got added in which order? who's on first? |
|
#8
|
|||
|
|||
|
Quote:
Employee number 1 is first , what I am doing is pumping data out of a relational data base into a sql table 1 and getting a unique id from that. I then want to get that unique number into all the employee numbers that have a common TaxRefNo. I will not be able no map as you have done all the employee numbers for all of the records(an employee can have up to 6 employee numbers and that will mean me changing the script everytime a new employee is taken on.I want to do something like , go and get all taxrefno that are the same then look at the id and populate that id into the other employee numbers |
|
#9
|
||||
|
||||
|
so you load all the rows into table 1 first, and then start loading them into table 2? how do you match employee numbers?
or you load one row into table 1 first, and then all the rows that match that employee into table 2, in sort of a nested loop? i know what you want to end up with, but i still have no idea what you are doing to get there |
|
#10
|
|||
|
|||
|
Quote:
I first of all extract them out of the relational database into table 1, I then get the unique id out of table1 and re-populate the relational database with the unique id.I then do another extract out of the relational database to populate table 2 with all the details, it is at this stage that I want to marry up the id's to all the same employee records. |
|
#11
|
||||
|
||||
|
sounds to me like you need an application program script
i mean, in order to "get the unique id out of table1 and re-populate the relational database with the unique id" you pretty well must have one already good luck, i don't think i can help you any further |
![]() |
| Viewing: Dev Shed Forums > Databases > MS SQL Development > re-populate a unique number into multiple fields |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|