一个简单的存储过程,请大家帮帮忙~~~~~~~~

qingyun67 2005-01-23 03:08:52
create procedure test
(
@dtsdate smalldatetime,
@chra varchar(50)
)
as
exec ('select * from table where chra=' + @chra + ' and Date =CONVERT(DATETIME, ' + @dtsdate + ', 102)' )

请大家帮我看看哪些错了??






...全文
117 22 打赏 收藏 转发到动态 举报
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
qingyun67 2005-01-23
  • 打赏
  • 举报
回复
谢谢:)
xluzhong 2005-01-23
  • 打赏
  • 举报
回复
比较
declare @test datetime

set @test=2005-1-23
print @test
set @test='2005-1-23'
print @test

再看看 datetime和smalldatetime 的范围
qingyun67 2005-01-23
  • 打赏
  • 举报
回复
可以了^-^
我想问一下
set @sql='select * from test0523 where chra=' +''''+ @chra+''''+ ' and dtsdate = CONVERT(DATETIME,'+''''+convert(nvarchar(30),@dtsdate ,112)+''''+',120)'
CONVERT(DATETIME,'+''''+convert(nvarchar(30),@dtsdate ,112)+''''+',120) 这部分为什么不能直接写成@dtsdate 呢?@dtsdate它的类型不是已经声名成@dtsdate smalldatetime 这个了么?
xluzhong 2005-01-23
  • 打赏
  • 举报
回复
create table test0523(id int,dtsdate datetime,chra nvarchar(50))
insert into test0523 select 1,'2005-1-1','test'
union all select 2,'2005-1-2','test'
union all select 3,'2005-1-3','aaa'
union all select 4,'2005-1-4','bbb'


declare
@dtsdate smalldatetime,
@chra varchar(50),
@sql nvarchar(4000)

set @dtsdate='2005-1-1'
set @chra = 'test'
set @sql='select * from test0523 where chra=' +''''+ @chra+''''+ ' and dtsdate = CONVERT(DATETIME,'+''''+convert(nvarchar(30),@dtsdate ,112)+''''+',120)'
print @sql
exec sp_executesql @sql


drop table test0523
qingyun67 2005-01-23
  • 打赏
  • 举报
回复
上面那样写就没有问题,现在是想写成我发的问题那样,就有问题!!!!!!1
qingyun67 2005-01-23
  • 打赏
  • 举报
回复
字段名:id,dtsdate,chra
表:table1
存储过程:test
create procedure test
(
@dtsdate smalldatetime,
@chra varchar(50)
)
as
select * from table1 where chra= @chra and dtsDate =@dtsdate
----------------------------------------------------------
传值:
@dtsdate='2005-01-01 00:00:00'
@chra='aaa'
---------------------------------------------------
qingyun67 2005-01-23
  • 打赏
  • 举报
回复
结果:
id dtsdate chra
----------- ------------------------------------------------------ --------------------------------------------------
1 2005-01-01 00:00:00 aaa


@RETURN_VALUE = 0
xluzhong 2005-01-23
  • 打赏
  • 举报
回复
给一点数据分析一下
qingyun67 2005-01-23
  • 打赏
  • 举报
回复
我的意思写成下面的查没问题,但是一做成上面的动态查就出错,不知道为什么!!!!

create procedure test
(
@dtsdate smalltime,
@chra varchar(50)
)
as
declare @dtsdate smalldatetime
select * from table where chra= @chra and Date =@dtsdate
GO
xluzhong 2005-01-23
  • 打赏
  • 举报
回复
把@dtsdate声明为datetime类型
qingyun67 2005-01-23
  • 打赏
  • 举报
回复
这句我就想从表中筛选出date=@dtsdate的数据,直接写可以,一动态的用字符串的形式写就出错,不知道为什么,试了大半天了,郁闷!!!!
qingyun67 2005-01-23
  • 打赏
  • 举报
回复
晕,还是不行
@dtsdate我赋值了,
set @dtsdate ='2005-01-01 00:00:00'

xluzhong 2005-01-23
  • 打赏
  • 举报
回复

访问和更改关系数据


转换 datetime 和 smalldatetime 数据
转换为 datetime 时,Microsoft® SQL Server™ 2000 将拒绝所有无法识别为日期的值(包括 1753 年 1 月 1 日以前的日期)。当日期在适当的范围内(1900 年 1 月 1 日到 2079 年 6 月 6 日)时,可将 datetime 值转换为 smalldatetime。时间值被四舍五入为最接近的分钟数。

此示例分别将 smalldatetime 和 datetime 值转换为 varchar 和 binary 数据类型。

DECLARE @mydate_sm SMALLDATETIME
SET @mydate_sm = '4/05/98'

SELECT CAST(@mydate_sm AS VARCHAR) AS SM_DATE_VARCHAR
GO

DECLARE @mydate DATETIME
SET @mydate = '4/05/98'

SELECT CAST(@mydate AS BINARY) AS DATE_BINARY
GO

下面是结果集:

(1 row(s) affected)

SM_DATE_VARCHAR
------------------------------
Apr 5 1998 12:00AM

(1 row(s) affected)

DATE_BINARY
--------------------------------------------------------------
0x0000000000000000000000000000000000000000000000008c3000000000

(1 row(s) affected)


请参见

CAST 和 CONVERT

数据类型

©1988-2000 Microsoft Corporation。保留所有权利。
shilei831115 2005-01-23
  • 打赏
  • 举报
回复
帮顶!!
Softlee81307 2005-01-23
  • 打赏
  • 举报
回复
create procedure test
@dtsdate smalldatetime,
@chra varchar(50)
as
exec ('select * from table where chra=''' + @chra + ''' and Date =CONVERT(DATETIME, ' + @dtsdate + ', 102)' )
xluzhong 2005-01-23
  • 打赏
  • 举报
回复
---test
declare
@dtsdate smalldatetime,
@chra varchar(50),
@sql nvarchar(4000)
set @dtsdate=getdate()
set @chra = 'test'
set @sql='select * from table where chra=' + @chra + ' and Date = CONVERT(DATETIME,'+convert(nvarchar(30),@dtsdate ,112)+ ',102)'
print @sql
---result
select * from table where chra=test and Date = CONVERT(DATETIME,20050123,102)
qingyun67 2005-01-23
  • 打赏
  • 举报
回复
to: xluzhong(打麻将一缺三,咋办?)
改过后提示:
服务器: 消息 8115,级别 16,状态 2,行 1
[Microsoft][ODBC SQL Server Driver][SQL Server]将 expression 转换为数据类型 datetime 时发生算术溢出错误。
qingyun67 2005-01-23
  • 打赏
  • 举报
回复
还是不行,还是提示那个错误
xluzhong 2005-01-23
  • 打赏
  • 举报
回复
如果这样不行则
create procedure test
(
@dtsdate smalldatetime,
@chra varchar(50)
)
as
declare @sql nvarchar(4000)
set @sql='select * from table where chra=' + @chra + ' and Date =CONVERT(DATETIME, ' + convert(nvarchar(30),@dtsdate,112) + ', 102)'


exec sp_executesql @sql
xluzhong 2005-01-23
  • 打赏
  • 举报
回复

create procedure test
(
@dtsdate smalldatetime,
@chra varchar(50)
)
as
declare @sql nvarchar(4000)
set @sql='select * from table where chra=' + @chra + ' and Date =CONVERT(DATETIME, ' + @dtsdate + ', 102)'


exec sp_executesql @sql
加载更多回复(2)

34,576

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧