Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3603438
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T20:52:20+00:00 2026-05-18T20:52:20+00:00

I am newbie in SQL.I am creating a dummy project.In my project, I have

  • 0

I am newbie in SQL.I am creating a dummy project.In my project, I have one register page and I want to create a UserName(FirstName + LastName + UserID).But the problem is, I cant catch the UserID value.I have used both After Trigger and Instead Of Trigeer. Below is my script –

Table –

CREATE TABLE UserInfo
(
    UserID INT IDENTITY(1,1),
    FirstName NVARCHAR(500),
    LastName NVARCHAR(500),
    [Password] NVARCHAR(200),
    EmailID NVARCHAR(200),
    [Address] NVARCHAR(500),
    CountryID INT,
    StateID INT,
    UserName NVARCHAR(500)
)

Proc of Insert Data –

CREATE PROC Create_User    
 @FirstName NVARCHAR(500),    
 @LastName NVARCHAR(500),    
 @Password NVARCHAR(200),    
 @EmailID NVARCHAR(200),    
 @Address NVARCHAR(500),    
 @CountryID INT,    
 @StateID INT,    
 @UserID INT OUTPUT    
AS    
 BEGIN    
 INSERT INTO UserInfo (FirstName,LastName,[Password],EmailID,[Address],CountryID,StateID)    
 VALUES    
 (@FirstName,@LastName,@Password,@EmailID,@Address,@CountryID,@StateID)    

 SET @UserID = SCOPE_IDENTITY();    

 END 

After Trigger –

CREATE TRIGGER Create_UserName
ON UserInfo
AFTER INSERT
AS
BEGIN
DECLARE @_UName VARCHAR(200);
SET @_UName = (SELECT FirstName+LastName+Cast(UserID AS NVARCHAR(100)) from INSERTED )

INSERT INTO UserInfo (UserName) VALUES (@_UName)
END
    GO

Output – Inserted two rows

Instead Of Trigger –

CREATE TRIGGER Create_UserName
ON UserInfo
INSTEAD OF INSERT
AS
BEGIN
DECLARE @UName NVARCHAR(200);
DECLARE @FName NVARCHAR(200);
DECLARE @LName NVARCHAR(200);
DECLARE @UPassword NVARCHAR(200);
DECLARE @UEmailID NVARCHAR(200);
DECLARE @UAddress NVARCHAR(200);
DECLARE @UCountryID INT;
DECLARE @UStateID INT;
SET @UName = (SELECT FirstName+LastName+Cast(UserID AS NVARCHAR(100)) from INSERTED )
SET @FName = (SELECT FirstName from INSERTED )
SET @LName = (SELECT LastName from INSERTED )
SET @UPassword = (SELECT [Password] from INSERTED )
SET @UEmailID = (SELECT EmailID from INSERTED )
SET @UAddress = (SELECT [Address] from INSERTED )
SET @UCountryID = (SELECT CountryID from INSERTED )
SET @UStateID = (SELECT StateID from INSERTED )




INSERT INTO UserInfo (FirstName,LastName,[Password],EmailID,[Address],CountryID,StateID,UserName)  
VALUES (@FName,@LName,@UPassword,@UEmailID,@UAddress,@UCountryID,@UStateID,@UName)
END
    GO

Output – this works fine but Identity is 0.

Please tell me how I can do this?

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T20:52:21+00:00Added an answer on May 18, 2026 at 8:52 pm

    You have problems

    • Why do you have a stored proc and INSTEAD OF? Use either a stored proc OR an instead of trigger: not both
    • Second, the triggers are coded for one row only
    • The after trigger is inserting a new row, should be an update

    As to why:

    The stored proc has no INSERT to trap the IDENTITY value: the scope for the INSERT is actually the instead of trigger. If you switch to @@IDENTITY (bad practice) you’d get the IDENTITY value from the AFTER trigger.

    What to do:

    • Drop both of the triggers: they add no value

    • Either add a computed column to the table if UserName cannot be changed

    eg

    ALTER TABLE UserInfo ADD UserName AS FirstName+LastName+Cast(UserID AS NVARCHAR(100)
    
    • …or add an UPDATE to the stored proc UserName could be changed later

    eg

    CREATE PROC Create_User    
    ...
    AS    
    
    SET NOCOUNT, XACT_ABORT ON
    
    BEGIN TRY
     BEGIN TRAN
    
     INSERT INTO UserInfo
       (FirstName,LastName,[Password],EmailID,[Address],CountryID,StateID)    
     VALUES    
       (@FirstName,@LastName,@Password,@EmailID,@Address,@CountryID,@StateID)
    
     SET @UserID = SCOPE_IDENTITY();    
    
     UPDATE UserInfo
     SET UserName = @FirstName+@LastName+Cast@UserID AS NVARCHAR(100)
     WHERE UserID = @UserID
    
       COMMIT TRAN
    END TRY
    BEGIN CATCH
       IF XACT_STATE() <> 0 ROLLBACK TRAN
    END CATCH
    GO
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Am a Oracle PL/SQL newbie. Basically, I have a table created as follows: CREATE
I am newbie to SQL Joins. I have two tables Version Vid, VName, IsActive
I'm a newbie with SQL... Now I want to display some instances of AddrDistances
Dear All, please help me since I'm newbie in SQL Server. I have a
I'm brand new to SQL Server 2008, and have some newbie questions about the
At first - Im sql newbie, sorry for this (mbe typicall) question. I Have
I am a newbie to SQL. please help with this. I have the below
i'm a pl/sql newbie. now i have a question about oracle type . i
Be gentle, I'm a SQL newbie. I have a table named autonumber_settings like this:
I’m a newbie in Stored Procedures in SQL Server 2005. Let’s say we have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.