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 2005 Forums
 Transact-SQL (2005)
 Updated from incremented Master Number Table

Author  Topic 

mhall
Starting Member

9 Posts

Posted - 2007-05-23 : 08:26:03
Hello,
I need to retrieve a group of checks to be printed. On request of the checks I want grab the next check number per bankaccount ID and update the check register table before returning the data to my APP.

declare @BankCheckNumbers table (BankID int, NextCheckNumber varchar(35))

insert @BankCheckNumbers
select 1, '40124' union all
select 2, '3211' union all
select 3, '3241' union all
select 4, '5211' union all
select 5, '8811'

declare @CheckRegister table (CheckID int, BankID int, CheckNumber varchar(35))

insert @CheckRegister
select 1, 1, NULL union all
select 2, 1, NULL union all
select 3, 2, NULL union all
select 4, 5, NULL union all
select 4, 5, NULL union all
select 4, 4, NULL union all
select 4, 3, NULL

Select * From @BankCheckNumbers
Select * From @CheckRegister

I have an SP that:

UPDATE BankCheckNumbers WITH(ROWLOCK)
SET @Value = NextCheckNumber = NextCheckNumber + 1
Where BankCheckNumbers.BankID= @BankAccountSysID

Return @NextNumber


Is there a way in SQL to update CheckRegister From BankCheckNumbers and still increment BankCheckNumbers for each value you pull with out using a cursor?

I know I can update it in my APP, but if I'm printing 1000 checks I would have to make 1000 calls to grab the 1000 check numbers and that bugs me.

Any suggestions or help would be much appreciated.

Cheers,
Mark

mhall
Starting Member

9 Posts

Posted - 2007-05-23 : 10:06:10
My colleague suggested that I grab the next ID in a Tran so as to lock views to the BankCheckNumbers, increment the number outside of the table, then update BankCheckNumbers with the last number incremented inside the transaction. My gut tells me that this is a bad idea, but given enough time I might be able to rationalize this over making 1000 requests from the app. Would that be crazy?

Thanks,
Mark
Go to Top of Page

rudesyle
Posting Yak Master

110 Posts

Posted - 2007-05-23 : 10:17:15
quote:
Originally posted by mhall

My colleague suggested that I grab the next ID in a Tran so as to lock views to the BankCheckNumbers, increment the number outside of the table, then update BankCheckNumbers with the last number incremented inside the transaction. My gut tells me that this is a bad idea, but given enough time I might be able to rationalize this over making 1000 requests from the app. Would that be crazy?

Thanks,
Mark



I'd stay away from a transaction where there's a select statement inside of it. Why not make the check number an identity column?

If not, do what you're going to do above, without the select within the transaction. Then I would put a unique key constraint on the column. If for some reason you had 2 queries grab the same number, one of them would error out when they did their insert (YOU WILL WANT TO DO THIS EITHER WAY!!!!!).
Go to Top of Page

mhall
Starting Member

9 Posts

Posted - 2007-05-23 : 10:33:57
Thanks for the suggestion. I can't do an identity column because I have multiple bank accounts with different check sequences and now that I think about it, I wouldn't be able to do what my colleague suggested either because I wouldn't just be grabbing one sequence run. In the case of the above table I would need a new check number from all 5 of the master accounts.
Damn, hate to do this at the client.

Cheers,
Mark
Go to Top of Page

rudesyle
Posting Yak Master

110 Posts

Posted - 2007-05-23 : 10:40:20
quote:
Originally posted by mhall

Thanks for the suggestion. I can't do an identity column because I have multiple bank accounts with different check sequences and now that I think about it, I wouldn't be able to do what my colleague suggested either because I wouldn't just be grabbing one sequence run. In the case of the above table I would need a new check number from all 5 of the master accounts.
Damn, hate to do this at the client.

Cheers,
Mark



Sounds like you need a user-defined function to grab the next check number then. I'd definitely put a unique constraint on that column.
Go to Top of Page

mhall
Starting Member

9 Posts

Posted - 2007-05-23 : 10:55:36
That would be awesome, but to my knowledge, you can't update inside of a user-defined function. When I tried to it threw this error

Invalid use of side-effecting or time-dependent operator in 'UPDATE' within a function.

If you know of a way to do that I would be much oblidged if you would show me how.

Cheers,
Mark
Go to Top of Page

mhall
Starting Member

9 Posts

Posted - 2007-05-23 : 11:13:12
Here is the code for the function that returns that error:

Create FUNCTION [dbo].[IW_BankGetNextCheckNumber]
(
@BankAccountSysID int
)
RETURNS varchar(40)
AS
Begin
DECLARE @Value Int

UPDATE dbo.IW_BankAccount WITH(ROWLOCK)
SET @Value = NextCheckNumber = NextCheckNumber + 1
Where dbo.IW_BankAccount.BankAccountSysID = @BankAccountSysID

Return cast(@Value as varchar(40))
End
Go to Top of Page

rudesyle
Posting Yak Master

110 Posts

Posted - 2007-05-23 : 11:14:17
quote:
Originally posted by mhall

That would be awesome, but to my knowledge, you can't update inside of a user-defined function. When I tried to it threw this error

Invalid use of side-effecting or time-dependent operator in 'UPDATE' within a function.

If you know of a way to do that I would be much oblidged if you would show me how.

Cheers,
Mark



Sorry ... I meant create a function to get the next available check number. You need to do the update inside the stored proc
Go to Top of Page

mhall
Starting Member

9 Posts

Posted - 2007-05-23 : 11:34:39
I don't follow you. Are you suggesting that I put a stored proc to increment the master table inside of the function? I heard that you could do that but it wasn't recomended. Is that warning overrated?

Cheers,
Mark
Go to Top of Page

mhall
Starting Member

9 Posts

Posted - 2007-05-24 : 11:22:36
I tried the SP inside of the function but that requires and extended SP and that seems like a bad idea. Anyone have any other suggestions?
Go to Top of Page

rudesyle
Posting Yak Master

110 Posts

Posted - 2007-05-24 : 11:53:57
(1)You have your stored proc.
(2)You have your cursor inside of your stored proc
(3)You call a function from within the cursor. The function returns the next available check number

All the function contains is a select statement to determine the next available check number. In all reality, you don't even need the function (I though it might be a nice place to encapsulate the code for obtaining the next available check number).
Go to Top of Page

mhall
Starting Member

9 Posts

Posted - 2007-05-24 : 15:36:52
I was trying not to put a SP into production that had a cursor in it. I guess I have to weigh the evil of a cursor in production against the client making 1000 calls to a function.

Thanks for the suggestions,
Mark
Go to Top of Page
   

- Advertisement -