[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

return a value from an INSERT statement

Posted on 2000-05-03
13
Medium Priority
?
242 Views
Last Modified: 2008-02-01
I am trying to return the value of the item that was just inserted using a stored procedure.  I have 2 stored procedures, GET_ID, and NEW_ID.  When I call NEW_ID, I want it to create a new ID in the table (based on a formula) and return the value of the new id.  I cannot use identity as the value is not autogenerated, it is calculated. EX:

Create Procedure test
(
    @in varchar(50)
)
As
    declare @NewID varchar(50)
    set @NewID = CONVERT(varchar(50), NEWID())
   
    insert into table1 (newid)
    values (@NewID)

-- All I want is the value of @NewID returned after this executes.  Any help would be great.

0
Comment
Question by:adjennin
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
  • 2
  • +1
13 Comments
 
LVL 6

Expert Comment

by:crsankar
ID: 2772952
Here is an example

CREATE PROCEDURE titles_sum @@TITLE varchar(40) = '%', @@SUM money OUTPUT
AS
SELECT 'Title Name' = title
FROM titles
WHERE title LIKE @@TITLE
SELECT @@SUM = SUM(price)
FROM titles
WHERE title LIKE @@TITLE
GO

While using execute you will have to declare this parameter (@@SUM) as output.

0
 
LVL 2

Expert Comment

by:DawsonB
ID: 2772953
Create Procedure test
(
    @in varchar(50)
)
As
Set NoCount On
    declare @NewID varchar(50)
    set @NewID = CONVERT(varchar(50), NEWID())
     
    insert into table1 (newid)
    values (@NewID)

    Select  NewID
    From    table1
    Where   NewID = @NewID

This shoud see your pocedure return a recordset with one column, NewID, and it's value, the same value you have just put in to it. I noticed that you have a variable @in varchar(50) passed in to the stored procedure, but your code doesn't seem to use it, I assume you meant to use it with your Convert(varchar(50),NewID()) bit?

0
 
LVL 6

Expert Comment

by:crsankar
ID: 2772964
Sorry the last statement is required only if it is a local variable.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:adjennin
ID: 2773010
DawsonB... that was the first thing I tried.  When I execute the procedure, it does not return any records.  It completely ignores the SELECT statement.
0
 

Author Comment

by:adjennin
ID: 2773022
I even tried creating a simpe select procedure GETID and adding

EXEC sp_GETID @input

with the same results.  When I execute the GETID procedure, it works fine and returns records.  when I do the NEWID it does not return any records.
0
 
LVL 1

Expert Comment

by:noonie072398
ID: 2773115
Is this Microsoft SQL Server?

If so what version?

If it is Version 7 did you note the "Set NoCount On" in the example from DawsonB?
0
 

Author Comment

by:adjennin
ID: 2773140
It is MSDE. Which is shrunken SQL 7
0
 

Author Comment

by:adjennin
ID: 2773194
When I put in SET NOCOUNT ON, it returns empty recordsets. Without it, it returns no data.
0
 
LVL 1

Expert Comment

by:noonie072398
ID: 2773198
Not having used that particular product... Does it have the equivalent of Query analyser or can you execute sql in an interactive environment?

If so note the output:

In SQL Server 7 it is possible (when using certain odbc drivers) to have two values returned as record sets. The first is the values expected the second is the output "n rows affected".

If you don't set nocount on then your subsequent operations will be using the last record set returned and everything gets confused.

Happened to me several times before I found out.
0
 

Author Comment

by:adjennin
ID: 2773226
Actually, you can use Access 2000 to interface with MSDE and do all your work from there.  Since I am only trying to retrieve one value, can I just return the NEW_ID value that is generated instead of a recordset with a single piece of data in it?
0
 
LVL 2

Expert Comment

by:DawsonB
ID: 2773242
Ok, this is actually more efficient, but is this bit:
    set @NewID = CONVERT(varchar(50), NEWID())
 supposed to look something like
    set @NewID = CONVERT(varchar(50), @in())
???

As far as I can tell, the sp as it is written wont do anything with @in.
Your Convert is on NewID?




Create Procedure test
(
    @in varchar(50)
)
As
Set NoCount On
    declare @NewID varchar(50)
    set @NewID = CONVERT(varchar(50), @in())
     
    insert into table1 (newid)
    values (@NewID)

    Select  @NewID as NewID

Try that,
0
 
LVL 2

Accepted Solution

by:
DawsonB earned 160 total points
ID: 2773254
Sorry, I didn't explain

Select @NewID as NewID will return a recordset of one field named NewID, with the value of the Variable @NewID, and as there is no reading of a table, it is both more efficient, and more correct in this case.
The problem I think though is as I mentioned in my previous message regarding the convert of @in & newid.
0
 

Author Comment

by:adjennin
ID: 2773266
Works great!
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how the fundamental information of how to create a table.

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question