2009-10-21 23 views
0

我努力做到以下幾點:的DateTime操作EXECUTE sp_executesql的

EXECUTE sp_executesql 
    N'SELECT TOP 10 * FROM dbo.Items WHERE DateCreated BETWEEN @start AND @end' 
    , N'@start DATETIME, @end DATETIME' 
    , @start = '20091001' 
    , @end = GETDATE() --problem is caused by this line 

Error: 
Msg 102, Level 15, State 1, Line 5 
Incorrect syntax near ')'. 

我需要操作的日期,並把它作爲參數,即週一的一週,一年中的月份等。它甚至有可能嗎?

謝謝。

回答

5

你需要聲明一個變量,我認爲它有麻煩與使用功能:

DECLARE @endDate as datetime 
SET @endDate = GETDATE() 
EXECUTE sp_executesql 
    N'SELECT TOP 10 * FROM dbo.Items WHERE DateCreated BETWEEN @start AND @end' 
    , N'@start DATETIME, @end DATETIME' 
    , @start = '20091001' 
    , @end = @endDate 
+0

好極了!謝謝。 – 2013-09-24 08:14:05