April 27, 2007 at 12:56 pm
Guys,
I have a table case_num_seq
select * from case_num_seq
seq_cd currval incr
____________________________
accs 1 1
I have stored procedure which call increments and currval column and spits out the next value
CREATE PROCEDURE [dbo].[SEQVAL] @tblname sysname AS
DECLARE @sql1 nvarchar(4000)
SELECT @sql1 = ' UPDATE CASE_NUM_SEQ' +
' SET CURRVALUE = CURRVALUE+INCR WHERE SEQ_CD = '+quotename(@tblname)
EXEC sp_executesql @sql1
DECLARE @sql nvarchar(4000)
SELECT @sql = ' SELECT CURRVALUE' +
' FROM CASE_NUM_SEQ WHERE SEQ_CD = ' + quotename(@tblname)
EXEC sp_executesql @sql
____________________
When I execute this stored procedure I get the following error
EXEC SEQVAL 'ACCS'
"Msg 207, Level 16, State 1, Line 1
Invalid column name 'ACCS'.
Msg 207, Level 16, State 1, Line 1
Invalid column name 'ACCS'."
Any suggestions and inputs would help
Thanks
April 27, 2007 at 1:05 pm
I'm not sure why you're doing the dynamic SQL. What about:
CREATE PROCEDURE [dbo].[SEQVAL] @tblname varchar(100) AS
Begin tran
UPDATE CASE_NUM_SEQ SET CURRVALUE = CURRVALUE + INCR WHERE SEQ_CD = @tblname
SELECT CURRVALUE FROM CASE_NUM_SEQ WHERE SEQ_CD = @tblname
commit
@tablename should be declared as the same type as seq_cd. Wrap the thing in a commit to keep anyone from doing another update before your 'Select'
April 27, 2007 at 1:07 pm
April 27, 2007 at 3:30 pm
CREATE PROCEDURE [dbo].[SEQVAL] @tblname varchar(100) AS
Begin tran
UPDATE CASE_NUM_SEQ SET CURRVALUE = CURRVALUE + INCR WHERE SEQ_CD = @tblname
SELECT CURRVALUE FROM CASE_NUM_SEQ WHERE SEQ_CD = @tblname
commit
We had a similar procedure at work (created by a 3rd party)... it made an average of 640 deadlocks per day with occasional spikes to 4000 a day. And, it's not gonna matter what you do to it so long as you have an UPDATE/SELECT in the transaction. I can tell you how to fix it but....
My first question would be "Why in the heck aren't you using an IDENTITY column on the tables to do this???" What do you think this is? ORACLE?
--Jeff Moden
Change is inevitable... Change for the better is not.
April 27, 2007 at 4:49 pm
Jeff's right. There are goofy things you have to do to limit conflicts with this scenario. Artificially long rows to force each entry onto it's own page, no indexes, and some other stuff I can't recall now since I haven't done this since I worked on Sybase in the last century.
Go with the IDENTITY, as suggested, it it's at all a possibility.
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply