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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T15:18:42+00:00 2026-06-08T15:18:42+00:00

I have a stored procedure with a SELECT statement that outputs one row. It

  • 0

I have a stored procedure with a SELECT statement that outputs one row. It is something like this:

CREATE PROCEDURE [dbo].[CreateCustomer]
...

INSERT INTO Customers values(, , , ,)
SELECT CustomerID, FirstName, LastNam.....
INSERT Roles values(, , , .....

This selects the newly stored values back from the Customers table. The second insert uses the new CustomerID to insert a new row to the Roles table.

Is there a way to just get the customerID from the above select statement without querying again for the CustomerID?

I have tried to declare a variable and do it like this:

SELECT takenCustomerID = CustomerID, FirstName, LastNam....(rest of query statement)

But I have to declare all the variables and do it this way:

SELECT takenCustomerID, takenFirstName, takenLastNa... = CustomerID, FirstName, LastNam... (rest of query statement)

But, I think this is bad because it wastes lot of memory on the server side.

So, is there an easy way of getting individual values right away without declaring all the variables in the select statement, such as an inbuilt TEMP-like variable where I can call TEMP(“customerID”) and get that value?

Also, Can there be more than one SELECT statement in a stored procedure? How can we get the select values from the select statement we want?

I am asking more out of curiosity because I already know a way to get the value. I just want to know if there is a more elegant way.

  • 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-06-08T15:18:44+00:00Added an answer on June 8, 2026 at 3:18 pm

    Accessing Inserted Values

    It is simple. Use the OUTPUT clause to return the values you just created.

    DECLARE @Customers TABLE (
       CustomerID uniqueidentifier,
       FirstName varchar(100),
       LastName varchar(100)
    );
    
    INSERT dbo.Customers 
    OUTPUT Inserted.*
    INTO @Customers
    SELECT newsequentialid(), @FirstName, @LastName, ...;
    -- now you have all the values of the new row in the `@Customers` table variable.
    

    Then you can do this:

    INSERT dbo.Roles
    SELECT
       CustomerID,
       @OtherValue,
       @AnotherValue,
       AnyotherColumnFrom@Customers
    FROM @Customers;
    

    If you have no further use for the values from the inserted Customer row, you could even do away with the table variable and just OUTPUT directly into the Roles table, assuming that all the values that go into that come from variables.

    The OUTPUT clause gives access to the inserted values using the special meta-table Inserted. However, you can use variables and expressions with constants as well.

    The good thing about using this method is that you can handle many inserted rows at once. If you are inserting only one row, you need only the CustomerID afterward, and you need to use it more than once, then instead do it this way:

    DECLARE @CustomerID uniqueidentifier = newsequentialid();
    INSERT @Customer VALUES (@CustomerID, ...);
    INSERT @Roles VALUES (..., @CustomerID ...);
    

    By creating the GUID in advance, you don’t need to get it back out of the table. By the way, newsequentialid() is probably superior to newid() if it is the clustered index (which is probably true), because you will avoid the page splits that newid would cause.

    Returning Multiple Result Sets

    This is always possible. Whatever means you are using to query will have a method that advances your data access object to the next recordset. If you’re using a DataReader then look into the NextResult method. In Classic ADO, recordsets have a NextRecordset method.

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

Sidebar

Related Questions

I have a stored procedure that uses this select statement: SELECT dbo.VendorProgram.id, dbo.VendorProgram.CODE, dbo.Programs.ProgramName
I have a stored procedure that has this select statement in it: select (select
I have a stored procedure that returns multiple resultsets, it looks something like this
I have a stored procedure which basically does something like select top 1 expiryDate,
I have a stored procedure that has this line: SET @SQL = 'SELECT path,title,tags
I have a table that looks like this: CREATE TABLE [dbo].[SomeTable]( [Guid] [uniqueidentifier] NOT
We have a stored procedure that has a select statement in it: select convert(int,
I want to (loosely) have a stored procedure like select * from table where
I have a select statement that is used in a gridview via a stored
I have a stored procedure that returns all fields of an object. CREATE PROCEDURE

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.