Solved

sql insert query and getting / setting last insert id using stored procedure

Posted on 2008-06-13
2
1,532 Views
Last Modified: 2012-06-21
hi folks im my query was running fine until i tried to use the last insert id to insert into another table.  here is my sql

ALTER PROCEDURE [dbo].[spregistration]
      @title Varchar(25),
      @firstName Varchar(50),
      @lastName Varchar(50),
      @emailAddress Varchar(150),
      @password Varchar(50),
      @institution Varchar(300),
      @nickname Varchar(100),
      @autherised int,
      @userid int = Null

AS
      INSERT INTO users (title,firstName,lastName,emailAddress,password,institution,nickname,autherised) VALUES
      (@title,@firstName,@lastName,@emailAddress,@password,@institution,@nickname,@autherised)
      SET @userid = SELECT SCOPE_IDENTITY()
      INSERT INTO userPriv_pivot (user_id,privelige_level)(@userid,5)

im sure its really simple, it did work fine until i added the lines

@userid int = Null

and

SET @userid = SELECT SCOPE_IDENTITY()
INSERT INTO userPriv_pivot (user_id,privelige_level)(@userid,5)

thanks in advance
0
Comment
Question by:andrew67
2 Comments
 
LVL 43

Accepted Solution

by:
TimCottee earned 50 total points
Comment Utility
Hello andrew67,

It should be:

Set @userid = Scope_Identity()
INSERT INTO userPriv_pivot (user_id,privelige_level) Values(@userid,5)

There is no need for the "SELECT" there and you were also missing the VALUES keyword on the subsequent insert statement.

Regards,

TimCottee
0
 
LVL 3

Author Closing Comment

by:andrew67
Comment Utility
mega thanks
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now