我是新来的,问几个数据库方面的问题,大家多帮忙,谢谢!(100分)

  • 主题发起人 主题发起人 CuiHY
  • 开始时间 开始时间
C

CuiHY

Unregistered / Unconfirmed
GUEST, unregistred user!
各位大侠,高手:
你们好!
小弟是个新手,经别人介绍来这的,感觉这确实是个学习,交流的好地方!

问题1: 临时表问题

[1]怎样建立临时表?

若用SQL语句似乎应该这样建立:
Query1->SQL->Add("Select 现有表.字段1,现有表.字段2... into '#临时表' from 现有表");
但执行出错! -- "General SQL error.查询语法错误.查询子句不完整."
难道应该先用"CREATE TABLE '#临时表'..."建完再执行上面的语句吗?
到底应该怎样建临时表我实在是弄不明白!!

若用TTable控件能建立临时表吗?怎样建?(好象给人的感觉是用TTable只能建立
dBase,Foxpro,Paradox这样的临时表,而不能建立Access,SQL临时表)

最好给出代码(Delphi,C++Builder都行)

[2]怎样使用临时表?
[3]怎样删除临时表?

上面的问题都没解决,这两个就更不用说了!


问题2: 内存表问题 (首先,我不想用Rxlib控件)

[1]怎样建立内存表?
[2]怎样使用内存表?
[3]怎样删除内存表?


各位多帮忙,谢谢!
 
insert into '#临时表' Select 现有表.字段1,现有表.字段2... from 现有表

按我的习惯,决不会在程序中创建、删除或修改表。Delphi的Local SQL局限性较大
要学SQL最好用MSSQL

 
to OopsWare:

用你的这个语句,依然出错,且错误提示还是
"General SQL error.查询语法错误.查询子句不完整."

你能不能给一段代码,包括建立,使用,删除.

>>按我的习惯,决不会在程序中创建、删除或修改表。

对此我有不同的看法.我个人认为在程序中(或者说在运行期)能做的事,能实现的
功能,比在设计期的设置要灵活,强大的多.况且在程序中创建临时表可以实现很
多复杂的查询和计算,这是大家都认可的.

>>Delphi的Local SQL局限性较大,要学SQL最好用MSSQL

这仅仅是目标DBMS对SQL语言支持的问题,与Delphi无关,
比如:我用Delphi操作Access97数据库,可以用IIf()函数,而这个函数在Delphi
的Local SQL中没有.
 
with TTable.Create(self) do begin
DatabaseName:=TDatabase.DatabaseName;
TableName:='Foo';
TableType:=ttDefault;
with FieldDefs do begin
Add('Number',ftInteger,0,True);
Add('Name',ftString,25,False);
end;
IndexDefs.Add('','Number',[ixPrimary,ixUnique]);
CreateTable;
end;

内存表方面我也不清楚,平时我总是用TClientDataSet把数据调入内存中。
 
to Hjking:
首先对你的解答表示感谢!

>>平时我总是用TClientDataSet把数据调入内存中
对此,麻烦你也给段代码.

更重要的是,用SQL语言怎样建临时表!!!也请帮忙想一想!
 
怎么没人回答呀!我知道现在全国各地都很热,麻烦大家了,帮帮小弟,谢谢!!
 
怎么没人回答呀!我知道现在全国各地都很热,麻烦大家了,帮帮小弟,谢谢!!
主要是用SQL语言怎样建临时表!!!
 
我前段时间也有类似的问题!
我想问一下你执行SQL语句时是用什么?
是query1.open还是query1.execsql;
如是query1.open则改成query1.execsql即可!
 
另给个例子给你!
query2.SQL.clear;
query2.sql.Add ('insert into yt2(station) select station from yt1');
query2.ExecSQL ;
以后对yt2的操作与其它表一模一样!
 
to 深圳小吴:
麻烦你把"用SQL语言怎样建临时表"的全过程写出来好吗?
包括设计期往Form上放置控件,设置属性,以及代码都写出来.
提出这样的要求实在不好意思,但我实在是搞不明白!

若能把问题解决,除这100分,我再追加100分,决不失言!!!
 
我拷!你好大方哦!现在手上有活!
你稍等!代码我发给你!
 
启只是"活",是"饭碗"!!
 
to 深圳小吴:
你快发呀!我等着呢!!
cuihongyuan@sina.com

多谢!!!
 
可以试试下面的程序,应该能满足你的要求
with TQuery.Create(Application) do
try
Sql.Add('SELECT AField FROM ATable');
Sql.Add('insert into yt2(station) select station from yt1');
Open;
finally
free;
end;
 
代码很长,自动换行后不易看。这是实际的例子。你凑合看吧。

dmInventory.qinvd.close;
dmInventory.qinvd.sql.text:=' CREATE TABLE #te ('+
'gperm2 smallint, grade char(2), w1 float null, b1 float null, w2 float null, b2 float null, '+
'w3 float null, b3 float null, w4 float null, b4 float null, w5 float null, b5 float null, '+
'w6 float null, b6 float null, w7 float null, b7 float null, w8 float null, b8 float null, '+
'w9 float null, b9 float null, w10 float null, b10 float null, wRoll float null, bRoll float null, '+
'inv float null, invb float null )'+
'declare @gperm2 smallint declare @grade char(2) declare @w1 float declare @b1 float '+
'declare @w2 float declare @b2 float declare @w3 float declare @b3 float declare @w4 float '+
'declare @b4 float declare @w5 float declare @b5 float declare @w6 float declare @b6 float '+
'declare @w7 float declare @b7 float declare @w8 float declare @b8 float declare @w9 float '+
'declare @b9 float declare @w10 float declare @b10 float declare @wRoll float declare @bRoll float '+
'declare @inv float declare @invb float '+
'declare c_InvStruc cursor for select distinct gperm2,grade from inventory i,vproductdata v,stores s '+
'where i.productno=v.productno and s.storeNo=i.storeNo and s.status=''n'' and s.compNo="0000" and (i.weight<>0 or i.wtemp<>0 or i.wbooked<>0 or wway<>0) and '+
'(productName="" ';
te:='';
tu:='';
for i:=0 to checklistbox1.Items.Count-1 do
begin
if checklistbox1.checked then
te:=te+' or productName='''+checklistbox1.items+'''';
end;

n:=0;
frmrepd.qrl1.caption:='';
frmrepd.qrl2.caption:='';
frmrepd.qrl3.caption:='';
frmrepd.qrl4.caption:='';
frmrepd.qrl5.caption:='';
frmrepd.qrl6.caption:='';
frmrepd.qrl7.caption:='';
frmrepd.qrl8.caption:='';
frmrepd.qrl9.caption:='';
frmrepd.qrl10.caption:='';
for i:=0 to checklistbox2.Items.Count-1 do
begin
if checklistbox2.checked then
begin
n:=n+1;
if n>10 then
begin
showmessage('规格值超过10个,报表无法打印!');
abort;
end
else
begin
case n of
1: begin
frmrepd.qrl1.caption:=checklistbox2.Items;
tu:=tu+' select @w1=convert(float,sum(i.weight+i.wtemp)/1000),@b1=convert(float,sum(wBooked)/1000) '+
' from vproductdata v,inventory i,stores s where i.productNo=v.productNo and s.storeNo=i.storeNo and s.status=''n'' and s.compNo="0000" and (i.weight<>0 or i.wtemp<>0 or i.wbooked<>0 or wway<>0) '+
' and gperm2=@gperm2 and grade=@grade and '+
' (productName="" '+te+') and sizespec="'+checklistbox2.Items+'" update #te set w1=@w1,b1=@b1 where gperm2=@gperm2 and grade=@grade ';{ }
end;
2: begin
frmrepd.qrl2.caption:=checklistbox2.Items;
tu:=tu+' select @w2=convert(float,sum(i.weight+i.wtemp)/1000),@b2=convert(float,sum(wBooked)/1000) '+
' from vproductdata v,inventory i,stores s where i.productNo=v.productNo and s.storeNo=i.storeNo and s.status=''n'' and s.compNo="0000" and (i.weight<>0 or i.wtemp<>0 or i.wbooked<>0 or wway<>0) '+
' and gperm2=@gperm2 and grade=@grade and '+
' (productName="" '+te+') and sizespec="'+checklistbox2.Items+'" update #te set w2=@w2,b2=@b2 where gperm2=@gperm2 and grade=@grade ';
end;
3: begin
frmrepd.qrl3.caption:=checklistbox2.Items;
tu:=tu+' select @w3=convert(float,sum(i.weight+i.wtemp)/1000),@b3=convert(float,sum(wBooked)/1000) '+
' from vproductdata v,inventory i,stores s where i.productNo=v.productNo and s.storeNo=i.storeNo and s.status=''n'' and s.compNo="0000" and (i.weight<>0 or i.wtemp<>0 or i.wbooked<>0 or wway<>0) '+
' and gperm2=@gperm2 and grade=@grade and '+
' (productName="" '+te+') and sizespec="'+checklistbox2.Items+'" update #te set w3=@w3,b3=@b3 where gperm2=@gperm2 and grade=@grade ';
end;
4: begin
frmrepd.qrl4.caption:=checklistbox2.Items;
tu:=tu+' select @w4=convert(float,sum(i.weight+i.wtemp)/1000),@b4=convert(float,sum(wBooked)/1000) '+
' from vproductdata v,inventory i,stores s where i.productNo=v.productNo and s.storeNo=i.storeNo and s.status=''n'' and s.compNo="0000" and (i.weight<>0 or i.wtemp<>0 or i.wbooked<>0 or wway<>0) '+
' and gperm2=@gperm2 and grade=@grade and '+
' (productName="" '+te+') and sizespec="'+checklistbox2.Items+'" update #te set w4=@w4,b4=@b4 where gperm2=@gperm2 and grade=@grade ';
end;
5: begin
frmrepd.qrl5.caption:=checklistbox2.Items;
tu:=tu+' select @w5=convert(float,sum(i.weight+i.wtemp)/1000),@b5=convert(float,sum(wBooked)/1000) '+
' from vproductdata v,inventory i,stores s where i.productNo=v.productNo and s.storeNo=i.storeNo and s.status=''n'' and s.compNo="0000" and (i.weight<>0 or i.wtemp<>0 or i.wbooked<>0 or wway<>0) '+
' and gperm2=@gperm2 and grade=@grade and '+
' (productName="" '+te+') and sizespec="'+checklistbox2.Items+'" update #te set w5=@w5,b5=@b5 where gperm2=@gperm2 and grade=@grade ';
end;
6: begin
frmrepd.qrl6.caption:=checklistbox2.Items;
tu:=tu+' select @w6=convert(float,sum(i.weight+i.wtemp)/1000),@b6=convert(float,sum(wBooked)/1000) '+
' from vproductdata v,inventory i,stores s where i.productNo=v.productNo and s.storeNo=i.storeNo and s.status=''n'' and s.compNo="0000" and (i.weight<>0 or i.wtemp<>0 or i.wbooked<>0 or wway<>0) '+
' and gperm2=@gperm2 and grade=@grade and '+
' (productName="" '+te+') and sizespec="'+checklistbox2.Items+'" update #te set w6=@w6,b6=@b6 where gperm2=@gperm2 and grade=@grade ';
end;
7: begin
frmrepd.qrl7.caption:=checklistbox2.Items;
tu:=tu+' select @w7=convert(float,sum(i.weight+i.wtemp)/1000),@b7=convert(float,sum(wBooked)/1000) '+
' from vproductdata v,inventory i,stores s where i.productNo=v.productNo and s.storeNo=i.storeNo and s.status=''n'' and s.compNo="0000" and (i.weight<>0 or i.wtemp<>0 or i.wbooked<>0 or wway<>0) '+
' and gperm2=@gperm2 and grade=@grade and '+
' (productName="" '+te+') and sizespec="'+checklistbox2.Items+'" update #te set w7=@w7,b7=@b7 where gperm2=@gperm2 and grade=@grade ';
end;
8: begin
frmrepd.qrl8.caption:=checklistbox2.Items;
tu:=tu+' select @w8=convert(float,sum(i.weight+i.wtemp)/1000),@b8=convert(float,sum(wBooked)/1000) '+
' from vproductdata v,inventory i,stores s where i.productNo=v.productNo and s.storeNo=i.storeNo and s.status=''n'' and s.compNo="0000" and (i.weight<>0 or i.wtemp<>0 or i.wbooked<>0 or wway<>0) '+
' and gperm2=@gperm2 and grade=@grade and '+
' (productName="" '+te+') and sizespec="'+checklistbox2.Items+'" update #te set w8=@w8,b8=@b8 where gperm2=@gperm2 and grade=@grade ';
end;
9: begin
frmrepd.qrl9.caption:=checklistbox2.Items;
tu:=tu+' select @w9=convert(float,sum(i.weight+i.wtemp)/1000),@b9=convert(float,sum(wBooked)/1000) '+
' from vproductdata v,inventory i,stores s where i.productNo=v.productNo and s.storeNo=i.storeNo and s.status=''n'' and s.compNo="0000" and (i.weight<>0 or i.wtemp<>0 or i.wbooked<>0 or wway<>0) '+
' and gperm2=@gperm2 and grade=@grade and '+
' (productName="" '+te+') and sizespec="'+checklistbox2.Items+'" update #te set w9=@w9,b9=@b9 where gperm2=@gperm2 and grade=@grade ';
end;
10: begin
frmrepd.qrl10.caption:=checklistbox2.Items;
tu:=tu+' select @w10=convert(float,sum(i.weight+i.wtemp)/1000),@b10=convert(float,sum(wBooked)/1000) '+
' from vproductdata v,inventory i,stores s where i.productNo=v.productNo and s.storeNo=i.storeNo and s.status=''n'' and s.compNo="0000" and (i.weight<>0 or i.wtemp<>0 or i.wbooked<>0 or wway<>0) '+
' and gperm2=@gperm2 and grade=@grade and '+
' (productName="" '+te+') and sizespec="'+checklistbox2.Items+'" update #te set w10=@w10,b10=@b10 where gperm2=@gperm2 and grade=@grade ';
end;
end;
end;
end;
end;
dmInventory.qinvd.sql.Add(te+')' );
dmInventory.qinvd.sql.Add(' open c_InvStruc fetch next from c_InvStruc into @gperm2,@grade '+
' while (@@fetch_status=0) begin insert #te (gperm2,grade) values(@gperm2,@grade) ');
dmInventory.qinvd.sql.Add(tu);
dmInventory.qinvd.sql.Add(' select @wRoll=convert(float,sum(i.weight+i.wtemp)/1000),@bRoll=convert(float,sum(wBooked)/1000) '+
' from vproductdata v,inventory i,stores s where i.productNo=v.productNo and s.storeNo=i.storeNo and s.status=''n'' and s.compNo="0000" and (i.weight<>0 or i.wtemp<>0 or i.wbooked<>0 or wway<>0) '+
' and (v.type="3" or v.type="4") and gperm2=@gperm2 and grade=@grade and '+
' (productName="" '+te+')'+' update #te set wRoll=@wRoll,bRoll=@bRoll where gperm2=@gperm2 and grade=@grade ' );
dmInventory.qinvd.sql.Add(' select @inv=convert(float,sum(i.weight+i.wtemp)/1000),@invb=convert(float,sum(wBooked)/1000) '+
' from vproductdata v,inventory i,stores s where i.productNo=v.productNo and s.storeNo=i.storeNo and s.status=''n'' and s.compNo="0000" and (i.weight<>0 or i.wtemp<>0 or i.wbooked<>0 or wway<>0) '+
' and gperm2=@gperm2 and grade=@grade and '+
' (productName="" '+te+')'+' update #te set inv=@inv,invb=@invb where gperm2=@gperm2 and grade=@grade ' );
dmInventory.qinvd.sql.Add(' fetch next from c_InvStruc into @gperm2,@grade end '+
'close c_InvStruc deallocate c_InvStruc select *,inv-invb least from #te ' );
// showmessage(dmInventory.qinvd.sql.text);

dmInventory.qinvd.open;
frmrepd.QuickRep1.Preview;
 
Sorry!这么晚才来!下面是我的例子!比较简单!你先用吧!可别记了给我加分哦!
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Grids, DBGrids, Db, DBTables;

type
TForm1 = class(TForm)
DataSource1: TDataSource;
Query1: TQuery;
DBGrid1: TDBGrid;
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure FormActivate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
i:integer;
implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
begin
i:=i+1;
query1.SQL.clear;
query1.sql.add('create table dademo (t1 integer )');//建只有一个字段名为t1(类型为整数型)的临时表dademo
query1.execsql;
query1.SQL.clear;
query1.sql.add('insert into dademo values('+inttostr(i)+')');//往临时表中加数据
query1.ExecSql;
//加完数据后,这中间你就可以对数据表进行查询,修改,删除等操作了
query1.SQL.clear;
query1.sql.Add('select * from dademo');
query1.open;
query1.active:=true;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
query1.CloseDatabase (query1.database);
if deletefile('dademo.db') then showmessage('ok!')//删除临时表,此时要注意你创建的临时表所在的目录
else showmessage('no!');
end;

procedure TForm1.FormActivate(Sender: TObject);
begin
i:=0;
end;

end.
 
难道你不知道有联机帮助吗?
这些问题查查联机帮助都有了
 
to 深圳小吴:
容我好好想想,分肯定给你加,我总觉得自己的思路是不是该变通一下.
 
其实大部分有些人认为需要临时表才能解决的问题用视图也能很好的解决
 
那你有什么新的想法啊?说出来大家听听吗?
 

Similar threads

S
回复
0
查看
730
SUNSTONE的Delphi笔记
S
S
回复
0
查看
738
SUNSTONE的Delphi笔记
S
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
后退
顶部