SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Output Parameter in Stored PRocedure


Output Parameter in Stored PRocedure

Author
Message
kapil_kk
kapil_kk
Hall of Fame
Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)

Group: General Forum Members
Points: 3103 Visits: 2766
Hi all, I have created a sp in which I want to extract last identity value after doing insertion....
But its giving me error that @new_identity parameter is not supplied

Create Procedure [dbo].[BS_StoreAllocation_AddSTNDetails]
@StoreCode int = 1,
@CourierName varchar(30) = 'BLUE Dart',
@CourierNo int = 98765732,
@new_identity int OUTPUT

AS
BEGIN

SET NOCOUNT ON;

DECLARE @STN varchar(20)

INSERT INTO GV_STNDetails
VALUES
(
REPLACE(STR(@StoreCode,4),' ','0'),
@CourierName,
@CourierNo,
GETDATE(),
CONVERT(VARCHAR,LEFT(@CourierName,3)) + '-' + CONVERT(VARCHAR,@CourierNo) + CONVERT(VARCHAR(10),GETDATE(),112)


)
SELECT @new_identity = SCOPE_IDENTITY()
RETURN

END

_______________________________________________________________
To get quick answer follow this link:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)

Group: General Forum Members
Points: 26313 Visits: 17553
kapil_kk (4/3/2013)
Hi all, I have created a sp in which I want to extract last identity value after doing insertion....
But its giving me error that @new_identity parameter is not supplied

Create Procedure [dbo].[BS_StoreAllocation_AddSTNDetails]
@StoreCode int = 1,
@CourierName varchar(30) = 'BLUE Dart',
@CourierNo int = 98765732,
@new_identity int OUTPUT

AS
BEGIN

SET NOCOUNT ON;

DECLARE @STN varchar(20)

INSERT INTO GV_STNDetails
VALUES
(
REPLACE(STR(@StoreCode,4),' ','0'),
@CourierName,
@CourierNo,
GETDATE(),
CONVERT(VARCHAR,LEFT(@CourierName,3)) + '-' + CONVERT(VARCHAR,@CourierNo) + CONVERT(VARCHAR(10),GETDATE(),112)


)
SELECT @new_identity = SCOPE_IDENTITY()
RETURN

END


Your code looks to be ok. Does the table have an identity column? How are you calling this?

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
devaji123
devaji123
Grasshopper
Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)

Group: General Forum Members
Points: 24 Visits: 289
Hi,

Use Select max(identity column name) from table name (data inserted table). In one of our scenario we used this it worked. Try this.

Vasu
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)

Group: General Forum Members
Points: 26313 Visits: 17553
devaji123 (4/3/2013)
Hi,

Use Select max(identity column name) from table name (data inserted table). In one of our scenario we used this it worked. Try this.

Vasu


This is not a good approach. You introduce concurrency issues like this. What happens when two or more connections are running the same code at the same time? Does one of them get the max value which now includes the insert from the second instance of it running? Using SCOPE_IDENTITY() is a better approach. Using OUTPUT is another option.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Lynn Pettis
Lynn Pettis
SSC-Dedicated
SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)

Group: General Forum Members
Points: 39806 Visits: 38563
kapil_kk (4/3/2013)
Hi all, I have created a sp in which I want to extract last identity value after doing insertion....
But its giving me error that @new_identity parameter is not supplied

Create Procedure [dbo].[BS_StoreAllocation_AddSTNDetails]
@StoreCode int = 1,
@CourierName varchar(30) = 'BLUE Dart',
@CourierNo int = 98765732,
@new_identity int OUTPUT

AS
BEGIN

SET NOCOUNT ON;

DECLARE @STN varchar(20)

INSERT INTO GV_STNDetails
VALUES
(
REPLACE(STR(@StoreCode,4),' ','0'),
@CourierName,
@CourierNo,
GETDATE(),
CONVERT(VARCHAR,LEFT(@CourierName,3)) + '-' + CONVERT(VARCHAR,@CourierNo) + CONVERT(VARCHAR(10),GETDATE(),112)


)
SELECT @new_identity = SCOPE_IDENTITY()
RETURN

END



Is this how you are invoking your procedure?



declare @NewValue int; -- will have the value returned by @new_identity

exec dbo.BS_StoreAllocation_AddSTNDetails
@StoreCode = <someinput>,
@CourierName = <someinput>,
@CourierNo = <someinput>,
@new_identity = @NewValue OUTPUT;




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)
kapil_kk
kapil_kk
Hall of Fame
Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)

Group: General Forum Members
Points: 3103 Visits: 2766
Lynn Pettis (4/3/2013)
kapil_kk (4/3/2013)
Hi all, I have created a sp in which I want to extract last identity value after doing insertion....
But its giving me error that @new_identity parameter is not supplied

Create Procedure [dbo].[BS_StoreAllocation_AddSTNDetails]
@StoreCode int = 1,
@CourierName varchar(30) = 'BLUE Dart',
@CourierNo int = 98765732,
@new_identity int OUTPUT

AS
BEGIN

SET NOCOUNT ON;

DECLARE @STN varchar(20)

INSERT INTO GV_STNDetails
VALUES
(
REPLACE(STR(@StoreCode,4),' ','0'),
@CourierName,
@CourierNo,
GETDATE(),
CONVERT(VARCHAR,LEFT(@CourierName,3)) + '-' + CONVERT(VARCHAR,@CourierNo) + CONVERT(VARCHAR(10),GETDATE(),112)


)
SELECT @new_identity = SCOPE_IDENTITY()
RETURN

END



Is this how you are invoking your procedure?



declare @NewValue int; -- will have the value returned by @new_identity

exec dbo.BS_StoreAllocation_AddSTNDetails
@StoreCode = <someinput>,
@CourierName = <someinput>,
@CourierNo = <someinput>,
@new_identity = @NewValue OUTPUT;




Thanks, Lyan
I was invoking the procedure in the wrong way

_______________________________________________________________
To get quick answer follow this link:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
kapil_kk
kapil_kk
Hall of Fame
Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)

Group: General Forum Members
Points: 3103 Visits: 2766
devaji123 (4/3/2013)
Hi,

Use Select max(identity column name) from table name (data inserted table). In one of our scenario we used this it worked. Try this.

Vasu

When we have inbuilt function that replaces your query then why we used that.....
Sean is correct about this....

_______________________________________________________________
To get quick answer follow this link:
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