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)
 Complex Query?

Author  Topic 

iloveorangesoda
Starting Member

30 Posts

Posted - 2004-08-30 : 09:53:36
I am trying to write a query that selects an entry code from 7 different columns but then has to execute another stored procedure for each individual code and input this to a table, I tried to use a cursor but I couldn't see how it could work as I am only ever going to have 1 row returned at a time.

Sorry if i am not clear

This is the steps I need to follow:

1. Select entry1, entry2 ......,entry7 etc.
From the roster table

2. I need to take entry1 and execute sproc rosterdata where will produce some hours and other code, which I will insert into another table rosterrecords,

3. I then need to do this for entry2, entry3 ..... etc.

I can get it too work but its doing it the long way which is not very efficient.

Can anyone give any ideas on how they would go about doing something like this?

Seventhnight
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2004-08-30 : 10:07:23
Can you give us some sample code to see what you are currently doing??

Beyond that, I don't really see why you are executing multiple procedures? I think that a little more detail would be the best way...

Corey
Go to Top of Page

iloveorangesoda
Starting Member

30 Posts

Posted - 2004-08-30 : 10:25:29
Hope this helps to make myself clearer, I am new to programming but from what I can tell is if I do it this way the code will be repeating over and over again, so I imagine this is not very efficient.

Thanks

CREATE  Procedure Mig_Roster
@f_number varchar(8),
@f_weekend datetime
AS

DECLARE @id varchar(6),
@entry1 varchar(36),
@entry2 varchar(36),
@entry3 varchar(36),
@entry4 varchar(36),
@entry5 varchar(36),
@entry6 varchar(36),
@entry7 varchar(36),
@weekend datetime,
@locationname char(40),
@payno varchar(8),
@fullname char(40),
@Atype varchar(2),
@Ahours decimal(7,2),
@Areason varchar(2),
@entryinserted bit,
@ReturnCode int,
@error int,
@values varchar(255)

BEGIN
SET @entryinserted = 0

--Get the rosterdatarecords for employee
SELECT @id = rdr.id,
@entry1 = rdr.e1,
@entry2 = rdr.e2,
@entry3 = rdr.e3,
@entry4 = rdr.e4,
@entry5 = rdr.e5,
@entry6 = rdr.e6,
@entry7 = rdr.e7,
@locationname = l.name,
@fullname = btas.fullname

FROM datarecord rdr
INNER JOIN rosrecord rr ON rdr.parent_id = rr.id
INNER JOIN btass btas ON rr.assign = btas.id
AND btas.pay = @f_number
INNER JOIN locationweek lw ON rr.parent_id = lw.id
AND (lw.weekend = @f_weekend)
INNER JOIN location l ON lw.parent_id = l.id

ORDER BY lw.weekend

--Execute convertroster sproc to convert rosterentry data and return absence type/reasons codes
BEGIN
EXEC @ReturnCode = Convertroster @entry = @entry1,
@shift = 1,
@Abtype = @Atype OUTPUT,
@Abhours = @Ahours OUTPUT,
@Abreason = @Areason OUTPUT

IF @ReturnCode != 0
--Write to exception table if error occurs
BEGIN
SET @entryinserted = 1
EXEC @ReturnCode = Exceptions @messtype = '01',
@messtype = 'Error - Entry1'

IF @ReturnCode != 0
--Write to error table if exceptions update fails
BEGIN
SET @values = 'Paynumber = ' + @f_number + ' Error Number is ' + CONVERT(varchar, @error)
EXEC Mig_WriteLog @f_error = 'Creating exceptions table',
@f_procedure = 'Roster',
@f_values = @values
RETURN -9
END
END
ELSE
IF @entryinserted = 0
--Insert employee roster details into roster table if no errors
BEGIN
INSERT INTO Tester.dbo.roster
(staffname,
paynumber,
location,
weekending,
type,
hours,
reason,
rosterentrydate)

VALUES
(@fullname,
@f_number,
@locationname,
@f_weekend,
@Atype,
@Ahours,
@Areason,
DATEADD(Day,-6,@f_weekend))

END
BEGIN

EXEC @ReturnCode = MIG_Convertroster @entry = @entry2,
@shift = 1,
@Abtype = @Atype OUTPUT,
@Abhours = @Ahours OUTPUT,
@ABS_reason = @Areason OUTPUT

IF @ReturnCode != 0
BEGIN
SET @entryinserted = 1
EXEC @ReturnCode = Exceptions @messtype = '02',
@messtype = 'Error - Entry2'

IF @ReturnCode != 0
BEGIN
SET @values = 'Paynumber = ' + @f_number + ' Error Number is ' + CONVERT(varchar, @error)
EXEC Mig_WriteLog @f_error = 'Creating exceptions table',
@f_procedure = 'Roster',
@f_values = @values
RETURN -9
END
END
END

IF @entryinserted = 0
BEGIN
INSERT INTO Tester.dbo.roster
(staffname,
paynumber,
location,
weekending,
type,
hours,
reason,
rosterentrydate)

VALUES
(@staffname,
@f_paynumber,
@locationname,
@f_weekending,
@type,
@hours,
@reason,
DATEADD(Day,-1,@f_weekend))

END
END

END


Go to Top of Page

Seventhnight
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2004-08-30 : 11:03:08
I think the executing of the SP with the @entryN variable could be done like

quote:


--Execute convertroster sproc to convert rosterentry data and return absence type/reasons codes
BEGIN
Declare @entryNum int
Set @entryNum = 1

While @entryNum<8
Begin
if (@entryNum=1) Set @entry = @entry1
if (@entryNum=2) Set @entry = @entry2
if (@entryNum=3) Set @entry = @entry3
if (@entryNum=4) Set @entry = @entry4
if (@entryNum=5) Set @entry = @entry5
if (@entryNum=6) Set @entry = @entry6
if (@entryNum=7) Set @entry = @entry7

EXEC @ReturnCode = Convertroster @entry,
@shift = 1,
@Abtype = @Atype OUTPUT,
@Abhours = @Ahours OUTPUT,
@Abreason = @Areason OUTPUT

IF @ReturnCode != 0
--Write to exception table if error occurs
BEGIN
SET @entryinserted = 1
EXEC @ReturnCode = Exceptions @messtype = '01',
@messtype = 'Error - Entry1'

IF @ReturnCode != 0
--Write to error table if exceptions update fails
BEGIN
SET @values = 'Paynumber = ' + @f_number + ' Error Number is ' + CONVERT(varchar, @error)
EXEC Mig_WriteLog @f_error = 'Creating exceptions table',
@f_procedure = 'Roster',
@f_values = @values
RETURN -9
END
END
ELSE
IF @entryinserted = 0
--Insert employee roster details into roster table if no errors
BEGIN
INSERT INTO Tester.dbo.roster
(staffname,
paynumber,
location,
weekending,
type,
hours,
reason,
rosterentrydate)

VALUES
(@fullname,
@f_number,
@locationname,
@f_weekend,
@Atype,
@Ahours,
@Areason,
DATEADD(Day,-6,@f_weekend))

END

Set @entryNum = @entryNum + 1
End
END




Corey
Go to Top of Page

iloveorangesoda
Starting Member

30 Posts

Posted - 2004-08-30 : 11:08:08
Great thanks for your help
Go to Top of Page

Seventhnight
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2004-08-30 : 11:10:37
Oh... and who doesn't love soda

Corey
Go to Top of Page
   

- Advertisement -