Solved

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

Posted on 2008-06-13
2
1,536 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Many companies are looking to get out of the datacenter business and to services like Microsoft Azure to provide Infrastructure as a Service (IaaS) solutions for legacy client server workloads, rather than continuing to make capital investments in h…
Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
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…

752 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