Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
ann
Posting Yak Master
220 Posts |
Posted - 2009-05-11 : 11:08:06
|
| Hi,I am having problems returning the record ID. I normally do something like this:Create procedure spExampleInsert into .. blah blah blah...Select Scope_IdentityThen in my code: Object obj = command.executeScalar(); .. blah blah blahWhich works fine. However, I have this sp I am trying to get the ID from:@TargetID int, @rptDate datetime, @title varchar(64), @grpName varchar(64), @FileName varchar(100), @newFileName as varchar (100) = null ASDECLARE @ReportID as intBEGIN SET NOCOUNT ON; INSERT INTO tblReports (Date ,Title ,GroupReportName ,ReportType) VALUES (@rptDate, @title, @grpName, 'NonSym') SET @ReportID = @@IDENTITYENDBEGIN INSERT INTO tblSections ( TargetID, ReportID, ViewCount ) VALUES ( @TargetID, @ReportID , 0 )ENDBEGIN IF @TargetID > 0 AND NOT EXISTS (SELECT TargetID FROM tblTargets WHERE TargetID = @TargetID) INSERT INTO tblTargets (TargetID) VALUES (@TargetID)ENDBEGIN SET @newFileName = (convert(varchar(50), @reportID) + '_' + @FileName)ENDBEGIN UPDATE tblReports Set FileName = @newFileName Where ReportID = @ReportIDENDHow do I get back the ReportID? I tried using the Scope_Identity, but that didn't work ...Any help appreciated |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-05-11 : 11:10:58
|
| use a OUTPUT parameter and return through it. or just use RETURN @ReportID to return value |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
|
|
ann
Posting Yak Master
220 Posts |
Posted - 2009-05-11 : 11:13:10
|
| Do I do this after I've the SET @ReportID = @@IDENTITY?And in my code, do I just do a datareader and read it out or is it like executeScalar()?Sorry, I've never done an output param |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-05-11 : 11:22:58
|
quote: Originally posted by ann Do I do this after I've the SET @ReportID = @@IDENTITY?And in my code, do I just do a datareader and read it out or is it like executeScalar()?Sorry, I've never done an output param
you need to use SCOPE_IDENTITY instead of @@IDENTITY and then use RETURN after that to return value. or you could use OUTPUT parameter in procedure and just set its value to this. then in your application create an output parameter to receive this value. |
 |
|
|
ann
Posting Yak Master
220 Posts |
Posted - 2009-05-11 : 11:25:26
|
| Thanks - got it working. The link was a great help :) |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
|
|
|
|
|