2015-04-03 69 views
-1

我寫這樣的如何在sql中的動態查詢中傳遞datetime?

ALTER PROCEDURE [dbo].[spLoadPendingPaymentSheetByFilter] --'2015-04-01','2015-04-02','Select-One','Select-One','Select-One','' 
    @FromDate as datetime, 
    @ToDate as datetime, 
    @Status as nvarchar(50), 
    @Remarks as nvarchar(50), 
    @Paymenttype as nvarchar(50), 
    @BillID as nvarchar(50)  
AS 
    Declare @Where as nvarchar(max) 

    set @Where = '(MenifestDate BETWEEN ''' + CONVERT(VARCHAR(10),@FromDate, 101) + ''' and ''' + CONVERT(VARCHAR(10),@ToDate, 101) + ''')' 

    if(@Status <> 'Select-One') 
     set @Where = 'Status = '+ @Status 

    if(@Remarks <> 'Select-One') 
     set @Where = @Where + 'and Remarks = '+ @Remarks 

    if(@Paymenttype <> 'Select-One') 
     set @Where = @Where + 'and PaymentType = ' + @Paymenttype 

    if(@BillID <> '') 
     set @Where = @Where + 'and BillID = '+ @BillID 

    Declare @SelectString as nvarchar(1000) 
    set @SelectString = 'SELECT MasterID,BillID, MenifestDate, FarwardingNo,ReceverCountryName,Status,Remarks,PaymentType  
    FROM tblMenifest 
    WHERE ' + @Where 

    exec @SelectString 

存儲過程當我執行它,我得到這個錯誤

名稱「選擇MasterID,BILLID,MenifestDate,FarwardingNo,ReceverCountryName,狀態,備註,PaymentType FROM tblMenifest WHERE(MenifestDate BETWEEN '04/01/2015' 和'04/02/2015' )」是不是有效的標識符

MenifestDate列數據類型是datetime

回答

1

我相信你想把EXEC(@SelectString)而不是exec @SelectString