惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

宝玉的分享
宝玉的分享
NISL@THU
NISL@THU
E
Exploit-DB.com RSS Feed
L
LINUX DO - 热门话题
L
Lohrmann on Cybersecurity
K
Kaspersky official blog
Project Zero
Project Zero
Cisco Talos Blog
Cisco Talos Blog
T
The Exploit Database - CXSecurity.com
P
Palo Alto Networks Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
T
Threatpost
S
Schneier on Security
G
GRAHAM CLULEY
The Hacker News
The Hacker News
T
Threat Research - Cisco Blogs
Scott Helme
Scott Helme
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
P
Privacy & Cybersecurity Law Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
Cyberwarzone
Cyberwarzone
C
CERT Recently Published Vulnerability Notes
T
Tor Project blog
AWS News Blog
AWS News Blog
Simon Willison's Weblog
Simon Willison's Weblog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
爱范儿
爱范儿
P
Privacy International News Feed
云风的 BLOG
云风的 BLOG
P
Proofpoint News Feed
S
Securelist
G
Google Developers Blog
The Last Watchdog
The Last Watchdog
Google Online Security Blog
Google Online Security Blog
美团技术团队
F
Fortinet All Blogs
小众软件
小众软件
Recorded Future
Recorded Future
V
Visual Studio Blog
B
Blog RSS Feed
H
Help Net Security
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Google DeepMind News
Google DeepMind News
Blog — PlanetScale
Blog — PlanetScale
博客园 - 聂微东
Stack Overflow Blog
Stack Overflow Blog
Martin Fowler
Martin Fowler
Latest news
Latest news
Spread Privacy
Spread Privacy
H
Heimdal Security Blog

博客园 - 董晓涛

如何将存储过程执行后的结果集放入临时表 Detect SQL timeout from ASP & Issue RollbackTrans SQL Server 2005 symmetric encrytion sample 如何解决:Error 14274: 无法添加、更新或删除从MSX服务器上发起的作业. Useful Links(to Learn SQL Server) Encrypting Data With the SQL Server Encrypt Function Convert IP To Numberic Generate Time Dim Generate Insert data script on a table. - 董晓涛 Execute T-SQL asynchronously Don't Use Select * Sql Server数据库被置疑后解决方法 SQL Server 2005中对BLOB的支持(ntext,text and image) SQL Server 2000 Service Pack 4 is released 数据规范化 Microsoft SQL Server 2005 and Vs.net 2005 April 2005 Version XML IN 20 MINUTES! CLR Integrated in SQL Server 2005 XQuery in SQL Server 2005
Generate table structure
董晓涛 · 2005-06-29 · via 博客园 - 董晓涛

-- --
 --exec Up_ExportTables 'Customers'
-- When you want to export a table structure to T-SQL, everyone always points you to Enterprise Manager, or a script that calls BCP.
-- Sometimes, a developer either doesn't have access to those tools, or just doesn't want to use them (like me) out of spite.
-- I made this T-SQL because it was educational, and noone else seems to have done it yet.
-- ideally, with this script AND the sp_generate_inserts script from http://vyaskn.tripod.com/code.htm, you could use a loop to export both the definition of a table, And then it's contents.
-- please, if you take this script and make it better, please email me a copy at lowell@stormrage.com so I can learn from your improvements.
--
-- Best to view the results in Query Analyzer Results in Text (Control +T)
--
-- Known limitations at this time:
-- 1.Doesn't add the check constraint's yet, only the implied PK AND Unique Constraints.
-- 2.Doesn't add the indexes for the table yet.
-- 3. Completely ignores the optional COLLATE statement because I never use it when creating a varchar; ie  COLLATE SQL_Latin1_General_CP1_CI_AS
 
CREATE PROCEDURE Up_ExportTables
-- USAGE: sp_ExportTables gmproj
@table_name    varchar(32)
as
begin

Create Table #CreateStatements    (uid int identity(1,1),Info    text)

DECLARE @table_id int,
    @CurrColumn    int,
    @MaxColumn    int,
    @CreateStatement    varchar(8000),
    @ColumnTypeName        varchar(255),
    @uid    int,
    @i    int,
    @primary_key_field varchar(50)

    select @table_id=id from sysobjects where xtype='U' and [name] <> 'dtproperties' and [name] = @table_name
    /*Since a table can have only one Primary key, get the column name for this table(if any) that is the PK*/
    select @primary_key_field = convert(varchar(32),c.name)
        from
         sysindexes i, syscolumns c, sysobjects o, syscolumns c1
        where
         o.id = @table_id
         and o.id = c.id
         and o.id = i.id
         and (i.status & 0x800) = 0x800
         and c.name = index_col (@table_name, i.indid, c1.colid)
         and c1.colid <= i.keycnt
         and c1.id = @table_id
    Select @CreateStatement = CHAR(13) + 'CREATE TABLE [' + [name] + '] ( ' from SYSOBJECTS WHERE ID=@TABLE_ID
    --For Each Column
    Select @CurrColumn=Min(colid),@MaxColumn = Max(colid) from syscolumns where id= @table_id
    --Select * from syscolumns where id=1511676433

    while @currColumn <= @MaxColumn
        begin
       
        --print @currColumn
        Declare @UQIndex int, @DefaultValue nvarchar(4000)
        set @DefaultValue = null
        select @DefaultValue=text from syscomments where id=
            (select constid from sysconstraints where id=@table_id and colid=@currColumn)

            --Process different Column Types differently
            SELECT @CreateStatement = @CreateStatement + CHAR(13) + '[' + [name] + '] ' + type_name(xtype) +
                case   
                    --ie numeric(10,2)
                    WHEN  type_name(xtype) IN ('decimal','numeric') THEN   
                        ' ('+ convert(varchar,prec) + ',' + convert(varchar,length) + ')'
                        + case when autoval is null then '' else ' IDENTITY(1,1)' end   
                        + CASE when isnullable=0 THEN  ' NOT NULL' ELSE ' NULL' END
                    --ie float(53)     
                    WHEN  type_name(xtype) IN ('float','real') THEN   
                        ' ('+ convert(varchar,prec) + ')'
                        + case when autoval is null then '' else ' IDENTITY(1,1)' end   
                        + CASE when isnullable=0 THEN  ' NOT NULL' ELSE ' NULL' END
                    --ie varchar(40)
                    WHEN  type_name(xtype) IN ('char','varchar','nchar','nvarchar') THEN
                        ' ('+ convert(varchar,length) + ')' 
                        + case when autoval is null then '' else ' IDENTITY(1,1)' end
                        + CASE when isnullable=0 THEN  ' NOT NULL' ELSE ' NULL' END
                    --ie int
                    ELSE
                        + case when autoval is null then '' else ' IDENTITY(1,1)' end
                        + CASE when isnullable=0 THEN  ' NOT NULL' ELSE ' NULL' END
                end
            --code to determine if 'PRIMARY KEY'
            + CASE when syscolumns.name = @primary_key_field THEN ' PRIMARY KEY' else '' END
            + CASE when @DefaultValue is null then ''
            ELSE
                CASE
                WHEN  type_name(xtype) IN ('decimal','numeric','float','real','bigint','int','smallint','tinyint','money','smallmoney') THEN
                    ' DEFAULT ' + convert(varchar,@DefaultValue)
                ELSE
                    ' DEFAULT ' + convert(varchar,@DefaultValue)
                END
            END
             + ',' from syscolumns where id=@table_id and colid=@CurrColumn

        Select @CurrColumn = @CurrColumn + 1
        end
        insert into #CreateStatements(Info) values(@CreateStatement)
        SELECT @CreateStatement=''
        select @uid=@@IDENTITY

        --CODE TO ADD ALL THE FOREIGN KEYS TO THE BOTTOM OF THE STATEMENT
        declare @cursorID    int
        declare c1 cursor for SELECT fkeyid from sysforeignkeys where fkeyid=@table_id
            open c1
            fetch next from c1 into @cursorID
           
            SELECT @CreateStatement=@CreateStatement +
            (select + CHAR(13) +'FOREIGN KEY (' +   [syscolumns].[name] + ') REFERENCES ' from syscolumns where id=fkeyid and colid =fkey) +
            (select (SELECT distinct [sysobjects].[name] from sysobjects where id=rkeyid) + '(' + [syscolumns].[name] + '),' from syscolumns where id=rkeyid and colid =rkey)
             from sysforeignkeys where fkeyid=@table_id
               
            close c1
            deallocate c1
        --CODE TO ADD ALL THE UNIQUE CONSTRAINTS TO THE BOTTOM OF THE DEFINITION.
        declare c1 cursor for select id from sysobjects where xtype='UQ' and parent_obj=@table_id
        open c1
        fetch next from c1 into @cursorID
            --adapted shamelessly from sp_helpconstraints
            while @@fetch_status >= 0
            begin
                declare @indid smallint
                SELECT @indid = indid,@CreateStatement=@CreateStatement + CHAR(13) + 'CONSTRAINT ' + object_name(@cursorID) + ' UNIQUE '
                 + case when (status & 16)=16 then ' CLUSTERED' else ' NONCLUSTERED' end
                from    sysindexes
                where    name = object_name(@cursorID) and id = @table_ID
                    declare @thiskey nvarchar(131), -- 128+3
                        @keys     nvarchar(2126) -- a specific size for MS for whatever reason
       
                    select @keys = index_col(@table_name, @indid, 1), @i = 2
                    if (indexkey_property(@table_id, @indid, 1, 'isdescending') = 1)
                        select @keys = @keys  + '(-)'
       
                    select @thiskey = index_col(@table_name, @indid, @i)
                    if ((@thiskey is not null) and (indexkey_property(@table_ID, @indid, @i, 'isdescending') = 1))
                        select @thiskey = @thiskey + '(-)'
       
                    while (@thiskey is not null)
                    begin
                        select @keys = @keys + ', ' + @thiskey, @i = @i + 1
                        select @thiskey = index_col(@table_name, @indid, @i)
                        if ((@thiskey is not null) and (indexkey_property(@table_ID, @indid, @i, 'isdescending') = 1))
                            select @thiskey = @thiskey + '(-)'
                    end
                    Select @CreateStatement=@CreateStatement + '(' + @keys + '),'
            fetch next from c1 into @cursorID
            end
        close c1
        deallocate c1
            --CODE TO ADD CHECK CONSTRAINTS TO THE BOTTOM OF THE DEFINITION?

            --CODE TO ADD INDEXES TO THE BOTTOM OF THE DEFINITION?

        --at this point, there is a trailing comma, or it blank
        DECLARE @ptrval binary(16),@txtlen INT
        if len(@CreateStatement) > 0
        BEGIN
            SELECT @ptrval = TEXTPTR(info) ,
            @txtlen = DATALENGTH(info)
               FROM #CreateStatements
                  WHERE uid=@uid
            UPDATETEXT #CreateStatements.info @ptrval @txtlen 0 @CreateStatement
        END
       
        --note that this trims the trailing comma from the end of the statement
        SELECT @ptrval = TEXTPTR(info) ,
        @txtlen = DATALENGTH(info) - 1
           FROM #CreateStatements
              WHERE uid=@uid
       
        SELECT @CreateStatement= ')'+ CHAR(13)    
        UPDATETEXT #CreateStatements.info @ptrval @txtlen 1 @CreateStatement

Select info from #CreateStatements
drop table #CreateStatements
end