Appending variables inside dynamic sql in sql server
<p>I'm having trouble building a dynamic sql string to run an openquery. When I print the query string it evaluates the variable name as string instead of the actual value. Here's what I have:</p>

<pre><code>Declare @tsql varchar(1000)

Declare @book_review_start as date

Declare @book_review_end as date

set @book_review_start =  convert(varchar(10),DATEADD(month, DATEDIFF(month, 0, GETDATE())-2, 0), 120)

set @book_review_end =  convert(varchar(10), DATEADD(mm, DATEDIFF(mm, 0, GETDATE()), -2), 120)

Set @tsql = 'select * from openquery(authorsdb, ''select distinct ssn as bsn
from authors.dbo.nytimes where review_date between '' + @book_review_start + 'and' + '@book_review_end' + 'and review = 'annual'
        and city_name = ''
        and review_hrs = 0
        and review_days = 0')' 
</code></pre>

<p>That throws all kinds of conversion errors and such.</p>