0

I want to insert data into table using both select statement and executing procedures.

That is... suppose a table 'TEMP' has 4 columns A,B,C,D. A & B column values will be coming from SELECT statement and C, D column values will be come by executing any stored procedures.

I have tried following but didn't facing error.

INSERT INTO TEMP
SELECT R1.A, R1.B, R2.C, R2.D FROM 
(SELECT A , B FROM SOME_TABLE) AS R1
CROSS JOIN EXEC [dbo].[SOME_PROCEDURE] @SOME_VARIABLE1 ) AS R2

ERROR IS:

Incorrect syntax near the keyword 'exec'
2
  • @Aquillo Sry. that's R2. Commented Feb 4, 2014 at 6:25
  • We have a similar question here. Try this : [How to combine stored procedure and select query result][1] [1]: stackoverflow.com/questions/11503067/… Commented Feb 4, 2014 at 6:34

2 Answers 2

1

check this...

declare @var int

    exec EXEC [dbo].[SOME_PROCEDURE] @SOME_VARIABLE1,@output=@var


    INSERT INTO TEMP
    SELECT R1.A, R1.B, R2.C, R2.D FROM 
    (SELECT A , B FROM SOME_TABLE) AS R1
    CROSS JOIN @var) AS R2
Sign up to request clarification or add additional context in comments.

1 Comment

YOU mean i should return output variable?
0

You cannot use Stored Procedure in JOIN . First store resultset of stored procedure in a temp table and then use this temp table in JOIN.

-- First create the table to which we want to push the SP result
CREATE TABLE #TempTableName(..........)

-- Insert result from the SP to temp table
INSERT INTO #TempTableName
EXEC [dbo].[SOME_PROCEDURE] @SOME_VARIABLE1 

Then do the joining

INSERT INTO TEMP
SELECT R1.A, R1.B, R2.C, R2.D FROM 
(SELECT A , B FROM SOME_TABLE) AS R1
CROSS JOIN #TempTableName AS R2

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.