sqlserver有什么语句或函数我不太清楚,我自己写了一个,你可以试试,不过只能在2000上使用,7.0上得去掉字段的描述属性。select rtrim(b.name) as colname
,case when h.id is not null then 'PK' else '' end as primarykey
,type_name(b.xusertype) + case when b.colstat & 1 = 1 then '[ID(' + convert(varchar,ident_seed(a.name)) + ',' + convert(varchar,ident_incr(a.name)) + ')]' else '' end as type
,b.length
,case b.isnullable when 0 then 'N' else 'Y' end as [isnull]
,isnull(e.text,'') as [default]
,isnull(c.value,'') as descript 
from sysobjects a,syscolumns b 
left outer join sysproperties c on b.id = c.id and b.colid = c.smallid 
left outer join syscomments e on b.cdefault = e.id
left outer join (select g.id,g.colid from sysindexes f,sysindexkeys g where f.id = g.id and f.indid = g.indid and f.indid > 0 and f.indid < 255 and (f.status & 2048)<>0) h on b.id = h.id and b.colid = h.colid
where a.id = b.id
and a.id = object_id('tablename') --tablename改成你要导出的表的名称
order by b.colid

解决方案 »

  1.   

    運行:
    exec up_getTableStruct 你的表名
    即可得到你想要的結果.up_getTableStruct過程如下: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 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
      

  2.   

    運行:
    exec up_getTableStruct 你的表名
    即可得到你想要的結果.up_getTableStruct過程如下: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 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
      

  3.   

    请问怎样得到up_getTableStruct的表结构的值?
      

  4.   

    这位仁兄怎么这么多问题?而且只是拼命问,给了解答也不说明白还是不明白,这样让别人怎么给你解答呢?
    还有“识别字”是什么属性值?怎么我不知道还有这个属性,能不能说得明白点!
    竹兄的取得表结构的存储过程真是精品,运行就能得到创建该表的语句,我已经收藏了。
    不过对于“请问怎样得到up_getTableStruct的表结构的值?”这个问题,真是不知道怎么回答好。
    我想怎么导入存储过程的问题,其实也应该是怎么创建这个存储过程吧!直接把“up_getTableStruct過程如下:”之后的sql语句拷到sqlserver的查询分析器中,运行就行了。
    之后再在查询分析器中输入up_getTableStruct 'yourtable'就能得到表yourtable的表结构了。
      

  5.   

    up_getTableStruct我是明白的,其实我是想是怎么在程序里建立存储过程(如vb),而不是在查询分析器中,希望能帮帮我
      

  6.   


    在SQL Server中建好該存儲過程.
    然後在VB中調用不就可以了???
      

  7.   

    用SQLDMO可以做。
    比如用VB导出某个表的SQL语句(注意要在VB中引用SQLDMO):
    Dim oSQLSERVER As New SQLDMO.SQLServer
    oSQLSERVER.Connect "myserver", "sa", ""
    Dim oTable As New SQLDMO.Table
    Set oTable= bbb.Tables("t_bill")
    oTable.Script SQLDMOScript_Default, "c:\abc.sql", "", SQLDMOScript2_UnicodeFile
    set oTable=nothing
    oSQLSERVER.close
    set oSQLSERVER=nothing不知道你是不是要这个?上面的方法也很好,存储过程是在数据库里建好了的。你用时只需调用就行了。
      

  8.   

    上面粘错了:P
    Dim oSQLSERVER As New SQLDMO.SQLServer
    oSQLSERVER.Connect "myserver", "sa", ""
    Dim oTable As New SQLDMO.Table
    Set oTable= oSQLSERVER.Tables("t_bill")
    oTable.Script SQLDMOScript_Default, "c:\abc.sql", "", SQLDMOScript2_UnicodeFile
    set oTable=nothing
    oSQLSERVER.close
    set oSQLSERVER=nothing
      

  9.   

    按钮的方法很好,我想是不是可以通过ado的connection对象直接执行创建存储过程的sql语句来实现。我没有试过,但我估计应该可以。
      

  10.   

    要注册什么才可以创建SQLDMO对象啊?