Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

SP for inserting max(eid)+1 no for all fields Expand / Collapse
Author
Message
Posted Thursday, June 6, 2013 11:10 PM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Tuesday, May 6, 2014 5:04 AM
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
Post #1460954
Posted Friday, June 7, 2013 12:02 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Yesterday @ 10:01 PM
Points: 22,980, Visits: 31,456
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.



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)
Post #1460967
Posted Friday, June 7, 2013 12:31 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Tuesday, May 6, 2014 5:04 AM
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
Post #1460972
Posted Friday, June 7, 2013 2:46 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Wednesday, July 16, 2014 8:33 AM
Points: 2,616, Visits: 4,720
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/
Post #1461003
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse