怎么在SQL语句中动态处理表名?

zhui22222 2012-08-29 09:12:03
表tb中有一列为type,char(1),
比如type可能为A,语句想完成的功能为select tb.*,(select count(*) from type_A where ...) as 数量 ,怎么将type_A替换为动态的类似type_ + tb.type
...全文
299 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhui22222 2012-08-31
  • 打赏
  • 举报
回复
搞定了,存储过程前还要加数据库名
zhui22222 2012-08-31
  • 打赏
  • 举报
回复
zhui22222 2012-08-31
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 的回复:]

引用 11 楼 的回复:
请问楼上有没在sql2000下试过?我这里建view的时候始终报错,找不到存储过程'dbo.ProcTest',但是明明这个存储过程已经建立成功了,可以看到的

为确保存储是否成功创建,你单独执行下exec dbo.ProcTest或exec ProcTest,看能不能执行?
[/Quote]

可以执行的
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 的回复:]
请问楼上有没在sql2000下试过?我这里建view的时候始终报错,找不到存储过程'dbo.ProcTest',但是明明这个存储过程已经建立成功了,可以看到的
[/Quote]
为确保存储是否成功创建,你单独执行下exec dbo.ProcTest或exec ProcTest,看能不能执行?
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 的回复:]
请问楼上有没在sql2000下试过?我这里建view的时候始终报错,找不到存储过程'dbo.ProcTest',但是明明这个存储过程已经建立成功了,可以看到的
[/Quote]
我机子上没有2000,只在2005下测试通过。但是以上代码并没有用到2005的新特性,2000运行肯定是可以的。
zhui22222 2012-08-31
  • 打赏
  • 举报
回复
请问楼上有没在sql2000下试过?我这里建view的时候始终报错,找不到存储过程'dbo.ProcTest',但是明明这个存储过程已经建立成功了,可以看到的
  • 打赏
  • 举报
回复



--建表
create table tb(f1 int,type varchar(1))
insert into tb
select 1,'A' union all
select 2,'B' union all
select 3,'C'
go
create table type_A(col1 int)
insert into type_A
select 1 union all
select 2
go
create table type_B(col1 int)
insert into type_B
select 1 union all
select 2
go
create table type_C(col1 int)
insert into type_C
select 1 union all
select 2
go
--建sp
create proc procTest
as
begin
declare @sql varchar(4000)
select @sql=isnull(@sql+' union all ','')+'select count(*) as cnt,'''+type+''' as type from type_'+type
from tb
set @sql='select tb.*,t.cnt from tb,('+@sql+')t where tb.type=t.type'
--print @sql
exec(@sql)
end
--建view
create view viewTest
as
select * from openrowset('sqloledb','Server';'user';'pwd','set fmtonly off exec dbo.ProcTest')
--其中Server为你的数据库实例或填IP及端口
--user为登陆用户名
--pwd为登陆密码


zhui22222 2012-08-30
  • 打赏
  • 举报
回复
继续求助
  • 打赏
  • 举报
回复

-->try
create table tb(f1 int,type varchar(1))
insert into tb
select 1,'A' union all
select 2,'B' union all
select 3,'C'
create table type_A(col1 int)
insert into type_A
select 1 union all
select 2
create table type_B(col1 int)
insert into type_B
select 1 union all
select 2
create table type_C(col1 int)
insert into type_C
select 1 union all
select 2
go
declare @sql varchar(4000)
select @sql=isnull(@sql+' union all ','')+'select count(*) as cnt,'''+type+''' as type from type_'+type
from tb
set @sql='select tb.*,t.cnt from tb,('+@sql+')t where tb.type=t.type'
--print @sql
exec(@sql)
/*
f1 type cnt
----------- ---- -----------
1 A 2
2 B 2
3 C 2
*/
  • 打赏
  • 举报
回复
这个需要动态拼接SQL语句

exec('动态拼接的语句')
zhui22222 2012-08-29
  • 打赏
  • 举报
回复
实在不熟,试了半天没搞定,能把创建存储过程到视图的语句都写一下吗?
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 的回复:]
引用 4 楼 的回复:
引用 3 楼 的回复:
这个是存储过程吗?怎么把查询结果放视图里?

是存储过程的写法,你可以参考以下方法把结果放到视图里
http://blog.csdn.net/htl258/article/details/5659935

我这里是sql2000,不知道能不能用
[/Quote]
应该可以用
zhui22222 2012-08-29
  • 打赏
  • 举报
回复
没有简单点的字符串连接函数吗?
zhui22222 2012-08-29
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]
引用 3 楼 的回复:
这个是存储过程吗?怎么把查询结果放视图里?

是存储过程的写法,你可以参考以下方法把结果放到视图里
http://blog.csdn.net/htl258/article/details/5659935
[/Quote]
我这里是sql2000,不知道能不能用
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]
这个是存储过程吗?怎么把查询结果放视图里?
[/Quote]
是存储过程的写法,你可以参考以下方法把结果放到视图里
http://blog.csdn.net/htl258/article/details/5659935
zhui22222 2012-08-29
  • 打赏
  • 举报
回复
这个是存储过程吗?怎么把查询结果放视图里?

34,590

社区成员

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

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