Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Inserting records while incrementing a value

Status
Not open for further replies.

MrBillSC

Programmer
Aug 6, 2001
592
US
I am trying to insert records into a history table. One of the columns in the table must be set to a specific value in the first record inserted and then incremented by 1 for each new record inserted. The column is not an identity column. In the code below I have commented out the "insert" statement because I want to view my results before running the "insert". @newval is the variable I am trying to increment for each new record. My Code so far:

Use GoldTest
Go
Update esxgenid
set val=val where module = 'hr' and keyname = 'eshshstd'
declare @maxval int
select @maxval = val from esxgenid where module = 'hr' and keyname = 'eshshstd'
declare @newval Int
set @newval = @maxval + 1

While
(Select sal_group from esypostr) = 'service' and
(Select step from esyapptr) ='G' and
(Select active_date from esygradr) = '07/01/2006' and
(Select step from esygradd) = 'H'

Begin
select esyemplr.emp_id,esyapptr.appoint_id,@newval,'07/01/2006',esypostr.pos_no,esyapptr.hourly_rate,
esygradd.hourly_rate,'MOU Change-New Step A','03/01/2006',esyapptr.grade_code,esyapptr.step,
esyapptr.grade_code,esygradd.step

/*insert into eshshstd (shst_emp_id,shst_appoint_id,shst_id,shst_chg_date,shst_pos_no,
shst_old_hrly_rate,shst_hourly_rate,shst_description,shst_entered_date,
shst_old_grade,shst_old_step,shst_grade_code,shst_step)*/

from esyapptr inner join esypostr on esyapptr.pos_id=esypostr.pos_id
inner join esygradr on esyapptr.grade_code= esygradr.grade_code
inner join esyemplr on esyapptr.emp_id = esyemplr.emp_id
inner join esygradd on esygradr.grade_id = esygradd.grade_id

where esypostr.sal_group = 'service' and
esyapptr.step ='G' and
esygradr.active_date = '07/01/2006' and
esygradd.step = 'H'

set @newval = @newval + 1

End

When I execute the above code I get the following error:
(2 row(s) affected)

Server: Msg 512, Level 16, State 1, Line 12
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.

I must be approaching the from the wrong angle.

Can anyone help me with this?

Thanks,
MrBill
 
Thank you for your reply SQLDenis.

Yes. I want it to return as many values as meet the select criteria. I am trying to insert a new history record for each value returned in the 'select' statements. How would I go about doing that? I must be taking the wrong approach.

MrBill
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top