0

I have created a Temp table(#TempTable). I am trying to insert the date to get but I am getting an error. It's a dynamic query.

I am trying to get the date from another table and inserting the date to temp table

Just to make sure you understand the problem I have given an example

DECLARE @OfferEndDateTime datetime
SELECT @OfferEndDateTime = getdate()-1
print @VOfferEndDateTime


DECLARE @SQL VarChar(1000)
SELECT @SQL ='INSERT INTO #TempTable '+
'SELECT D,Points,@OfferEndDateTime '
exec(@sql)

Please Let me know where I am going wrong

1
  • may be @OfferEndDateTime should be outside single quote.means u have to concat it to ur query string Commented Jan 4, 2012 at 11:05

2 Answers 2

3

You need to use sp_executesql when passing a parameter to dynamic sql

exec sp_executesql @sql, N'@OfferEndDateTime datetime', @OfferEndDateTime=@OfferEndDateTime
Sign up to request clarification or add additional context in comments.

Comments

1

You have, at least, three problems:

  1. The variable needs to be outside:
SELECT @SQL ='INSERT INTO #TempTable '+
'SELECT D,Points,' + @OfferEndDateTime
  1. The variable needs to be varchar type or similar

  2. What is D,Points? They are not defined anywhere. If they are varchar values you nead to quote them (use " or '') for that purpose.


If you need to use the parameter like datetime you should use sp_executesql instead. Check HERE for some info on it!

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.