CREATE Procedure Search(@OrderID int, @MemberID int, @TypeClass nvarchar(8), @MyDayBegin datetime, @MyDayEnd datetime)
AS
Declare @sql nvarchar(1000)
Declare @iWhere nvarchar(1000)
Set @sql= 'SELECT * FROM Order '
Set @iWhere= ' Where 1=1 '
if @OrderID <> -1
Begin
Set @iWhere=@iWhere + 'And OrderID = ' + convert(nvarchar,@OrderID)
End
if @MemberID <> -1
Begin
Set @iWhere=@iWhere + 'And MemberID = ' + convert(nvarchar,@MemberID)
End
if @TypeClass <> ' '
Begin
Set @iWhere=@iWhere + 'And TypeClass = ' ' ' + @TypeClass + ' ' ' '
End
if @MyDayBegin <> convert(datetime, '1900-1-1 ')
Begin
Set @iWhere=@iWhere + ' And DateDiff(d,MyDay, ' ' ' + @MyDayBegin + ' ' ') <= 0 '
End
if @MyDayEnd <> convert(datetime, '1900-1-1 ')
Begin
Set @iWhere=@iWhere + ' And DateDiff(d,MyDay, ' ' ' + @MyDayEnd + ' ' ') > =0 '
End
Set @sql=@sql + @iWhere
Execute sp_executesql @sql
GO
------------------------
提示:从字符串转换为 datetime 时发生语法错误。
还有上面我写的存储过程请大家忧化一下
谢谢
------解决方案--------------------------------------------------------
@MyDayBegin <> convert(datetime, '1900-1-1 ') 改成 @MyDayBegin <> '1900-1-1 '
@MyDayEnd <> convert(datetime, '1900-1-1 ') 改成 @MyDayEnd <> '1900-1-1 '
建议: <> 改成>
------解决方案--------------------------------------------------------
是传参数就报错了,还是执行存储过程的时候出错,如果是执行的时候才出错,执行到那行代码的时候出错?
------解决方案--------------------------------------------------------
添加print语句,调试一下就好了
或者使用VS自带的连接数据库的功能,可以进行断电调试的
------解决方案--------------------------------------------------------
UP下,给出详细的错误提示吧呵呵
------解决方案--------------------------------------------------------
没测试,先发一个
CREATE Procedure Search
(
@OrderID int,
@MemberID int,
@TypeClass nvarchar(8),
@MyDayBegin datetime,
@MyDayEnd datetime
)
AS
Declare @sql nvarchar(1000)
Declare @iWhere nvarchar(1000)
Set @sql= 'SELECT * FROM Order '
Set @iWhere= ' Where 1=1 '
if @OrderID <> -1
Begin
Set @iWhere = @iWhere + ' And OrderID = ' + convert(nvarchar,@OrderID)
End
if @MemberID <> -1
Begin
Set @iWhere = @iWhere + ' And MemberID = ' + convert(nvarchar,@MemberID)
End
if @TypeClass <> ' '
Begin
Set @iWhere = @iWhere + ' And TypeClass = ' ' ' + @TypeClass + ' ' ' '
End
if @MyDayBegin <> '1900-1-1 '
Begin
Set @iWhere=@iWhere + ' And DateDiff(d,MyDay, ' ' ' + Convert(nvarchar(20),@MyDayBegin) + ' ' ') <= 0 '