Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

Troubleshooting
Research
Professional Opinions
Ask a Question
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

troubleshooting Question

Insert data from multiple select statements

Avatar of webressurs
webressursFlag for Norway asked on
SQL
3 Comments1 Solution256 ViewsLast Modified:
I have a stored procedure that shall insert some user data into a table. The stored procedure has only one input; @UserId. All userdata related to this UserId shall be selected from multiple tables in my solution, and inserted into another table.

How can this be done without making one SELECT statement for each value (like I do when getting the @UserName)? As you see  in the attached code I get 7 values from 2 different tables based on @UserId.
ALTER PROCEDURE [dbo].[spLogUser]
(
	@UserId varchar(64)
)
AS
BEGIN
    SET NOCOUNT ON;

    DECLARE @UserName varchar(256);
    SET @UserName = (SELECT UserName FROM aspnet_Users WHERE UserId =@userId);
	
    // I can get Firstname, Lastname, Address, Postalcode, Postalplace from tblUserProfileData like this:
    // SELECT Firstname, Lastname, Address, Postalcode, Postalplace FROM tblUserProfileData WHERE USerID = @UserId;

    // I can get  CreateDate, LastLoginDate from aspnet_Membership like this:
    // SELECT CreateDate, LastLoginDate FROM aspnet_Membership WHERE UserId = @UserId;

    // HOW TO INSERT THE SELECT VALUES ABOVE INTO THIS INSERT STATEMENT BELOW:

    INSERT INTO tblUserLog 
    (UserId, UserName, Firstname, Lastname, [Address], Postalcode, Postalplace, CreateDate, LastLoginDate, LastUpdatedDate) 
    VALUES 
    (@UserId, @UserName, Firstname, Lastname, [Address], Postalcode, Postalplace, CreateDate, LastLoginDate, LastUpdatedDate);


END
ASKER CERTIFIED SOLUTION
Avatar of CGmail
CGmail

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Commented:
This problem has been solved!
Unlock 1 Answer and 3 Comments.
See Answers