Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2008-06-13
2
Medium Priority
?
1,539 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 200 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

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

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

Recently I was talking with Tim Sharp, one of my colleagues from our Technical Account Manager team about MongoDB’s scalability. While doing some quick training with some of the Percona team, Tim brought something to my attention...
In this article, I’ll look at how you can use a backup to start a secondary instance for MongoDB.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

688 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