日期:2014-05-18  浏览次数:20611 次

sql提示语法错误
SQL code

EXEC
     ('
         SELECT vvm.*, 
                dbo.GetBidPublish(vvm.TvaID,'''+CAST(@StartDateTime AS VARCHAR)+''','''+CAST(@EndDateTime AS VARCHAR)+''') AS BidPublishNum, 
                dbo.BuyerPublish(vvm.TvaID,'''+CAST(@StartDateTime AS VARCHAR)+''','''+CAST(@EndDateTime AS VARCHAR)+''') AS BuyerPublishNum,                                                                                  
                dbo.AttentionPublish(vvm.TvaID,'''+CAST(@StartDateTime AS VARCHAR)+''','''+CAST(@EndDateTime AS VARCHAR)+''') AS AttentionPublishNum,
                TradeOrder.TradeAmount,
                TradeOrder.Sevice_Pay,
                TradeOrder.Sevice_NotPay,
                TradeOrder.LogisticsFee,
                TradeOrder.TransferFee,     
                dbo.GetTradeSuccessPublish(vvm.TvaID,'''+CAST(@StartDateTime AS VARCHAR)+''','''+CAST(@EndDateTime AS VARCHAR)+''') AS TradeSuccessPublishNum, 
                dbo.GetAppealPublish(vvm.TvaID,'''+CAST(@StartDateTime AS VARCHAR)+''','''+CAST(@EndDateTime AS VARCHAR)+''') AS AppealedPublishNum
         FROM V_VendorManager vvm 
         LEFT JOIN (SELECT * FROM dbo.GetVendorTradeData('''+CAST(@StartDateTime AS VARCHAR)+''','''+CAST(@EndDateTime AS VARCHAR)+''')) AS TradeOrder
         ON vvm.TvaID=TradeOrder.BuyerTvaId
     ')



提示错误:'CAST' 附近有语法错误。 我确实找不到哪错了,请大神指点

------解决方案--------------------
SQL code

declare @StartDatetime datetime
declare @EndDateTime datetime
declare @sql varchar(max)
set @sql='
         SELECT vvm.*, 
                dbo.GetBidPublish(vvm.TvaID,'''+CAST(@StartDateTime AS VARCHAR(20))+''','''+CAST(@EndDateTime AS VARCHAR(20))+''') AS BidPublishNum, 
                dbo.BuyerPublish(vvm.TvaID,'''+CAST(@StartDateTime AS VARCHAR(20))+''','''+CAST(@EndDateTime AS VARCHAR(20))+''') AS BuyerPublishNum,                                                                                  
                dbo.AttentionPublish(vvm.TvaID,'''+CAST(@StartDateTime AS VARCHAR(20))+''','''+CAST(@EndDateTime AS VARCHAR(20))+''') AS AttentionPublishNum,
                TradeOrder.TradeAmount,
                TradeOrder.Sevice_Pay,
                TradeOrder.Sevice_NotPay,
                TradeOrder.LogisticsFee,
                TradeOrder.TransferFee,     
                dbo.GetTradeSuccessPublish(vvm.TvaID,'''+CAST(@StartDateTime AS VARCHAR(20))+''','''+CAST(@EndDateTime AS VARCHAR(20))+''') AS TradeSuccessPublishNum, 
                dbo.GetAppealPublish(vvm.TvaID,'''+CAST(@StartDateTime AS VARCHAR(20))+''','''+CAST(@EndDateTime AS VARCHAR(20))+''') AS AppealedPublishNum
         FROM V_VendorManager vvm 
         LEFT JOIN (SELECT * FROM dbo.GetVendorTradeData('''+CAST(@StartDateTime AS VARCHAR(20))+''','''+CAST(@EndDateTime AS VARCHAR(20))+''')) AS TradeOrder
         ON vvm.TvaID=TradeOrder.BuyerTvaId
     '
exec (@sql)

------解决方案--------------------
SQL code

declare @StartDatetime datetime
set @StartDatetime=getdate()
select convert(varchar(10),@StartDateTime,120)
/*
2012-06-05
*/

--1\转时间格式建议使用convert
--2\在外面拼接后,统一exec