0

Hey all so I have a database that has been up and running for a few months now. I had a request to add the users First and Last name as well as their organization.

This was easy enough to do for NEW users:

"INSERT INTO " +
    "aspnet_UsersAddition " +
        "(UserId, firstName, LastName, OrganizationID) " +
"VALUES " +
    "(" +
        "'" + genGUID + "'," +
        "'" + Params.firstName + "'," +
        "'" + Params.lastName + "'," +
        "" + Params.Params["Org"] +
    ")";

However, and this is the problem, is that OLD users who do not have that First/Last and organization associated with their account need to have those added to them.

I have tried the following:

INSERT INTO 
    aspnet_UsersAddition   
        (UserId, firstName, LastName, OrganizationID) 
VALUES 
    ('','','',1) 
WHERE 
    UserId = (SELECT UserId FROM aspnet_Membership WHERE Email = '[email protected]');

But of course the above would not work since the UserId in the aspnet_UsersAddition was non-excitant when comparing it to the aspnet_Membership UserId. The only info I have access to on the page where I am calling this function is their email address and user name.

So I guess my question is - How do I add a new record using the users GUID [that needs to be looked up via email address or user name] so that I can add that info [UserId] to the new table [first/last and organization]?

Note: The UserId is a Uniqueindentifier.

Do I have to make separate query's

Select old users GUID using their email address.

Insert new record into new table with blanks other than the GUID.

in order to do this? Or is there a way to do this in one query?

The only query I can come up with is a 2 parter:

DECLARE @userGUID VARCHAR(50)
SET @userGUID = (SELECT UserId FROM aspnet_Membership WHERE Email = '[email protected]')

INSERT INTO 
    aspnet_UsersAddition 
        (UserId, firstName, LastName, OrganizationID) 
VALUES 
    (Convert(UNIQUEIDENTIFIER, @userGUID), '', '', 1);
0

1 Answer 1

1

Sorry, I don't fully follow your question, if you could clarify it a little bit that would be helpful, regardless it sounds like you're looking for a query like this:

INSERT INTO aspnet_UsersAddition
     (UserId, firstName, LastName, OrganizationID) 
SELECT 
     UserId, '', '', 1
FROM aspnet_Membership 
WHERE Email = '[email protected]'

This will INSERT the UserId with blanks for the FirstName and LastName fields, and 1 for the OrganizationID field, into the new table from the old table, for the supplied email address.

2
  • 1
    Yep that works like I needed it to! Thanks for the help J.D.
    – StealthRT
    Commented Jun 3, 2021 at 20:23
  • @StealthRT Cool, np! SELECTing a dataset is another way you can insert into a table, instead of using the VALUES keyword.
    – J.D.
    Commented Jun 3, 2021 at 20:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.