Question : Calculated variable on server...

Hi there.

I need to create a calculated value in a field on my MS SQL database. Right now I'm calculating the value on the client, but I want it to happen on the server. Here's what I do now:

I have an input field like this:
<input type="text" name="chainlink" value="<%=year(date())&"-"&(Recordset13.Fields.Item("").Value)+1%>" size="32" />

It's supposed to create a number on new records like "year-####"... year slash and up to 4 numbers. It's selecting the present year and look up in recordset13 the highest value for the present year.  But this way the number is calculated when the user starts creating a new record, and while the user is doing that, another user may do the same, and untill the number has been saved, many users can create the same value. That is NOT good.
So how can I create this value when the user is activating the INSERT INTO statement in the SQL codes?
When the user clicks "insert new record" the SQL server must run the recordset13 to find the maximum value of the "year-####" and save into the variable in the database. I just got a lot of syntax erros when I tried doing some codes in the SQL....
I bet you guys have a cool suggestion? :-)  Remember I use MS SQL and classic asp.

Best regards

Ullenulle

Answer : Calculated variable on server...

Create procedure which does the following pass @id if needed in SP.:

declare @retValue int --considering below value is int
SELECT @retValue  = IsNull(MAX(SUBSTRING(chainlink,6,6)),0) FROM dbo.tbl_registrer WHERE SUBSTRING(chainlink,1,4) LIKE DATEPART(year, getdate())
SET @retValue = @retValue  + 1
update your_table set field1 = 'year-' + cast(@retValue as varchar(4)) where id = @id
Random Solutions  
 
programming4us programming4us