全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:2010
推到 Plurk!
推到 Facebook!

如何用SQL語法建立變數新Table?

尚未結案
nick167
中階會員


發表:86
回覆:133
積分:53
註冊:2003-02-12

發送簡訊給我
#1 引用回覆 回覆 發表時間:2004-03-20 16:06:47 IP:61.228.xxx.xxx 未訂閱
使用 MSSQL DATABASE RM_040102 RM_040506 RM_040708 RM_040910 RM_041112 RM_050102........[_050102] 後面是變數 var D:STRING S:????? D:='040102' // 變數 S:=RM_ D // rm_040102 ADOQuery1.SQL.Clear; ADOQuery1.SQL.Add('select * from ??s?? '); if ADOQuery1.Active = False then begin try ADOQuery1.Active:=True; except ADOQuery1.SQL.Clear; ADOQuery1.SQL.Add('Create Table ??s??(cp_no varchar(3) not null , '); ADOQuery1.SQL.Add('product_no varchar(6) not null ,cp_qty numeric (9,0) default(0) null ) on [Primary]'); end; end; 請問可有方法解決變數代入用SQL嗎? select * from ??s?? Create table ??s??(......
timhuang
尊榮會員


發表:78
回覆:1815
積分:1608
註冊:2002-07-15

發送簡訊給我
#2 引用回覆 回覆 發表時間:2004-03-20 21:58:10 IP:61.62.xxx.xxx 未訂閱
其實就是檢查資料表是否存在後, 再進行建立資料表吧. 你可以不需要使用 try... except ... end 的方式來進行檢查, 因為有更直接檢驗資料表是否存在的語法. 建議你可以這樣做:    
var
  S, D: string;
begin
  S := 'RM_';
  D := '040102';
  S := S + D;  // assebmle the table name      // check if table if exists , if not exists then create a table
  ADOQuery1.SQL.Clear;
  ADOQuery1.SQL.Text :=
  'if not exists (select * from dbo.sysobjects where id = object_id(N'[dbo].['+S+']') and OBJECTPROPERTY(id, N'IsUserTable') = 1) '+
  'begin '+
  '  CREATE TABLE [dbo].['+S+'] ( '+
  '    cp_no varchar(3) not null , '+
  '    product_no varchar(6) not null, '+
  '    cp_qty numeric(9,0) default(0) null '+
  '  ) ON [PRIMARY] '+
  'end ';
  ADOQuery1.ExecSQL;      // open the table
  ADOQuery1.Close;
  ADOQuery1.SQL.Text := 'select * from '+S;
  ADOQuery1.Open;
end;
nick167
中階會員


發表:86
回覆:133
積分:53
註冊:2003-02-12

發送簡訊給我
#3 引用回覆 回覆 發表時間:2004-03-22 08:49:49 IP:61.228.xxx.xxx 未訂閱
引言: 其實就是檢查資料表是否存在後, 再進行建立資料表吧. 你可以不需要使用 try... except ... end 的方式來進行檢查, 因為有更直接檢驗資料表是否存在的語法. 建議你可以這樣做:
var
  S, D: string;
begin
  S := 'RM_';
  D := '040102';
  S := S + D;  // assebmle the table name      // check if table if exists , if not exists then create a table
  ADOQuery1.SQL.Clear;
  ADOQuery1.SQL.Text :=
  'if not exists (select * from dbo.sysobjects where id = object_id(N'[dbo].['+S+']') and OBJECTPROPERTY(id, N'IsUserTable') = 1) '+
  'begin '+
  '  CREATE TABLE [dbo].['+S+'] ( '+
  '    cp_no varchar(3) not null , '+
  '    product_no varchar(6) not null, '+
  '    cp_qty numeric(9,0) default(0) null '+
  '  ) ON [PRIMARY] '+
  'end ';
  ADOQuery1.ExecSQL;      // open the table
  ADOQuery1.Close;
  ADOQuery1.SQL.Text := 'select * from '+S;
  ADOQuery1.Open;
end;
此行 'if not exists (select * from dbo.sysobjects where id = object_id(N'[dbo].['+S+']') and OBJECTPROPERTY(id, N'IsUserTable') = 1) '+ 會出現 ERROR MESSAGE Thank [Error] Unit1.pas(38): Undeclared identifier: 'dbo'
timhuang
尊榮會員


發表:78
回覆:1815
積分:1608
註冊:2002-07-15

發送簡訊給我
#4 引用回覆 回覆 發表時間:2004-03-22 09:40:17 IP:203.95.xxx.xxx 未訂閱
sorry! 沒有注意到 single quote 的問題, 修改一下: (就是將在 ' '(字串) 中的 '(single quote) 改為 ''(兩個 single quote)!!    
var
  S, D: string;
begin
  S := 'RM_';
  D := '040102';
  S := S + D;  // assebmle the table name      // check if table if exists , if not exists then create a table
  ADOQuery1.SQL.Clear;
  ADOQuery1.SQL.Text :=
  'if not exists (select * from dbo.sysobjects where id = object_id(N''[dbo].['+S+']'') and OBJECTPROPERTY(id, N''IsUserTable'') = 1) '+
  'begin '+
  '  CREATE TABLE [dbo].['+S+'] ( '+
  '    cp_no varchar(3) not null , '+
  '    product_no varchar(6) not null, '+
  '    cp_qty numeric(9,0) default(0) null '+
  '  ) ON [PRIMARY] '+
  'end ';
  ADOQuery1.ExecSQL;      // open the table
  ADOQuery1.Close;
  ADOQuery1.SQL.Text := 'select * from '+S;
  ADOQuery1.Open;
end;
nick167
中階會員


發表:86
回覆:133
積分:53
註冊:2003-02-12

發送簡訊給我
#5 引用回覆 回覆 發表時間:2004-03-22 10:35:17 IP:61.228.xxx.xxx 未訂閱
謝謝 timhuang 可以了
系統時間:2024-05-19 4:44:30
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!