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 122661
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T04:09:50+00:00 2026-05-11T04:09:50+00:00

We have a user table, every user has an unique email and username. We

  • 0

We have a user table, every user has an unique email and username. We try to do this within our code but we want to be sure users are never inserted (or updated) in the database with the same username of email. I’ve added a BEFORE INSERT Trigger which prevents the insertion of duplicate users.

CREATE TRIGGER [dbo].[BeforeUpdateUser]    ON  [dbo].[Users]    INSTEAD OF INSERT AS  BEGIN     -- SET NOCOUNT ON added to prevent extra result sets from     -- interfering with SELECT statements.     SET NOCOUNT ON;      DECLARE @Email nvarchar(MAX)     DECLARE @UserName nvarchar(MAX)     DECLARE @UserId int     DECLARE @DoInsert bit      SET @DoInsert = 1      SELECT @Email = Email, @UserName = UserName FROM INSERTED      SELECT @UserId = UserId FROM Users WHERE Email = @Email      IF (@UserId IS NOT NULL)         BEGIN             SET @DoInsert = 0         END      SELECT @UserId = UserId FROM Users WHERE UserName = @UserName      IF (@UserId IS NOT NULL)         BEGIN             SET @DoInsert = 0         END      IF (@DoInsert = 1)         BEGIN             INSERT INTO Users             SELECT                             FirstName,                             LastName,                             Email,                             Password,                             UserName,                             LanguageId,                            Data,                             IsDeleted                         FROM INSERTED         END     ELSE         BEGIN             DECLARE @ErrorMessage nvarchar(MAX)             SET @ErrorMessage =                           'The username and emailadress of a user must be unique!'             RAISERROR 50001 @ErrorMessage         END  END 

But for the Update trigger I have no Idea how to do this. I’ve found this example with google: http://www.devarticles.com/c/a/SQL-Server/Using-Triggers-In-MS-SQL-Server/2/ But I don’t know if it applies when you update multiple columns at once.

EDIT:

I’ve tried to add a unique constraint on these columns but it doesn’t work:

Msg 1919, Level 16, State 1, Line 1 Column 'Email' in table 'Users' is of a type  that is invalid for use as a key column in an index. 
  • 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. 2026-05-11T04:09:51+00:00Added an answer on May 11, 2026 at 4:09 am

    You can add a unique contraint on the table, this will raise an error if you try and insert or update and create duplicates

    ALTER TABLE [Users] ADD  CONSTRAINT [IX_UniqueUserEmail] UNIQUE NONCLUSTERED  (     [Email] ASC )  ALTER TABLE [Users] ADD  CONSTRAINT [IX_UniqueUserName] UNIQUE NONCLUSTERED  (     [UserName] ASC ) 

    EDIT: Ok, i’ve just read your comments to another post and seen that you’re using NVARCHAR(MAX) as your data type. Is there a reason why you might want more than 4000 characters for an email address or username? This is where your problem lies. If you reduce this to NVARCHAR(250) or thereabouts then you can use a unique index.

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

Sidebar

Ask A Question

Stats

  • Questions 71k
  • Answers 71k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Your $DJANGO_SETTINGS_MODULE should just be set to ch3.settings. Just make… May 11, 2026 at 1:17 pm
  • added an answer I have a sneaking suspicion it's something along the lines… May 11, 2026 at 1:17 pm
  • added an answer If you really must use LINQ (as a learning exercise,… May 11, 2026 at 1:17 pm

Related Questions

No related questions found

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.