SQL Stored Procedure - Returned Data

Hitman
Soldato
Joined
25 Feb 2004
Posts
2,837
Hi,

Got a quick problem! I've created a stored procedure that takes the input, fetches more data then does an INSERT statement:

Code:
CREATE PROCEDURE Duplicate
    @Account nvarchar(20)
AS
    DECLARE @Email nvarchar(50), @Hint nvarchar(50)

    SET @Email = (SELECT Email FROM Users WHERE Account=@Account)
    SET @Hint = (SELECT Hint FROM Users WHERE Account=@Account)
    
    INSERT INTO Users(Account, Email, Hint) VALUES('[1]'+@Account, @Email, @Hint)

As you can see, this will do two SELECTs within this procedure.

Is there a way to combine these two SELECTs into one, whilst still letting me use the returned values as variables?

This is a very basic example of what I'm needing. The actual one does eight SELECTs!
 
Last edited:
You can go one better...

Code:
CREATE PROCEDURE Duplicate
    @Account nvarchar(20)
AS

INSERT INTO Users(Account, Email, Hint) 
    SELECT '[1] '+ @Account, CurrentUser.Email, CurrentUser.Hint
        FROM Users CurrentUser
        WHERE CurrentUser.Account = @Account
 
You can go one better...

Code:
CREATE PROCEDURE Duplicate
    @Account nvarchar(20)
AS

INSERT INTO Users(Account, Email, Hint) 
    SELECT '[1] '+ @Account, CurrentUser.Email, CurrentUser.Hint
        FROM Users CurrentUser
        WHERE CurrentUser.Account = @Account

aye, i was gonna say this
 
Back
Top Bottom