Execute Stored Procedure with Parameters and Insert the Resultset Into a Table


 
 --Set parameters
declare	@did int = 1,
		@eid int =88

declare @sqltax varchar(1000);
select @sqltax = 'SET NOCOUNT ON;SET FMTONLY OFF; EXEC [mydb].[dbo].[getMytaxes]
	 @did = '+cast(@did as varchar(15))
	 +', @eid = '+cast(@eid as varchar(15))
		
EXEC ('SELECT *  INTO mytaxes  FROM  OPENROWSET(''SQLNCLI'', ''Server=MC12\MSSQL08R2;Trusted_Connection=yes;'',''' +  @sqltax + ''') AS t;')
;

select eid, sum(tax) as taxtotal 
into mytaxes2
from mytaxes
group by eid


Drop table mytaxes
Drop table mytaxes2

 
Advertisements


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s