谁能提供一些自动生成sql查询语句的资料?类似sql server管理器查询界面那样,可以根据用户操作自动生成sql语句。

top_abrams 2003-03-30 04:21:57
多谢。
...全文
69 点赞 收藏 3
写回复
3 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
top_abrams 2003-03-31
非常感谢个各位的解答,但是如果用户操作涉及到表的内联,外联,以及sum , group by 之类的东西,如何自动生成呢?
回复
pengdali 2003-03-30
这还有一个,你搜索一下有很多的!
转贴另一个:

create procedure up_getTableStruct
@v_tableName varchar(256)
as
declare @i_objectId int, -- 對象id
@i_indId smallint, -- 索引id
@v_pkInfo varchar(100), -- 主鍵信息
@v_clusteredInfo varchar(20), -- clustered信息
@v_pkCol varchar(100), -- 主鍵字段
@v_key varchar(50),
@i_i smallint
set @i_objectId = object_id(@v_tableName)
if @i_objectId is null -- 判斷對象是否存在
begin
print 'The object not exists'
return
end
if OBJECTPROPERTY(@i_objectId,'IsTable') <> 1 -- 判斷對象是否是table
begin
print 'The object is not table'
return
end

create table #temp1
(
i_id int identity,
v_desc varchar(200)
)

insert into #temp1(v_desc)
values('create table '+@v_tableName+'(') --

insert into #temp1(v_desc) -- 將表的字段信息存入臨時表
select a.name+space(4)+b.name+
case when b.xtype in (167,175,231,239) then '('+cast(a.length as varchar)+')'
when b.xtype in (106,108) then '('+cast(a.xprec as varchar)+','+cast(a.xscale as varchar)+')'
else '' end+space(4)+
case when (a.colstat & 1 = 1) then 'identity('+cast(ident_seed(@v_tableName) as varchar)+',' +
cast(ident_incr(@v_tableName) as varchar)+')' else '' end +space(4)+
case a.isnullable when 0 then 'not null' else 'null' end+'|'
from syscolumns a,systypes b
where a.id = @i_objectId and a.xtype = b.xusertype
order by a.colid

create procedure 名
@aa varchar(10)
as
begin
select a.id 编号,a.name 名称,b.id,b.name from table1 a join table2 b on a.id=b.id where
end

if exists(select 1 from sysobjects where parent_obj = @i_objectId and xtype = 'PK') -- 如果存在主鍵
begin
select @v_pkInfo = b.name,@i_indId = indid, -- 得到主鍵名,id及是否clustered信息
@v_clusteredInfo = (case when (a.status & 16)=16 then 'clustered' else 'nonclustered' end )
from sysindexes a,sysobjects b
where a.id = b.parent_obj and a.name = b.name and b.xtype = 'PK' and b.parent_obj = @i_objectId

select @v_pkCol = index_col(@v_tableName, @i_indId, 1), @i_i = 2 -- 得到主鍵的第1個字段名
select @v_key = index_col(@v_tableName, @i_indId, @i_i) -- 得到主鍵的第2個字段名
while (@v_key is not null)
begin
select @v_pkCol = @v_pkCol + ',' + @v_key, @i_i = @i_i + 1
select @v_key = index_col(@v_tableName, @i_indId, @i_i) -- 得到主鍵的第@i_i個字段名
end -- 組合成主鍵信息
set @v_pkInfo = 'constraint '+@v_pkInfo+' primary key '+@v_clusteredInfo+'('+@v_pkCol+')'
insert into #temp1(v_desc) values(@v_pkInfo) -- 將主鍵信息插入臨時表
end
else
begin
select @i_i = count(1) from #temp1
-- 如果沒有主鍵,那麼將最後一筆紀錄的'|'去掉
update #temp1 set v_desc = replace(v_desc,'|','') where i_id = @i_i
end

insert into #temp1(v_desc) values(')') --
update #temp1 set v_desc = replace(v_desc,'|',',')

select v_desc from #temp1 order by i_id

drop table #temp1
回复
Lostinet 2003-03-30

我做过一个。
在这里我只能把设计的Schema帖出来。


<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="QueryData" targetNamespace="http://tempuri.org/QueryData.xsd" elementFormDefault="qualified" attributeFormDefault="qualified" xmlns="http://tempuri.org/QueryData.xsd" xmlns:mstns="http://tempuri.org/QueryData.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="QueryData" msdata:IsDataSet="true">
<xs:complexType>
<xs:choice maxOccurs="unbounded">
<xs:element name="Solution" minOccurs="1" maxOccurs="1">
<xs:complexType>
<xs:sequence>
<xs:element name="Field" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element name="Condition" minOccurs="0">
<xs:complexType>
<xs:sequence />
<xs:attribute name="Op" type="xs:string" use="required" />
<xs:attribute name="Value" type="xs:string" use="required" />
<xs:attribute name="ValueType" type="xs:string" use="required" />
<xs:attribute name="Hidden" type="xs:boolean" use="required" />
<xs:attribute name="UseAnd" type="xs:boolean" use="required" />
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="DisplayName" type="xs:string" use="required" />
<xs:attribute name="Description" type="xs:string" use="required" />
<xs:attribute name="Expression" type="xs:string" use="required" />
<xs:attribute name="DataType" type="xs:string" use="required" />
<xs:attribute name="ChoiceDistinct" type="xs:boolean" use="required" />
<xs:attribute name="Statistical" use="required" type="xs:boolean">
</xs:attribute>
<xs:attribute name="Display" type="xs:boolean" use="required" />
<xs:attribute name="UseSort" type="xs:boolean" use="required" />
<xs:attribute name="SortOrder" type="xs:int" use="required" />
<xs:attribute name="SortAsc" type="xs:boolean" use="required" />
<xs:attribute name="GroupMethod" type="xs:string" use="required" />
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="TableList" type="xs:string" use="required" />
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
回复
相关推荐
发帖
疑难问题
创建于2007-09-28

2.1w+

社区成员

MS-SQL Server 疑难问题
申请成为版主
帖子事件
创建了帖子
2003-03-30 04:21
社区公告
暂无公告