Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在SQL Server 2008中查找用于创建表的查询_Sql_Sql Server - Fatal编程技术网

如何在SQL Server 2008中查找用于创建表的查询

如何在SQL Server 2008中查找用于创建表的查询,sql,sql-server,Sql,Sql Server,是否有方法显示用于创建表的查询?例如,有一个名为CLIENT的表,我想查看用于创建此表的查询。如何执行此操作?右键单击SSMS中的表,然后选择“脚本表>作为创建”右键单击SSMS中的表,然后选择“脚本表>作为创建” 右键单击表格 将表脚本设置为 创造 新建查询编辑器窗口 编辑:该死的,你必须在这里很快的简单代表!哈哈 右键单击表格 将表脚本设置为 创造 新建查询编辑器窗口 编辑:该死的,你必须在这里很快的简单代表!哈哈。如果你想从某个脚本中完成它,那么它就在这里 declare @table

是否有方法显示用于创建表的查询?例如,有一个名为CLIENT的表,我想查看用于创建此表的查询。如何执行此操作?

右键单击SSMS中的表,然后选择“脚本表>作为创建”

右键单击SSMS中的表,然后选择“脚本表>作为创建”

  • 右键单击表格
  • 将表脚本设置为
  • 创造
  • 新建查询编辑器窗口
  • 编辑:该死的,你必须在这里很快的简单代表!哈哈

  • 右键单击表格
  • 将表脚本设置为
  • 创造
  • 新建查询编辑器窗口

  • 编辑:该死的,你必须在这里很快的简单代表!哈哈。

    如果你想从某个脚本中完成它,那么它就在这里

    declare @table varchar(100)
    set @table = 'client_table' -- set table name here
    declare @sql table(s varchar(1000), id int identity)
    
    -- create statement
    insert into  @sql(s) values ('create table [' + @table + '] (')
    
    -- column list
    insert into @sql(s)
    select 
        '  ['+column_name+'] ' + 
        data_type + coalesce('('+cast(character_maximum_length as varchar)+')','') + ' ' +
        case when exists ( 
            select id from syscolumns
            where object_name(id)=@table
            and name=column_name
            and columnproperty(id,name,'IsIdentity') = 1 
        ) then
            'IDENTITY(' + 
            cast(ident_seed(@table) as varchar) + ',' + 
            cast(ident_incr(@table) as varchar) + ')'
        else ''
        end + ' ' +
        ( case when IS_NULLABLE = 'No' then 'NOT ' else '' end ) + 'NULL ' + 
        coalesce('DEFAULT '+COLUMN_DEFAULT,'') + ','
    
     from information_schema.columns where table_name = @table
     order by ordinal_position
    
    -- primary key
    declare @pkname varchar(100)
    select @pkname = constraint_name from information_schema.table_constraints
    where table_name = @table and constraint_type='PRIMARY KEY'
    
    if ( @pkname is not null ) begin
        insert into @sql(s) values('  PRIMARY KEY (')
        insert into @sql(s)
            select '   ['+COLUMN_NAME+'],' from information_schema.key_column_usage
            where constraint_name = @pkname
            order by ordinal_position
        -- remove trailing comma
        update @sql set s=left(s,len(s)-1) where id=@@identity
        insert into @sql(s) values ('  )')
    end
    else begin
        -- remove trailing comma
        update @sql set s=left(s,len(s)-1) where id=@@identity
    end
    
    -- closing bracket
    insert into @sql(s) values( ')' )
    
    -- result!
    select s from @sql order by id
    
    这将为您提供如下输出

    create table [client_table] (
      [colA] varchar(250)  NOT NULL DEFAULT (''),
      [colB] int NOT NULL DEFAULT(0)
    )
    

    如果你想从某个脚本开始,那么就在这里

    declare @table varchar(100)
    set @table = 'client_table' -- set table name here
    declare @sql table(s varchar(1000), id int identity)
    
    -- create statement
    insert into  @sql(s) values ('create table [' + @table + '] (')
    
    -- column list
    insert into @sql(s)
    select 
        '  ['+column_name+'] ' + 
        data_type + coalesce('('+cast(character_maximum_length as varchar)+')','') + ' ' +
        case when exists ( 
            select id from syscolumns
            where object_name(id)=@table
            and name=column_name
            and columnproperty(id,name,'IsIdentity') = 1 
        ) then
            'IDENTITY(' + 
            cast(ident_seed(@table) as varchar) + ',' + 
            cast(ident_incr(@table) as varchar) + ')'
        else ''
        end + ' ' +
        ( case when IS_NULLABLE = 'No' then 'NOT ' else '' end ) + 'NULL ' + 
        coalesce('DEFAULT '+COLUMN_DEFAULT,'') + ','
    
     from information_schema.columns where table_name = @table
     order by ordinal_position
    
    -- primary key
    declare @pkname varchar(100)
    select @pkname = constraint_name from information_schema.table_constraints
    where table_name = @table and constraint_type='PRIMARY KEY'
    
    if ( @pkname is not null ) begin
        insert into @sql(s) values('  PRIMARY KEY (')
        insert into @sql(s)
            select '   ['+COLUMN_NAME+'],' from information_schema.key_column_usage
            where constraint_name = @pkname
            order by ordinal_position
        -- remove trailing comma
        update @sql set s=left(s,len(s)-1) where id=@@identity
        insert into @sql(s) values ('  )')
    end
    else begin
        -- remove trailing comma
        update @sql set s=left(s,len(s)-1) where id=@@identity
    end
    
    -- closing bracket
    insert into @sql(s) values( ')' )
    
    -- result!
    select s from @sql order by id
    
    这将为您提供如下输出

    create table [client_table] (
      [colA] varchar(250)  NOT NULL DEFAULT (''),
      [colB] int NOT NULL DEFAULT(0)
    )
    

    谢谢湿婆!在做了好几年的水蛭后才开始这么做。谢谢湿婆!在做了好几年的水蛭之后,才开始这么做。