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

  • Home
  • SEARCH
  • 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 862599
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T09:10:09+00:00 2026-05-15T09:10:09+00:00

I have complaint table 1. tblProfile with columns userId | name | age |

  • 0

I have complaint table
1. tblProfile with columns
userId | name | age | address | mobileno |
2. tblUserId with columns userId | role | status

now when user fills the form I want to insert one row in tblProfile, before inserting a new row I want to create userId by combining starting letters of name and mobile no and then insert into tblprofile with userId after this I want to insert that UserId into tblUserId table.
for this I have to use two triggers one is before insert trigger and another is after insert trigger.but I dont know how to capture user information to create userId and how to pass that Id to second trigger.

  • 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-15T09:10:09+00:00Added an answer on May 15, 2026 at 9:10 am

    Since you’re acting upon INSERTs, try #inserted or inserted as the container of your inserting values.

    CREATE TRIGGER...
        insert into tblProfile (userId, name, age, address, mobileno) (
            select N'do-your-concat-here...', name, age, address, mobileno
                from inserted
        )
    

    I have not worked with triggers for a long time now, but this should help you get what you need.

    Please have an eye out this link: Using the inserted and deleted Tables.

    However, you already have all the tools on the application-side as you have the information and the capability to work with the in-memory information data.

    EDIT #1

    how to pass this Id to second trigger?

    In this particular situation, I see more suitable to process with a stored procedure than two independant triggers. We have interdependent information data here (userId). I do think the simplest way would be a stored procedure. It is adviseable to wrap these operation within a transaction scope, as if either insert fails, both won’t be applied, assuring data integrity.

    CREATE PROCEDURE prcInsProfileUserId
        -- Assuming data types. Replace with proper data types as needed.
        @name nvarchar(50) NOT NULL 
        , @age int NOT NULL
        , @address nvarchar(150) NOT NULL
        , @mobileno bigint NOT NULL
        , @role nvarchar(10) NOT NULL
        , @status int NOT NULL
    AS BEGIN TRANSACTION
        DECLARE @UserId nvarchar(10)
        SET @UserId = N'do-your-concat-here...';
    
        -- We then have the userId value, so we may insert into both tables accordingly.
        insert into tblProfile (userId, name, age, mobileno) 
            values (@userId, @name, @age, @address, @mobileno)
    
        insert into tblUserId (userId, role, status)
            values (@userId, @role, @status)
    
        COMMIT
    END
    

    However, if you do prefer to go with triggers, then the alternative would be to insert the concatenated value for userId into a temporary table. You then should have a DDL looking as follows:

    DECLARE @UserIdTempTable TABLE (
        userId nvarchar(10) NOT NULL
    )
    

    Then, within the first trigger, you would have to set the value of a @userId variable to contain you concatenated userId, then use it to insert into tblProfile, then perform a second insert into @UserIdTempTable.

    CREATE TRIGGER...
        DECLARE @userId nvarchar(10)
        SET @userId = N'do-your-concat-here...'
    
        insert into tblProfile (userId, name, age, address, mobileno) (
            select @userId, name, age, address, mobileno
                from inserted
        )
    
        IF @@ROWCOUNT > 0
        BEGIN
            delete from @UserIdTempTable -- assuring there is no mistake possible while populating and retrieving the userId
            insert into @UserIdTempTable (userId)
                values (@userId)
        END
    END
    

    Then, you may select it from your second trigger.

    CREATE TRIGGER second...
        insert into tblUserId (userId, role, status) (
            select tmp.userId, i.role i.status
                from @UserIdTempTable tmp
                    , inserted i
        )
    

    Be careful here though, because no data integrity is absolutely preserved as the first insert may have been processed successfully, but the second not. To preserve data integrity, you would have to verify whether @@ROWCOUNT is greater than 0, unless you would delete the record with this actual userId from tblProfile.

    This is abosolute hard hand-work. Processing through triggers is not adviseable here, because within the insertion of tblProfile, you do not have information data required by tblUserId, so you have to have two DbCommand and launch two ExecuteNonQuery() in a row. That is a lot of overhead for such a tiny task. Then, it would be more aviseable to process with the stored procedure as suggested, plus it assures data integrity by the DBMS itself, instead of simulating it through a @@ROWCOUNT verification.

    Disclaimer: This code is provided as-is and is not guaranteed to compile without you to adapt it to fit your situation. I wrote it off the top of my head with no verification.

    I do hope this helps! =)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a database (NexusDB (supposedly SQL-92 compliant)) which contains and Item table, a
I have created two tables ORDERS and ORDERITEMS with the following constraint: alter table
I need to update some timestamp columns in a table in a Postgres (8.3)
I have a fairly standards compliant XHTML+CSS site that looks great on all browsers
I have a requirement to make a large amount of code MISRA compliant. First
I have been using Netbeans 6.5 recently - it complains (on startup, and if
We have a 3D viewer that uses OpenGL, but our clients sometimes complain about
To be truly standards-compliant, must all functions in C (except for main) have a
I am looking for url encoding tips for SEO compliant site. I have a
Have just started using Google Chrome , and noticed in parts of our site,

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.