有没有方法再存储过程中实现根据变量访问不同的表?

tiandike 2004-11-22 11:03:31

例如根据变量@tabelname
如果是1 则select * from tabel1
如果是2 则select * from tabel2,
......
(不用下面的方式
if (tabelname=1)
select * from tabel1
if (tabelname=2)
select * from tabel2
.......

(
我本来是想这样:
declare @tabelname nvarchar(50)

select * from @tabelname
但好像在存储过程中不能这样写!
)
...全文
54 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
lipkissnow 2004-11-22
  • 打赏
  • 举报
回复
exec('select * from'+ @tabelname )
tiandike 2004-11-22
  • 打赏
  • 举报
回复
非常感谢
现在又有个问题
假设有几个表tabel1,tabel2,tabel3.....
表结构都是有a,b两列

有这样一语句:
select @temp1=
(select a
from tabel1
where b=@temp2)

如何改变为楼上的方式
tiandike 2004-11-22
  • 打赏
  • 举报
回复
非常感谢
现在又有个问题
假设有几个表tabel1,tabel2,tabel3.....
表结构都是有a,b两列

有这样一语句:
select @temp1=
(select a
from tabel1
where a=@temp2)

如何改变为楼上的方式


davorsuker39 2004-11-22
  • 打赏
  • 举报
回复
exec('select * from'+ @tabelname )
WangZWang 2004-11-22
  • 打赏
  • 举报
回复

exec('select * from '+@tabelname)

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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