如何在sql语句中实现变量?

skydb 2003-07-13 10:00:31
我常要执行这样一类sql语句
select * from tableA where date='20003-6-30'
union all
select * from tableB where date='20003-6-30'
union all
select * from tableC where date='20003-6-30'
union all
select * from tableD where date='20003-6-30'

可不可以将其中的'2003-6-30'设成一个变量代替,这样在执行sql语名前我只需更改变量的值即可
谢谢


...全文
27 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
xueqs 2003-07-13
  • 打赏
  • 举报
回复
这好象只能在存储过程重用吧
skydb 2003-07-13
  • 打赏
  • 举报
回复
结了分,发现问题:

DECLARE @theDate datetime
SELECT @theDate = '20003-6-30'

select * from orderin where 入库日期 = @theDate

执行后提示:
服务器: 消息 241,级别 16,状态 1,行 2
从字符串转换为 datetime 时发生语法错误。

所以不得不再发贴请教了
pengdali 2003-07-13
  • 打赏
  • 举报
回复
declare @a datetime
set @a='2003-6-30'
select * from tableA where date=@a
union all
select * from tableB where date=@a
union all
select * from tableC where date=@a
union all
select * from tableD where date=@a
caiyunxia 2003-07-13
  • 打赏
  • 举报
回复
declare @date datetime
select * from tableA where date=@date
union all
select * from tableB where date=@date
union all
select * from tableC where date=@date
union all
select * from tableD where date=@date

34,576

社区成员

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

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