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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:16:36+00:00 2026-05-29T09:16:36+00:00

I have a User table, which is having Userid(primary key), Username, Password There is

  • 0

I have a User table, which is having Userid(primary key), Username, Password

There is a stored procedure which accepts list of users as XML. My requirement is to insert the users to User table, but before inserting I need to check whether the Username is already exists. If exists then a random number should be appended with username.

How do i check whether username already exists or not and randomise it, below is the sql which blindly inserts the username. Any help?

XML Structure:<ROOT><User UserId="0" Username="ajohn" Password="548788844" ></ROOT>

SQL:

INSERT INTO [User] (Username, Password) 
        SELECT tab.col.value('@Username','nVarchar(max)') AS Username,     tab.col.value('@Password','nVarchar(max)') AS Password,            
        FROM @pupilDetails.nodes('ROOT/User') tab(col)
  • 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-29T09:16:37+00:00Added an answer on May 29, 2026 at 9:16 am

    Maybe this will help:

    --Existing data
    DECLARE @User TABLE
    (
        UserId INT PRIMARY KEY CLUSTERED IDENTITY(1,1),
        Username nVarchar(MAX),
        Password nVarchar(MAX)
    )
    INSERT INTO @User(Username,Password)
    SELECT 'bob_456254','1584526'
    UNION ALL
    SELECT 'ajohn_4525846','4561584'
    --Something has to separate the username and the number I used "username"_"randomnumber"
    --The xml
    DECLARE @pupilDetails XML='<ROOT><User UserId="0" Username="ajohn" Password="548788844" ></User></ROOT>'
    
    ;WITH CTE AS
    (
        SELECT
            tab.col.value('@Username','nVarchar(max)') AS Username,
            tab.col.value('@Password','nVarchar(max)') AS Password
    
        FROM
            @pupilDetails.nodes('ROOT/User') AS tab(col)
    )
    INSERT INTO @User
    (
        Username,
        Password        
    )
    SELECT 
        CTE.Username+
        (
            CASE WHEN EXISTS
                    (
                        SELECT
                            NULL
                        FROM
                            @User AS Users
                        WHERE
                            SUBSTRING(Users.Username,0,CHARINDEX('_',Users.Username))=CTE.Username
                    )
                THEN '_'+CAST(ABS(CHECKSUM(NewId())) AS VARCHAR(MAX))
                ELSE ''
            END
        ) AS UserName,
        CTE.Password  
    FROM 
        CTE
    
    SELECT * FROM @User
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a User table which has a PrivilegeId foreign key points to a
I have a table User which has an identity column UserID , now what
I currently have a user's table which contains a one-to-one relationship for Youtube OAuth
I have a table which stores test results like this: user | score |
I have a simple table view which is editable. All I need the user
I have a table of data sorted by date, from which a user can
I have a user table in my mysql database that has a password column.
We have a user table, every user has an unique email and username. We
i have a Stored Procedure which returns output as a ref cursor. I would
I have a Car table and User table which store car and user particular.

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.