Solved

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

Posted on 2008-06-13
2
1,533 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
ID: 21777792
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
ID: 31466888
mega thanks
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

This article describes some very basic things about SQL Server filegroups.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
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…

813 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

8 Experts available now in Live!

Get 1:1 Help Now