Insert multiple values into table from IN

  • Hi. How would I go about inserting multiple values into a table based on the results of the IN as below:

    SELECT UserId

    FROM Users

    WHERE UserName IN ('Joes_Bloggs','John Doe' etc..........)

    GO

    --Inset values returned from previous query.

    INSERT INTO [db].[dbo].[ProfileUsers]

    ([Profile],

    [UserId],

    [CreatedDate],

    [CreatedUser],

    [LastModifiedUser],

    [LastModifiedDate])

    VALUES

    (1,

    WHATEVER THE USER NAMES MIGHT BE FROM IN,

    GETDATE(),

    'Sys',

    'Sys',

    GETDATE())

    GO

    Am I barking up the wrong tree?

    Kind Regards,

    Phil.

    -------------------------------------------------------------------------------------
    A neutron walks into a bar. "I'd like a beer" he says. The bartender promptly serves up a beer. "How much will that be?" asks the neutron. "For you?" replies the bartender, "no charge."

    Two hydrogen atoms walk into a bar. One says, 'I think I've lost an electron.' The other says 'Are you sure?' The first says, 'Yes, I'm positive... '

    Tommy Cooper

  • Don't think about a specific user. Do it set based:

    INSERT INTO [db].[dbo].[ProfileUsers]

    ([PROFILE],

    [UserId],

    [CreatedDate],

    [CreatedUser],

    [LastModifiedUser],

    [LastModifiedDate])

    SELECT

    1,

    UserId,

    GETDATE(),

    'Sys',

    'Sys',

    GETDATE()

    FROM Users

    WHERE UserName IN ('Joes_Bloggs','John Doe' etc..........)



    Lutz
    A pessimist is an optimist with experience.

    How to get fast answers to your question[/url]
    How to post performance related questions[/url]
    Links for Tally Table [/url] , Cross Tabs [/url] and Dynamic Cross Tabs [/url], Delimited Split Function[/url]

  • Lutz now that is what I call service.

    Many thanks,

    Phil.

    -------------------------------------------------------------------------------------
    A neutron walks into a bar. "I'd like a beer" he says. The bartender promptly serves up a beer. "How much will that be?" asks the neutron. "For you?" replies the bartender, "no charge."

    Two hydrogen atoms walk into a bar. One says, 'I think I've lost an electron.' The other says 'Are you sure?' The first says, 'Yes, I'm positive... '

    Tommy Cooper

  • You're welcome! 😀

    Going back to the picture you used:

    Sometimes it's faster to just cut the tree rather than just barking up... 😉



    Lutz
    A pessimist is an optimist with experience.

    How to get fast answers to your question[/url]
    How to post performance related questions[/url]
    Links for Tally Table [/url] , Cross Tabs [/url] and Dynamic Cross Tabs [/url], Delimited Split Function[/url]

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply