1、用VARCHAR类型
declare @t varchar(10)
select @t='10:23'
select @t结果:----------
10:23 (1 row(s) affected)
2、用DATETIME类型
declare @t datetime
select @t='10:23'
select @t
select convert(char(10),@t,108)结果:
---------------------------
01 1 1900 10:23AM (1 row(s) affected)
----------
10:23:00 (1 row(s) affected)
declare @t varchar(10)
select @t='10:23'
select @t结果:----------
10:23 (1 row(s) affected)
2、用DATETIME类型
declare @t datetime
select @t='10:23'
select @t
select convert(char(10),@t,108)结果:
---------------------------
01 1 1900 10:23AM (1 row(s) affected)
----------
10:23:00 (1 row(s) affected)
解决方案 »
- [求助]SQL多表查询+汇总
- 数据循环插入时如果有错记录当前记录,其他继续执行怎么写??急!!!
- 如何实现如下SQL语句
- 急!!!请教高手在sqlserver中如何实现时间按月,日,小时分组查询
- 数据库是sql server 2008 因为操作失误日志文件(log)没有,数据库不能附加?
- 看这个SQL语句那儿错了
- 关于一个切换服务器的问题!(delphi+sql2000)
- 讨论:C/S软件,如何通过软件许可的最大用户数对软件进行校验是否有效注册
- 请各位大哥帮忙看一下这个SQL怎么写
- 如何解锁
- SQL SEVER为何不能启动
- SQL SERVER中的c:\program files\missoft sql server\mssql\data\中的文件为何拷贝不行呢?
我是说用一个变量来获得当前时间的小时和分的参数
比如declare @char_time char(10)
select @char_time=datepart(h,getdate())
可是这样只能单独的获得一个参数:小时
我要一起得到两个参数怎么的到那?
convert(datetime,@char_time)命令?
cast(@char_time as datetime)
都行!
values('+@Char_CardNo+','+@Char_StuNo+','+@Char_ExpNo+','+@Char_Location+','+@Char_Type+','+cast(@Float_ExpPrice as char(10))+','+cast(@Float_ExpCost as char(10))+','+@Char_TeacherNo+','+@Char_PriceType+',convert(datetime,''' +@curymd+''')'+',convert(datetime,'''+@curhour+'''),'+cast(@Float_ElaspedTime as char(1))+','+@Char_Room+')'
为什么总是提示“datetime”附近有错误那?
主要是Convert(datetime,'''+@curhour+''')这个数据,
其他的都没问题,可是只要加上这个数据就提示错误!!!
select @Char_SQL='insert into TempData_'+@Char_Room+' (Card_No,Stu_No,Exp_No,location,Exp_Type,Exp_Price,Exp_Cost,Recorder_No,PriceType,Exp_Date,Exp_Time,Elapsed_Time,Room_No)
values('+@Char_CardNo+','+@Char_StuNo+','+@Char_ExpNo+','+@Char_Location+','+@Char_Type+','+cast(@Float_ExpPrice as char(10))+','+cast(@Float_ExpCost as char(10))+','+@Char_TeacherNo+','+@Char_PriceType+',convert(datetime,''' +@curymd+''')'+','convert(datetime,'''+@curhour+'''),'+cast(@Float_ElaspedTime as char(1))+','+@Char_Room+')'
原来是不多的,加了就多了!
Singal(落花流水) :
你把@curhour PRINT出来看看,可能个是不对!
还有一个问题,就是数据@Char_Room
这个数据作为一个外部变量,
我有一个表Temp_+@Char_Room
的,可是每次exec procedure proc_name 's101'进行插入时
总是说
“在此上下文中不允许使用 's101'。此处只允许使用常量、表达式或变量。不允许使用列名。”
用 print @Char_SQL
拿出来单独执行看一看
估计是你中间有空格的问题.
declare @a char(10)
declare @b varchar(10)
declare @c varchar(20)
select @a='ab',@b='ab'
select @c=@a+'1'
select @c
select @c=@b+'1'
select @c结果:(1 row(s) affected)
(1 row(s) affected)
--------------------
ab 1 (1 row(s) affected)
(1 row(s) affected)
--------------------
ab1 (1 row(s) affected)
而且没有其他的付值操作,这个insert操作是在存储过程的最后使用的,在这之前
Char_Room都能正确的得到's101'这个值的,现在我把所有的外部变量类型
都改成varchar了,可是还是不行
改成这样事实:select @Char_SQL='insert into TempData_'+@Char_Room+' (Card_No,Stu_No,Exp_No,location,Exp_Type,Exp_Price,Exp_Cost,Recorder_No,PriceType,Exp_Date,Exp_Time,Elapsed_Time,Room_No)
values('+@Char_CardNo+','+@Char_StuNo+','+@Char_ExpNo+','+@Char_Location+','+@Char_Type+','+cast(@Float_ExpPrice as char(10))+','+cast(@Float_ExpCost as char(10))+','+@Char_TeacherNo+','+@Char_PriceType+',convert(datetime,''' +@curymd+''')'+',convert(datetime,'''+@curhour+'''),'+cast(@Float_ElaspedTime as char(1))+','''+@Char_Room+''')'
把结果字符串复制出来单独执行,应该知道错在那
其他的不加引号也可以用啊!