一个关于动态建表的问题(50分)

  • 主题发起人 主题发起人 heihei_76
  • 开始时间 开始时间
H

heihei_76

Unregistered / Unconfirmed
GUEST, unregistred user!
大家好!我在动态建表的时候,在批处理sql脚本的时候,遇到不能批处理动态建视图
的问题,提示go处有语法错误或者create view查询必须放在第一行的问题,
例:
sql.txt
CREATE TABLE [dbo].[Instead_Kind] (
[instead_code] [varchar] (4) NOT NULL ,
[reason] [varchar] (50) NULL ,
[instead_start] [datetime] NULL ,
[instead_day] [numeric](18, 0) NULL
) ON [PRIMARY]
GO

CREATE TABLE [dbo].[Instead_record] (
[user_id] [varchar] (4) NOT NULL ,
[instead_code] [varchar] (4) NULL ,
[instead_time] [datetime] NULL
) ON [PRIMARY]
GO

CREATE TABLE [dbo].[Member_infor] (
[user_id] [varchar] (4) NOT NULL ,
[name] [varchar] (10) NULL ,
[department_id] [varchar] (4) NULL ,
[level_code] [varchar] (4) NULL ,
[password] [varchar] (10) NULL
) ON [PRIMARY]
GO

CREATE TABLE [dbo].[MoreWork_Require] (
[department_id] [varchar] (4) NOT NULL ,
[decison] [varchar] (50) NULL ,
[special_start] [datetime] NULL ,
[special_end] [datetime] NULL
) ON [PRIMARY]
GO

CREATE TABLE [dbo].[Statistics_timeset] (
[start_date] [int] NULL ,
[end_date] [int] NULL
) ON [PRIMARY]
GO

CREATE TABLE [dbo].[WorkTime_Set] (
[starttime] [datetime] NULL ,
[latetime] [datetime] NULL ,
[emptytime] [datetime] NULL ,
[type] [varchar] (4) NOT NULL
) ON [PRIMARY]
GO

CREATE TABLE [dbo].[WorkerRight] (
[ID] [varchar] (10) NULL ,
[FunID] [varchar] (10) NULL
) ON [PRIMARY]
GO

CREATE TABLE [dbo].[configuration] (
[id] [int] NOT NULL ,
[kind] [int] NULL ,
[explain] [varchar] (50) NULL ,
[sashfont] [varchar] (50) NULL ,
[sashfontsize] [char] (10) NULL ,
[sashfontcolor] [char] (10) NULL ,
[buttonfont] [varchar] (50) NULL ,
[buttonfontsize] [varchar] (50) NULL ,
[buttonfontcolor] [char] (10) NULL ,
[sashbgcolor] [char] (10) NULL ,
[buttonbgcolor] [char] (10) NULL ,
[defaultname] [varchar] (50) NULL ,
[flag] [int] NULL ,
[imgName] [varchar] (50) NULL ,
[tablebgcolor] [varchar] (50) NULL ,
[tableheadbgcolor] [varchar] (50) NULL ,
[tableheadfont] [varchar] (50) NULL ,
[tableheadfontsize] [char] (10) NULL ,
[tablefont] [varchar] (50) NULL ,
[tablefontSize] [char] (10) NULL ,
[tablefontcolor] [varchar] (50) NULL ,
[tableoddBgcolor] [varchar] (50) NULL ,
[tableoddOvercolor] [varchar] (50) NULL ,
[tableevenBgcolor] [varchar] (50) NULL ,
[tableevenOvercolor] [varchar] (50) NULL ,
[tableborder] [int] NULL ,
[tablebordercolor] [varchar] (50) NULL ,
[tablebordercolorlight] [varchar] (50) NULL ,
[tablebordercolordark] [varchar] (50) NULL ,
[tablewidth] [int] NULL ,
[tablecellspacing] [int] NULL ,
[tablecellpadding] [int] NULL ,
[tableheadfontcolor] [varchar] (50) NULL ,
[tablerows] [int] NULL
) ON [PRIMARY]
GO

CREATE TABLE [dbo].[department_infor] (
[department_id] [varchar] (4) NOT NULL ,
[department_name] [varchar] (50) NULL ,
[director] [varchar] (20) NULL ,
[user_id] [varchar] (4) NULL
) ON [PRIMARY]
GO

CREATE TABLE [dbo].[department_total] (
[department_id] [varchar] (4) NULL ,
[user_id] [varchar] (4) NULL ,
[normal] [int] NULL ,
[late] [int] NULL ,
[empty] [int] NULL ,
[total_miss] [int] NULL ,
[jia1] [int] NULL ,
[jia2] [int] NULL ,
[jia3] [int] NULL ,
[jia4] [int] NULL ,
[jia5] [int] NULL ,
[jia6] [int] NULL ,
[jia13] [int] NULL ,
[jia9] [int] NULL ,
[jia10] [int] NULL
) ON [PRIMARY]
GO

CREATE TABLE [dbo].[kaoqin_Kind] (
[kaoqin_type] [varchar] (4) NOT NULL ,
[kaoqin_reason] [varchar] (50) NULL
) ON [PRIMARY]
GO

CREATE TABLE [dbo].[level_code] (
[level_code] [varchar] (4) NOT NULL ,
[level_name] [varchar] (20) NOT NULL
) ON [PRIMARY]
GO

CREATE TABLE [dbo].[menu] (
[mNo] [int] NOT NULL ,
[FunID] [char] (7) NOT NULL ,
[Father] [char] (6) NULL ,
[Father2] [char] (6) NULL ,
[value] [varchar] (100) NULL ,
[HTTP] [varchar] (100) NULL ,
[Target] [char] (10) NULL ,
[Layer] [char] (6) NULL ,
[StatusBar] [varchar] (50) NULL ,
[imgPath] [varchar] (100) NULL ,
[s0verImgpath1] [varchar] (100) NULL ,
[showFlag] [bit] NULL
) ON [PRIMARY]
GO

CREATE TABLE [dbo].[month_total] (
[userid] [varchar] (4) NULL ,
[monthdate] [datetime] NULL ,
[amkaoqin] [varchar] (10) NULL ,
[pmkaoqin] [varchar] (10) NULL ,
[username] [varchar] (10) NULL
) ON [PRIMARY]
GO

CREATE TABLE [dbo].[morework_Record] (
[user_id] [varchar] (4) NOT NULL ,
[start_time] [datetime] NULL ,
[end_time] [datetime] NULL
) ON [PRIMARY]
GO

CREATE TABLE [dbo].[work_Record] (
[user_id] [varchar] (4) NOT NULL ,
[kaoqin_time] [datetime] NULL ,
[endtime] [datetime] NULL ,
[kaoqin_type] [varchar] (20) NULL ,
[loginIP] [varchar] (20) NULL
) ON [PRIMARY]
GO

CREATE TABLE [dbo].[work_date] (
[dayofyear] [datetime] NULL ,
[am] [bit] NULL ,
[pm] [bit] NULL
) ON [PRIMARY]
GO

ALTER TABLE [dbo].[Member_infor] WITH NOCHECK ADD
CONSTRAINT [PK_Member_infor] PRIMARY KEY CLUSTERED
(
[user_id]
) ON [PRIMARY]
GO

ALTER TABLE [dbo].[WorkTime_Set] WITH NOCHECK ADD
CONSTRAINT [PK_WorkTimeSet] PRIMARY KEY CLUSTERED
(
[type]
) ON [PRIMARY]
GO

ALTER TABLE [dbo].[department_infor] WITH NOCHECK ADD
CONSTRAINT [PK_department_infor] PRIMARY KEY CLUSTERED
(
[department_id]
) ON [PRIMARY]
GO

SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO

CREATE VIEW dbo.VIEW_department_total
AS
SELECT a.user_id, b.name, a.department_id, c.department_name, a.normal, a.late,
a.empty, a.total_miss, a.jia10, a.jia9, a.jia13, a.jia6, a.jia5, a.jia4, a.jia3, a.jia2,
a.jia1
FROM dbo.department_total a INNER JOIN
dbo.Member_infor b ON a.user_id = b.user_id INNER JOIN
dbo.department_infor c ON a.department_id = c.department_id

GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO

CREATE VIEW dbo.VIEW_member_infor
AS
SELECT a.user_id, a.name, a.department_id, a.level_code, a.password,
b.department_name, c.level_name, b.director, b.user_id AS director_id
FROM dbo.Member_infor a INNER JOIN
dbo.department_infor b ON a.department_id = b.department_id INNER JOIN
dbo.level_code c ON a.level_code = c.level_code

GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO

CREATE VIEW dbo.VIEW_work_record
AS
SELECT dbo.Member_infor.name, dbo.kaoqin_Kind.kaoqin_reason,
dbo.work_Record.kaoqin_time, dbo.work_Record.endtime, dbo.work_Record.loginIP,
dbo.work_Record.user_id, dbo.kaoqin_Kind.kaoqin_type
FROM dbo.work_Record INNER JOIN
dbo.Member_infor ON
dbo.work_Record.user_id = dbo.Member_infor.user_id INNER JOIN
dbo.kaoqin_Kind ON dbo.work_Record.kaoqin_type = dbo.kaoqin_Kind.kaoqin_type

GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO

在delphi中
memo1.Lines.LoadFromFile(sql.txt);
adoquery1.sql.add(memo1.txt);
就会出现go处有语法错误(但在分析查询器中没问题)
把go全部删除又会出现
create view查询必须放在第一行的问题
请问这是什么问题?该如果解决?谢谢!
 
delphi的sql不支持go语法。
create view是必须在第一行。
找不到好的方法,create view只能多次建!
adoquery1.close;
adoquery1.sql.clear;
adoquery1.sql.add('...')
adoquery1.execsql;

adoquery1.close;
adoquery1.sql.clear;
adoquery1.sql.add('...')
adoquery1.execsql;
 
自己分析sql文件,提取go以外的sql一句一句执行

-----
http://www.8421.org
 
如果视图比较多的话不是很麻烦吗?有别的什么方法吗?
 
qdyoung的办法可行

打开文件
while not EOF do
begin
if 当前行<>'GO' then
Query1.Add(当前行)
else
begin
Query1.ExecSQL;
Query1.Close;
Query1.SQL.Clear;
end;
end

估计SQL查询分析器也是这么做的:)
 

Similar threads

I
回复
0
查看
612
import
I
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
后退
顶部