Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / July 2007

Tip: Looking for answers? Try searching our database.

problem with scope_identity

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Dan - 08 Jul 2007 22:52 GMT
Hi,

after inserting a record in a table (sql server), i need the last value of
the primary key of that table, which increments automatically, before
inserting that value in another table.

I did like this:

comd.CommandText = "insert into [mytable] (field1, field2) values(@datbeg
,@datend')"
       comd.Parameters.Add("@datbeg", SqlDbType.DateTime).Value = tda
       comd.Parameters.Add("@datend", SqlDbType.DateTime).Value = tda2
       connection.Open()
comd.CommandText = "DECLARE @orderid    int"
       comd.CommandText = "SET @orderid = SCOPE_IDENTITY()"
       comd.CommandText = "select @orderid"
       Dim x As Integer
       x = Convert.ToInt32(comd.ExecuteScalar())

This giives an error:
"error: Must declare the scalar variable "@orderid". "

Thanks
Dan
Bob Johnson - 08 Jul 2007 23:53 GMT
Hi Dan,

Your code and approach have a number of problems. Please stick with me; I'm
not being critical... just pointing out the facts:

1. In every line where you have [comd.CommandText = ...] you are completely
changing the value of comd.CommandText. That is, each line *overwrites* the
previous value of CommandText. So, when you finally get around to
comd.ExecuteScalar(), the value of comd.CommandText is simply "select
@orderid".

2. The solution to the above problem (of overwriting the value of
CommandText in each successive line) is to concatenate the incremental
values, possibly via +=, and ensuring you add a blank space between each).
But you DON'T want to do that in your situation because of #3 below:

3. It appears that you are trying to create a stored procedure without
creating one (and instead putting all of the T-SQL in your CommandText.
There's no way that's going to work the way you are attempting.

What will work is to do the following:
1. Create a stored procedure that does the INSERT, followed immediately by
SET @orderid = SCOPE_IDENTITY, and returning exactly one result set via
SELECT @OrderID.

Then In your client code

2. Set CommandText = name of the stored procedure

3. Set ComandType = CommandType.StoredProcedure

4. Add to the Command.Parameters collection one SqlParameter object for each
of the parameters in the stored procedure.

5. Finally execute the stored procedure via the ExecuteScalar method (as you
were already trying to do).

The above assumes you have opened a connection etc..

-HTH

> Hi,
>
[quoted text clipped - 20 lines]
> Thanks
> Dan
Dan - 09 Jul 2007 00:25 GMT
Thanks, you're right of course with the concatenation.

But, instead of using a stored procedure (which i know is beter), would it
be posiible to do that in code-behind, more or less like this:
comd.CommandText = "DECLARE @orderid int," _
       & "SET @orderid = SCOPE_IDENTITY()," _
       & "select @orderid"
       Dim x As Integer
       x = Convert.ToInt32(comd.ExecuteScalar())

because i get the error:
Incorrect syntax near the keyword 'SET'.
Incorrect syntax near ',

Thanks again

> Hi Dan,
>
[quoted text clipped - 61 lines]
>> Thanks
>> Dan
Göran Andersson - 09 Jul 2007 00:37 GMT
> Thanks, you're right of course with the concatenation.
>
[quoted text clipped - 9 lines]
> Incorrect syntax near the keyword 'SET'.
> Incorrect syntax near ',

Use semicolon to separate the SQL statements, then it might work.

comd.CommandText = "DECLARE @orderid int;" _
       & "SET @orderid = SCOPE_IDENTITY();" _
       & "select @orderid"

But why not simply:

comd.CommandText = "select SCOPE_IDENTITY()"

Signature

Göran Andersson
_____
http://www.guffa.com

Bob Johnson - 09 Jul 2007 01:46 GMT
RE:
<< then it might work >>

Right - can you (op) please let us know if you get this to work? I'm
curious.
Bob Johnson - 09 Jul 2007 01:51 GMT
>> Thanks, you're right of course with the concatenation.
>>
[quoted text clipped - 19 lines]
>
> comd.CommandText = "select SCOPE_IDENTITY()"

A couple of other thoughts:
1. set comd.CommandType = CommandType.Text
2. In all those strings you are concatenating for the .CommandText value, be
sure to add white space where appropriate.
3. to test this, first get the script to work in query analyzer (SS2K) or
Management Studio (2005). Once it works there, then move it to your client
code.

-HTH
Dan - 09 Jul 2007 09:40 GMT
Yes, it works like this:

comd.CommandText = "insert into [mytable] (field1, field2) values(@datbeg
,@datend');" _
& " select SCOPE_IDENTITY()"

Thanks
Bob Johnson - 09 Jul 2007 14:27 GMT
How are you populating @datbeg and @datend? That query won't work unless you
send parameters. Is that your actual query?

Just curious. Thanks!

> Yes, it works like this:
>
[quoted text clipped - 3 lines]
>
> Thanks

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.