Beginner stored proc issue

G

Guest

I need to call a sp that inserts a row and returns the id. I take it that I
have to use command vs. reader sqlcleint object to achieve this? How'd I
read the id back if the id is returded via a select at the end of the SP vs.
thru a o/p parameter? Thanks.
 
V

Vitor Alcântara Batista

I made that once like this:

Create Procedure sp_myproc
@name as varchar(100)
as
begin
insert into table (name) values (@name)
select @@identity --assuming that id is identity
end

In your .Net code, you declare a commandType of scalar a get the return of
the stored procedure after execute it.

I hope this helps you.
 
G

Guest

Hi,

How come when I tried to read the sqlCommand sCMD and found it to be -1
after exec and 0 before exec? Thanks.
 
B

Brendan Green

With the example given below, you'd need to return the identity via:

object o = sCMD.ExecuteScalar()

Is that what you're doing?
 
G

Guest

Thanks very much , Brendan and Victor.
--
bic


Brendan Green said:
With the example given below, you'd need to return the identity via:

object o = sCMD.ExecuteScalar()

Is that what you're doing?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top