谢谢各位高手2

latahu 2004-04-12 09:19:17
zjcxc(邹建) CCEO(CSDN的CEO??) guangli_zhang(广丽)请领分
...全文
37 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
latahu 2004-04-13
  • 打赏
  • 举报
回复
zjcxc(邹建)兄到新贴 谢谢zjcxc(邹建)兄 帖子取分 给你100分
请Hahahahahaha CCEO 到谢谢Hahahahahaha CCEO 贴子取分,haha兄30,ceo兄70

有意见当面提。呵呵。
我的msn:latahu@sina.com 本人从事军工行业应用系统开发,主要是ASP.NET C#
希望多联系和探讨。
latahu 2004-04-13
  • 打赏
  • 举报
回复
搞定了,是一个项目组成员加了一个表,数据类型设置错了。昨天不仔细,没看到。不过还是回答一下ceo的猜测,实际的表是一个userid的内容都在一个表里。因为数据量很大,我这边实际项目中,把一个单位的该内容放一个表(当然不是指userid等人员信息了)。
guangli_zhang 2004-04-12
  • 打赏
  • 举报
回复
接分,接分,谢谢再次
zjcxc 2004-04-12
  • 打赏
  • 举报
回复
谢谢楼主.

请帖上原帖地址.
CCEO 2004-04-12
  • 打赏
  • 举报
回复
也就是你需要先union再汇总还是先汇总再union?

CCEO 2004-04-12
  • 打赏
  • 举报
回复
还是说清楚点吧,
有没有可能如下:

tab_info_1 tab_info_2 .........其他表....
userid xmmc xmnr userid xmmc xmnr userid xmmc xmnr
01 姓名 a 01 生日 1980/1/1 02 ...
01 年龄 20 01 手机 13525698 02 ...
01 电话 010-1234 01 籍贯 江苏 02 ...

或者

tab_info_1 tab_info_2 .........其他表....
userid xmmc xmnr userid xmmc xmnr userid xmmc xmnr
01 姓名 a 01 年龄 20 01 ...
02 姓名 b 02 年龄 21 02 ...
03 姓名 c 03 年龄 22 03 ...


zjcxc 2004-04-12
  • 打赏
  • 举报
回复
--估计是你的表结构的数据类型不一致导致,试试下面的

--生成横表的结构固定的处理存储过程
create proc p_qry
@tbname sysname='##temp' --要生成的表名
as
declare @s1 varchar(8000),@s2 varchar(8000),@s3 varchar(8000)

if exists(select 1 from sysobjects where name=@tbname)
exec('drop table ['+@tbname+']')


select @s1='',@s2='',@s3=''
select @s1=@s1+',@'+cast(id as varchar)+' varchar(8000)'
,@s2=@s2+',@'+cast(id as varchar)+'=''
select userid=cast(userid as varchar)
,name=max(case xmmc when ''''姓名'''' then xmnr else '''''''' end)
,age=max(case xmmc when ''''年龄'''' then xmnr else '''''''' end)
,tel=max(case xmmc when ''''电话'''' then xmnr else '''''''' end)
from ['+name+'] group by userid'''
,@s3=@s3+'+'' union all ''+@'+cast(id as varchar)
from sysobjects
where name like 'tab_info_%' and xtype='U'
select @s1=substring(@s1,2,8000),@s2=substring(@s2,2,8000)
,@s3=substring(@s3,14,8000)
exec('declare '+@s1+'
select '+@s2+'
exec(''select * into ['+@tbname+'] from('+@s3+'+'')a'')
')
go

--调用
exec p_qry '##temp'

select * from ##temp
zjcxc 2004-04-12
  • 打赏
  • 举报
回复
表结构脚本,所以的表结构统一吗?
latahu 2004-04-12
  • 打赏
  • 举报
回复
执行时发生如下错误
服务器: 消息 245,级别 16,状态 1,行 1
将 varchar 值 '2002-1-2' 转换为数据类型为 int 的列时发生语法错误。

上面的那个userid在我真实表里是编号,值是2002-1-2,可能有关。帮忙看看。
zjcxc 2004-04-12
  • 打赏
  • 举报
回复
注意,如果数据库中已经有传递的表名,会被自动删除.

不固定表名是为你方便你在多用户环境下,多个用户同时调用
zjcxc 2004-04-12
  • 打赏
  • 举报
回复
--表名你自己定义


--生成横表的结构固定的处理存储过程
create proc p_qry
@tbname sysname='##temp' --要生成的表名
as
declare @s1 varchar(8000),@s2 varchar(8000),@s3 varchar(8000)

if exists(select 1 from sysobjects where name=@tbname)
exec('drop table ['+@tbname+']')


select @s1='',@s2='',@s3=''
select @s1=@s1+',@'+cast(id as varchar)+' varchar(8000)'
,@s2=@s2+',@'+cast(id as varchar)+'=''
select userid
,name=max(case xmmc when ''''姓名'''' then xmnr else '''''''' end)
,age=max(case xmmc when ''''年龄'''' then xmnr else '''''''' end)
,tel=max(case xmmc when ''''电话'''' then xmnr else '''''''' end)
from ['+name+'] group by userid'''
,@s3=@s3+'+'' union all ''+@'+cast(id as varchar)
from sysobjects
where name like 'tab_info_%' and xtype='U'
select @s1=substring(@s1,2,8000),@s2=substring(@s2,2,8000)
,@s3=substring(@s3,14,8000)
exec('declare '+@s1+'
select '+@s2+'
exec(''select * into ['+@tbname+'] from('+@s3+'+'')a'')
')
go

--调用
exec p_qry '##temp'

select * from ##temp
latahu 2004-04-12
  • 打赏
  • 举报
回复
结果表的表名叫什么呢?我还要从结果表里查询、统计,所以需要一个表名。是不是存储过程的最开始得建一个表(第二次执行的话,还得先清空这个表),然后把下面查询出来得内容放进去。邹兄给个再完整一点的。CCEO兄也谢谢,不过我要固定列的,那样可以改列名。
zjcxc 2004-04-12
  • 打赏
  • 举报
回复
--测试

--测试数据
create table tab_info_1(userid char(2),xmmc varchar(10),xmnr varchar(10))
insert tab_info_1 select '01','姓名','a'
union all select '01','年龄','20'
union all select '01','电话','010-1234'

create table tab_info_2(userid char(2),xmmc varchar(10),xmnr varchar(10))
insert tab_info_2 select '02','姓名','b'
union all select '02','年龄','21'
union all select '02','电话','1234'

create table tab_info_3(userid char(2),xmmc varchar(10),xmnr varchar(10))
insert tab_info_3 select '03','姓名','c'
union all select '03','年龄','22'
union all select '03','电话','a1234'
go

--生成横表的结构固定的处理存储过程
create proc p_qry
as
declare @s1 varchar(8000),@s2 varchar(8000),@s3 varchar(8000)
select @s1='',@s2='',@s3=''

select @s1=@s1+',@'+cast(id as varchar)+' varchar(8000)'
,@s2=@s2+',@'+cast(id as varchar)+'=''
select userid
,name=max(case xmmc when ''''姓名'''' then xmnr else '''''''' end)
,age=max(case xmmc when ''''年龄'''' then xmnr else '''''''' end)
,age=max(case xmmc when ''''电话'''' then xmnr else '''''''' end)
from ['+name+'] group by userid'''
,@s3=@s3+'+'' union all ''+@'+cast(id as varchar)
from sysobjects
where name like 'tab_info_%' and xtype='U'
select @s1=substring(@s1,2,8000),@s2=substring(@s2,2,8000)
,@s3=substring(@s3,14,8000)
exec('declare '+@s1+'
select '+@s2+'
exec('''+@s3+')
')
go

--调用
exec p_qry
go

--删除测试
drop table tab_info_1,tab_info_2,tab_info_3
drop proc p_qry

/*--测试结果

userid name age age
------ ---------- ---------- ----------
01 a 20 010-1234
02 b 21 1234
03 c 22 a1234

(所影响的行数为 3 行)
--*/
zjcxc 2004-04-12
  • 打赏
  • 举报
回复
--生成横表的结构固定的处理存储过程
create proc p_qry
as
declare @s1 varchar(8000),@s2 varchar(8000),@s3 varchar(8000)
select @s1='',@s2='',@s3=''

select @s1=@s1+',@'+cast(id as varchar)+' varchar(8000)'
,@s2=@s2+',@'+cast(id as varchar)+'=''
select userid
,name=max(case xmmc when ''''姓名'''' then xmnr else '''''''' end)
,age=max(case xmmc when ''''年龄'''' then xmnr else '''''''' end)
,age=max(case xmmc when ''''电话'''' then xmnr else '''''''' end)
from ['+name+'] group by userid'''
,@s3=@s3+'+'' union all ''+@'+cast(id as varchar)
from sysobjects
where name like 'tab_info_%' and xtype='U'
select @s1=substring(@s1,2,8000),@s2=substring(@s2,2,8000)
,@s3=substring(@s3,14,8000)
exec('declare '+@s1+'
select '+@s2+'
exec('''+@s3+')
')
go

--调用
exec p_qry
CCEO 2004-04-12
  • 打赏
  • 举报
回复
6哈兄好,怎么还是错了不管?

一起考虑(偷懒了,用了全局临时表):
declare @sql varchar(2000)
set @sql='select * into ##a '
select @sql=@sql+' from '+name+' union all select * ' from sysobjects where xtype='u' and name like 'tab[_]info[_]___'
if len(@sql)>19
begin
set @sql=left(@sql,len(@sql)-19)
exec (@sql)
end

declare @sql1 varchar (2000)

set @sql1 =''
select @sql1=@sql1+',max(case xmmc when '''+xmmc+''' then xmnr else null end) as ['+xmmc+']'
from (select distinct xmmc from ##a) as x
set @sql1='select userid'+@sql1+' from ##a group by userid drop table ##a'
exec (@sql1)
latahu 2004-04-12
  • 打赏
  • 举报
回复
哈哈兄,执行了命令成功,但没有结果,您麻烦点。建几个表试试吧。给你加分。

邹兄,这个问题和上面原来的竖表变横表是一起的。要不写一个这样的吧。把以tab_info_开头的所有的表(结构一样,都是竖表),内容合并在一起,而且变成横表格式。生成横表时不要动态的,就那些列,假设这几个表都是如下结构
字段(用户名userid,项目名称xmmc,项目内容xmnr)
数据举例如下
tab_info_1 tab_info_2 .........其他表....
userid xmmc xmnr userid xmmc xmnr
01 姓名 a 02 姓名 b
01 年龄 20 02 年龄 21
01 电话 010-1234 02 电话 1234


想要通过sql语句变成如下结构,并得到数据
userid name age tel
01 a 20 010-1234
02 b 21 1234
....

最后生成的表最好生成一个视图或新表(每次执行语句时,新表或者视图里原来的数据得清空),那样程序里好读好统计。
另开200分。整个问题等于500分。
Hahahahahaha 2004-04-12
  • 打赏
  • 举报
回复
更正一下,再错了就不管了

declare @sql varchar(2000)
set @sql=''
select @sql=@sql+' select * from '+name+' union all' from sysobjects where xtype='u' and name like 'tab[_]info[_]___'
if len(@sql)>10
begin
set @sql=left(@sql,len(@sql)-10)
exec (@sql)
end


哈,哈哈,哈哈哈......

Hahahahahaha 2004-04-12
  • 打赏
  • 举报
回复
declare @sql varchar(2000)
set @sql=''
select @sql=@sql+' select * from '+name+' union all' from sysobjects where xtype='u' and name like 'tab[_]info[_]___'
set @sql=left(@sql,len(@sql)-10
exec (@sql)

好久没写了,还是那句话,错了不管

哈,哈哈,哈哈哈......
latahu 2004-04-12
  • 打赏
  • 举报
回复
原贴地址:http://expert.csdn.net/Expert/topic/2946/2946303.xml?temp=.2294428

不过邹兄,能不能再帮我解决个问题。呵呵

我的数据库里有表tab_info_101
tab_info_102
tab_info_103
这三个表结构完全一样,我现在要把三个表的内容用union合在一起。tab_info_是固定的,但表名的最后部分不确定,可能是101,102,103,还可能有其他的如104,105。我要把所有以tab_info_开头的表数据合在一起(表结构都一样)。我可以通过程序读取所有表,进行判断,再合并。有没有可以直接通过sql语句实现,请帮忙。如果还想要分,解答后告诉我希望多少分,我再开贴结了。^_^

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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