create procedure test
(
@dtsdate smalldatetime,
@chra varchar(50)
)
as
declare @sql nvarchar
set @sql='select * from table where chra=' + @chra + ' and Date =CONVERT(DATETIME, ' + @dtsdate + ', 102)'exec sp_executesql @sql
(
@dtsdate smalldatetime,
@chra varchar(50)
)
as
declare @sql nvarchar
set @sql='select * from table where chra=' + @chra + ' and Date =CONVERT(DATETIME, ' + @dtsdate + ', 102)'exec sp_executesql @sql
解决方案 »
- 求一个SQL
- 请简单介绍SQL Server2005数据库如何附加数据库
- 一个比较复杂的sql查询,求真相,且重赏
- 由于工作的原因sql 指会一点,下决心系统的学一下!推荐本好书吧!谢谢
- MSSQL跨库更新问题
- 初次到数据库来提问,希望能得到各位的帮助:怎么将word文档弄进SQL数据库啊?二进制的。具体的操作步骤。
- 爱好者和高手请进,一个奇怪的输出
- 这可能是最难的按月统计问题!!!!
- 各位大侠,关于Access编译的.......
- 怎样在数据库的一个表里筛选出每一人的时间最新的一条记录?用SQL语句。感谢大神啊
- 紧急求助!!!用sqlserver做一个存储课题资料的数据库
- 求解,关于数据库的几个简单问题?在线等待...
运行上面的代码,提示这样的错误:服务器: 消息 295,级别 16,状态 3,过程 test,行 8
[Microsoft][ODBC SQL Server Driver][SQL Server]从字符串转换为 smalldatetime 数据类型时发生语法错误。
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
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
改过后提示:
服务器: 消息 8115,级别 16,状态 2,行 1
[Microsoft][ODBC SQL Server Driver][SQL Server]将 expression 转换为数据类型 datetime 时发生算术溢出错误。
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)
@dtsdate smalldatetime,
@chra varchar(50)
as
exec ('select * from table where chra=''' + @chra + ''' and Date =CONVERT(DATETIME, ' + @dtsdate + ', 102)' )
访问和更改关系数据
转换 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
GODECLARE @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。保留所有权利。
@dtsdate我赋值了,
set @dtsdate ='2005-01-01 00:00:00'
(
@dtsdate smalltime,
@chra varchar(50)
)
as
declare @dtsdate smalldatetime
select * from table where chra= @chra and Date =@dtsdate
GO
id dtsdate chra
----------- ------------------------------------------------------ --------------------------------------------------
1 2005-01-01 00:00:00 aaa
@RETURN_VALUE = 0
表: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'
---------------------------------------------------
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
我想问一下
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 这个了么?
declare @test datetimeset @test=2005-1-23
print @test
set @test='2005-1-23'
print @test再看看 datetime和smalldatetime 的范围