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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Assign Count to Variable

Status
Not open for further replies.

Eddiemonster

Programmer
Apr 24, 2002
1
US
I am creating a trigger in SQL to update a table under certain circumstances. The table I am updating has a record ID number. I want to count the number of records in the table and assign that count to a variable so that I can then later add one to get the next record ID. How do I do this?
 
declare @myCount int
set @myCount = 0
SELECT @TotalCost = COUNT(*) from MyTable

....
set @myCount = @myCount + 1

---------------------
bperry
 
EddieMonster, what if someone deletes a record? The count of records would not match the max id. I'd modify the code to this:

declare @myCount int
set @myCount = 0
SELECT @myCount = MAX(ID) from MyTable

....
set @myCount = @myCount + 1
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top