Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


SP for inserting max(eid)+1 no for all fields


SP for inserting max(eid)+1 no for all fields

Author
Message
ssurekha2000
ssurekha2000
SSC Journeyman
SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)

Group: General Forum Members
Points: 79 Visits: 207
tableA has eid field which stores the last eid value from tableB when records are added

in tableB records are saved from the application thr' stored procedure

stored procedure has insert statemnet for inserting records in tableB , while inserting the records i need the eid field value as below


AS
BEGIN
DECLARE @EID AS INT

BEGIN
set @EID=select max(eid) from tableA
set @EID=@EID+1
end


begin
insert statement
end



begin

set @EID=select max(eid) from tableB
update tableA set eid=@EID
end

suppose the max eid no in tableA is 10 .
5 records are inserted in tableB i need eid field in tableB as 11 for all 5 records &
eid field in tableA shld be set to 11

but currently i am getting eid as serial no .. as 11,12,12,14,15
Lynn Pettis
Lynn Pettis
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24179 Visits: 37948
Unfortunately you haven't provided enough information for anyone to try and help you. We really need to see the entire procedure plus DDL for the tables involved, sample data for the tables, sample data for the test update/insert to the tables, and the expected results when all is done based on the sample data.

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
ssurekha2000
ssurekha2000
SSC Journeyman
SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)

Group: General Forum Members
Points: 79 Visits: 207
ALTER PROCEDURE [dbo].[JOBRECINSERT]

@JOBID as bigInt,
@YEARMONTH as int,
@TRANSTYPE as char,
AS
BEGIN

DECLARE @ERNO AS INT
BEGIN
SET @ERNO=(SELECT EID FROM TABLEA where JOBID=@JOBID)
SET @ERNO=@ERNO+1 -- this no shld be genereated only once
END

BEGIN


INSERT INTO TABLEB(JOBID,YEARMONTH,TRANSTYPE,EID)
VALUES(@JOBID,@YEARMONTH,@TRANSTYPE,@ERNO)


END

begin
SET @ERNO=0
SET @ERNO=(SELECT MAX(EID) FROM TABLEB WHERE JOBID=@JOBID)

UPDATE TABLEA SET EID=@ERNO WHERE JOBID=@JOBID
end
Kingston Dhasian
Kingston Dhasian
Hall of Fame
Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)

Group: General Forum Members
Points: 3002 Visits: 5000
It seems you are calling the procedure 5 times for the 5 rows
You will have to change this approach and call the procedure only once and insert all the 5 rows at once.

Do you have any staging table where you are storing these 5 rows before inserting them into the main table?

Edit:Corrected spelling mistake.


Kingston Dhasian

How to post data/code on a forum to get the best help - Jeff Moden
http://www.sqlservercentral.com/articles/Best+Practices/61537/
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search