convert date variable to text for use in EXEC statement

I'm trying to pass a variable to a stored procedure and return a recordset from the SP.  I started with:

Declare @FromDate as Date
Set @FromDate = '2015-07-01'
EXEC df_MyStoredProcedure @FromDate

Which works just fine.  But the stored procedure returns a recordset, which I want to stuff into a temporary table, so I migrated to:

CREATE Table #MergeTemp(
[MergeAction] [nvarchar] (10) not null,
[RecCount] [int] not null

Declare @FromDate as Date
Set @FromDate = '2015-07-01'
INSERT INTO #MergeTemp EXEC('exec df_MyStoredProcedure @FromDate')

But this obviously doesn't work, because @FromDate is no longer considered a value.  How do I modify this EXEC statement to pass the actual date value to the EXEC command?
LVL 52
Dale FyeOwner, Developing Solutions LLCAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jim HornMicrosoft SQL Server Data DudeCommented:
Did you try this?
exec df_MyStoredProcedure @FromDate

Open in new window

>But this obviously doesn't work, because @FromDate is no longer considered a value.
The GO statement is not necessary here, and since it executes everything up to that point as a batch and then releases from memory, any variable declared above the GO such as @FromDate will no longer be in memory after the GO.

And in your T-SQL @FromDate is within the single quotes, so it's passing the text '@FromDate' instead of whatever the variable value is, so maybe give this a whirl..
Declare @sql nvarchar(4000) 
SELECT @sql = 'INSERT INTO #MergeTemp  exec df_MyStoredProcedure ''' + CONVERT(varchar, getdate(), 112) + ''''
sp_executesql @sql

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jim HornMicrosoft SQL Server Data DudeCommented:
The above comment has been updated a couple of times after original submission.
Dale FyeOwner, Developing Solutions LLCAuthor Commented:

Thanks for the prompt reply.  First solution worked great, but good to know the second as well.

Jim HornMicrosoft SQL Server Data DudeCommented:
Thanks for the grade Dale.  Good luck with your project.  -Jim
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.