新手求助:如何根据一个表中的记录,建立一张以每条记录为字段的新表?

lazulite 2003-08-03 03:47:40
我又一张表格结构如下:[数据结构表] (
[eName] [char] (10) COLLATE Chinese_PRC_CI_AS NOT NULL ,
[ID] [int] NOT NULL ,
[parient_id] [int] NOT NULL ,
[child_id] [int] NOT NULL ,
[brother_id] [int] NOT NULL ,
[pos] [int] NOT NULL ,
[sybol] [int] NOT NULL
现在表中存储了20条记录,我想以每条记录的eName为字段名建一张新表,如何做?
...全文
55 25 打赏 收藏 转发到动态 举报
写回复
用AI写文章
25 条回复
切换为时间正序
请发表友善的回复…
发表回复
zjcxc 2003-08-03
  • 打赏
  • 举报
回复
你的调用方法不对,应该是这样调用:

declare @re varchar(10) --定义变量用来保存存储过程执行结果
exec btable @re out --输入参数
print @re --显示存储过程返回结果.
qianguob 2003-08-03
  • 打赏
  • 举报
回复
学习
lazulite 2003-08-03
  • 打赏
  • 举报
回复
我还有一个问题,如果我的存储过程有一个输出参数
create proc btable @tbname varchar(10) output
为什么我执行的时候出现这个问题呢?
过程 '建基础表' 需要参数 '@tbname',但未提供该参数。
lazulite 2003-08-03
  • 打赏
  • 举报
回复
那为什么我定义字段长度为varchar(1000)是没有出现这个问题呢
pengdali 2003-08-03
  • 打赏
  • 举报
回复
警告: 已创建表 'Jgzdml',但其最大行大小(11250)超过了每行的最大字节数(8060)。如果结果行长度超过 8060 字节,则此表中行的 INSERT 或 UPDATE 将失败。

是应为你定义的表超过了一页8K
lazulite 2003-08-03
  • 打赏
  • 举报
回复
我还想请问一下,为什么我将定义的字段由1000改为800,在执行就出现下面的错误呢?
警告: 已创建表 'Jgzdml',但其最大行大小(11250)超过了每行的最大字节数(8060)。如果结果行长度超过 8060 字节,则此表中行的 INSERT 或 UPDATE 将失败。

我开始已经执行了一次定义为1000的,然后该了执行就出现上面的错
lazulite 2003-08-03
  • 打赏
  • 举报
回复
select @sql=@sql+'['+ename +'] int,' --如果不是int,就将它改成其他类型
from(select distinct ename from 数据结构表 where parient_id=1) a
能给我解释一下这句中的a做什么用的吗
zjcxc 2003-08-03
  • 打赏
  • 举报
回复
照你原来的写法,你检查的表名是:[dbo].[@tbname]
而不是真正要检查的表.(误将变量放入了字符串中)
zjcxc 2003-08-03
  • 打赏
  • 举报
回复
判断表是存在,你写错了.

应该这样:
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].['+@tbname+']') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
exec('drop table'+' [dbo].['+@tbname+']')
pengdali 2003-08-03
  • 打赏
  • 举报
回复
declare cursor_insert cursor for select eName from [数据结构表] where parient_id=1
declare @i varchar(255)
open cursor_insert
fetch cursor_insert into @i
while @@fetch_status=0
begin
exec('create table ['+@fdname+'](字段名 int,其他字段 int)')
fetch cursor_insert into @i
end
close cursor_insert
deallocate cursor_insert
lazulite 2003-08-03
  • 打赏
  • 举报
回复
我在建表之前游一个判断表是否已经存在的语句,可是好像不管用啊
在open tb之前有这么一句:
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[@tbname]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
exec('drop table'+' [dbo].['+@tbname+']')
pengdali 2003-08-03
  • 打赏
  • 举报
回复
select * into 新表 from [数据结构表] where parient_id=1
zjcxc 2003-08-03
  • 打赏
  • 举报
回复
将上面这段改一下,再试试

begin
set @sql=@sql+' ['+@fdname+'] varchar(1000) Not null,'
fetch next from tb into @fdname --根据你的要求,应该是这样的.
end
close tb
zjcxc 2003-08-03
  • 打赏
  • 举报
回复
....
begin
set @sql=@sql+' ['+@fdname+'] varchar(1000) Not null,'
fetch next from #tb into @fdname --这里的#tb是从那里来的?你没有定义这个游标吧?
end
close tb
lazulite 2003-08-03
  • 打赏
  • 举报
回复
CREATE proc btable
As
declare @tbname varchar(10)
declare @fdname varchar(10)
declare @pd integer,@cd integer
declare sCursor cursor for select eName,parient_id from [dbo].[数据结构表]
declare @sql varchar(8000)
declare tb cursor for select distinct eName from [dbo].[数据结构表] where parient_id=1
Open sCursor
fetch next from sCursor into @tbname,@pd
while @@fetch_status=0
begin
if @pd=0 break
fetch next from sCursor into @tbname,@pd
end
close sCursor
deallocate sCursor

open tb
set @sql='create table '+@tbname+'( '
fetch next from tb into @fdname
while @@fetch_status<>0
begin
set @sql=@sql+' ['+@fdname+'] varchar(1000) Not null,'
fetch next from #tb into @fdname
end
close tb
zjcxc 2003-08-03
  • 打赏
  • 举报
回复
游标没有定义错.
应该是你写的语句有错,贴出来看看.


记录不多的话,不用游标好一点,用我的这个方法试试:
declare @sql varchar(8000)
set @sql='create table 表名('
select @sql=@sql+'['+ename +'] int,' --如果不是int,就将它改成其他类型
from(select distinct ename from 数据结构表 where parient_id=1) a
set @sql=left(@sql,len(@sql)-1)+')'
exec(@sql)
lazulite 2003-08-03
  • 打赏
  • 举报
回复
执行的结果不对,建好的表应该有20各字段,他只有一个字段
我还忘了说一个问题:当我从表中取出eName的记录值时,有个要求是parient_id字段等于1的eName记录值,所以我定义的游标是
declare #tb cursor for select distinct eName from [dbo].[数据结构表] where parient_id=1
zjcxc 2003-08-03
  • 打赏
  • 举报
回复
有什么问题?
错误提示是什么?
lazulite 2003-08-03
  • 打赏
  • 举报
回复
有问题
zjcxc 2003-08-03
  • 打赏
  • 举报
回复
那就是这样处理:

declare @sql varchar(8000)
set @sql='create table 表名('
select @sql=@sql+'['+ename +'] int,' --如果不是int,就将它改成其他类型
from(select distinct ename from 数据结构表) a
set @sql=left(@sql,len(@sql)-1)+')'
exec(@sql)
加载更多回复(5)

22,209

社区成员

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

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