请教数据表问题

parrot919 2002-05-13 10:10:10
有两个表#S1()(临时表),S2(名称,字段类型),请问怎样将S2中的记录(n条)
S2 : M1 Int
M2 String
M3 Bool
……
作为#S1的字段名和字段类型?
Eg: #S1 :字段 M1 M2 M3
记录 3 abc 1
4 bdf 0
……
...全文
27 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
firetoucher 2002-05-16
  • 打赏
  • 举报
回复
declare @s nvarvhar(4000),@f1 varchar(255),@f2 varchar(255)


select @s = 'create table #s ('
declare cursor c for
select 名称, 字段类型 from s2
open c
fetch c nect into @f1, @f2
select @s = @s + @f1 + ' ' + @f2
fetch c nect into @f1, @f2
while @@fetch_status <> 0 do
begin
select @s = @s + ',' + @f1 + ' ' + @f2
fetch c nect into @f1, @f2
end
exec (@s)

我现在在网吧,所以随手写的,可能有表示错误,自己调试一下吧;)
firetoucher 2002-05-16
  • 打赏
  • 举报
回复
declare @s nvarvhar(4000)


select @s = 'create table #s ('
declare cursor c for
select 名称, 字段类型 from s2

parrot919 2002-05-15
  • 打赏
  • 举报
回复
怎样解决?
firetoucher 2002-05-14
  • 打赏
  • 举报
回复
用sql就能解决啊
王集鹄 2002-05-14
  • 打赏
  • 举报
回复
begin
Table1.First;
Query1.Close;
Query1.SQL.Clear;
Query1.SQL.Add('CREATE TABLE S1 (');
while not Table1.Eof do begin
if SameText(Table1.FieldByName('字段类型').AsString, 'int') then
Query1.SQL.Add(Format('%s int,', [Table1.FieldByName('名称').AsString]))
else if SameText(Table1.FieldByName('字段类型').AsString, 'string') then begin
Query1.SQL.Add(Format('%s varchar(30),', [Table1.FieldByName('名称').AsString]))
end else if SameText(Table1.FieldByName('字段类型').AsString, 'bool') then begin
Query1.SQL.Add(Format('%s varchar(2),', [Table1.FieldByName('名称').AsString]))
//直接字符“是”、“否”取代bool类型
end else if SameText(Table1.FieldByName('字段类型').AsString, 'float') then begin
Query1.SQL.Add(Format('%s float,', [Table1.FieldByName('名称').AsString]))
end;
Table1.Next;
end;
Query1.SQL.Add(')');
// Memo1.Text := Query1.SQL.Text; //调试用//如果不成功就把Memo1的文本复制到SQL管理程序中执行看看什么地方错误
Query1.ExecSQL;
end;

flyqi 2002-05-13
  • 打赏
  • 举报
回复
好像可以通过sql语句直接实现吧?你去sql2000帮助查一下select 语句
parrot919 2002-05-13
  • 打赏
  • 举报
回复
非常感谢!SQLServer2000
王集鹄 2002-05-13
  • 打赏
  • 举报
回复
你用的是什么数据库?
//参考
var
vSize: Integer;
vDataType: TFieldType;
begin
Table1.First;
Table2.Close;
Table2.TableName := 'S1';
while not Table1.Eof do begin
if SysUtils.SameText(Table1.FieldByName('字段类型').AsString, 'int') then begin
vSize := 0;
vDataType := ftInteger;
end else if SysUtils.SameText(Table1.FieldByName('字段类型').AsString, 'string') then begin
vSize := 10;
vDataType := ftString;
end else if SysUtils.SameText(Table1.FieldByName('字段类型').AsString, 'bool') then begin
vSize := 0;
vDataType := ftBoolean;
end else if SysUtils.SameText(Table1.FieldByName('字段类型').AsString, 'float') then begin
vSize := 0;
vDataType := ftFloat;
end;
Table2.FieldDefs.Add(Table1.FieldByName('名称').AsString, vDataType, vSize);
Table1.Next;
end;
Table2.CreateTable;
end;

5,388

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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