Solved

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

Posted on 2008-06-13
2
1,535 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

Free Webinar: AWS Backup & DR

Join our upcoming webinar with experts from AWS, CloudBerry Lab, and the Town of Edgartown IT to discuss best practices for simplifying online backup management and cutting costs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Foxpro errors 4 49
T-SQL Default value in Select? 5 51
A beginner asks: Which macos postgreSQL client? 5 84
SSRS: Why is Visual Studio stripping these properties? 2 39
These days, all we hear about hacktivists took down so and so websites and retrieved thousands of user’s data. One of the techniques to get unauthorized access to database is by performing SQL injection. This article is quite lengthy which gives bas…
This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
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…

679 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