<tfoot id='LEjJC'></tfoot>
<i id='LEjJC'><tr id='LEjJC'><dt id='LEjJC'><q id='LEjJC'><span id='LEjJC'><b id='LEjJC'><form id='LEjJC'><ins id='LEjJC'></ins><ul id='LEjJC'></ul><sub id='LEjJC'></sub></form><legend id='LEjJC'></legend><bdo id='LEjJC'><pre id='LEjJC'><center id='LEjJC'></center></pre></bdo></b><th id='LEjJC'></th></span></q></dt></tr></i><div id='LEjJC'><tfoot id='LEjJC'></tfoot><dl id='LEjJC'><fieldset id='LEjJC'></fieldset></dl></div>

<small id='LEjJC'></small><noframes id='LEjJC'>

<legend id='LEjJC'><style id='LEjJC'><dir id='LEjJC'><q id='LEjJC'></q></dir></style></legend>

      • <bdo id='LEjJC'></bdo><ul id='LEjJC'></ul>

        在 SQL Server 中,如何为给定的表生成 CREATE TABLE 语句?

        时间:2023-07-17
          <bdo id='Ylgkj'></bdo><ul id='Ylgkj'></ul>
                <i id='Ylgkj'><tr id='Ylgkj'><dt id='Ylgkj'><q id='Ylgkj'><span id='Ylgkj'><b id='Ylgkj'><form id='Ylgkj'><ins id='Ylgkj'></ins><ul id='Ylgkj'></ul><sub id='Ylgkj'></sub></form><legend id='Ylgkj'></legend><bdo id='Ylgkj'><pre id='Ylgkj'><center id='Ylgkj'></center></pre></bdo></b><th id='Ylgkj'></th></span></q></dt></tr></i><div id='Ylgkj'><tfoot id='Ylgkj'></tfoot><dl id='Ylgkj'><fieldset id='Ylgkj'></fieldset></dl></div>

                  <legend id='Ylgkj'><style id='Ylgkj'><dir id='Ylgkj'><q id='Ylgkj'></q></dir></style></legend>

                  <small id='Ylgkj'></small><noframes id='Ylgkj'>

                    <tbody id='Ylgkj'></tbody>

                  <tfoot id='Ylgkj'></tfoot>
                  本文介绍了在 SQL Server 中,如何为给定的表生成 CREATE TABLE 语句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我花了很多时间想出解决这个问题的方法,所以本着 这篇文章,我把它贴在这里,因为我认为它可能对其他人有用.

                  I've spent a good amount of time coming up with solution to this problem, so in the spirit of this post, I'm posting it here, since I think it might be useful to others.

                  如果有人有更好的脚本或要添加的任何内容,请发布.

                  If anyone has a better script, or anything to add, please post it.

                  是的,伙计们,我知道如何在 Management Studio 中执行此操作 - 但我需要能够从另一个应用程序中执行此操作.

                  Yes guys, I know how to do it in Management Studio - but I needed to be able to do it from within another application.

                  推荐答案

                  我修改了上面的版本以运行所有表并支持新的 SQL 2005 数据类型.它还保留主键名称.仅适用于 SQL 2005(使用交叉应用).

                  I've modified the version above to run for all tables and support new SQL 2005 data types. It also retains the primary key names. Works only on SQL 2005 (using cross apply).

                  
                  select  'create table [' + so.name + '] (' + o.list + ')' + CASE WHEN tc.Constraint_Name IS NULL THEN '' ELSE 'ALTER TABLE ' + so.Name + ' ADD CONSTRAINT ' + tc.Constraint_Name  + ' PRIMARY KEY ' + ' (' + LEFT(j.List, Len(j.List)-1) + ')' END
                  from    sysobjects so
                  cross apply
                      (SELECT 
                          '  ['+column_name+'] ' + 
                          data_type + case data_type
                              when 'sql_variant' then ''
                              when 'text' then ''
                              when 'ntext' then ''
                              when 'xml' then ''
                              when 'decimal' then '(' + cast(numeric_precision as varchar) + ', ' + cast(numeric_scale as varchar) + ')'
                              else coalesce('('+case when character_maximum_length = -1 then 'MAX' else cast(character_maximum_length as varchar) end +')','') end + ' ' +
                          case when exists ( 
                          select id from syscolumns
                          where object_name(id)=so.name
                          and name=column_name
                          and columnproperty(id,name,'IsIdentity') = 1 
                          ) then
                          'IDENTITY(' + 
                          cast(ident_seed(so.name) as varchar) + ',' + 
                          cast(ident_incr(so.name) as varchar) + ')'
                          else ''
                          end + ' ' +
                           (case when UPPER(IS_NULLABLE) = 'NO' then 'NOT ' else '' end ) + 'NULL ' + 
                            case when information_schema.columns.COLUMN_DEFAULT IS NOT NULL THEN 'DEFAULT '+ information_schema.columns.COLUMN_DEFAULT ELSE '' END + ', ' 
                  
                       from information_schema.columns where table_name = so.name
                       order by ordinal_position
                      FOR XML PATH('')) o (list)
                  left join
                      information_schema.table_constraints tc
                  on  tc.Table_name       = so.Name
                  AND tc.Constraint_Type  = 'PRIMARY KEY'
                  cross apply
                      (select '[' + Column_Name + '], '
                       FROM   information_schema.key_column_usage kcu
                       WHERE  kcu.Constraint_Name = tc.Constraint_Name
                       ORDER BY
                          ORDINAL_POSITION
                       FOR XML PATH('')) j (list)
                  where   xtype = 'U'
                  AND name    NOT IN ('dtproperties')
                  
                  

                  更新:添加了对 XML 数据类型的处理

                  Update: Added handling of the XML data type

                  更新 2: 修复了以下情况:1) 有多个同名但架构不同的表,2) 有多个具有相同名称的 PK 约束的表

                  Update 2: Fixed cases when 1) there is multiple tables with the same name but with different schemas, 2) there is multiple tables having PK constraint with the same name

                  这篇关于在 SQL Server 中,如何为给定的表生成 CREATE TABLE 语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Integrated Security = True 和 Integrated Security = SSPI 有什么区 下一篇:如何使用 sqlcmd 从 SQL Server 将数据导出为 CSV 格式?

                  相关文章

                  <legend id='F2yuJ'><style id='F2yuJ'><dir id='F2yuJ'><q id='F2yuJ'></q></dir></style></legend>

                  1. <i id='F2yuJ'><tr id='F2yuJ'><dt id='F2yuJ'><q id='F2yuJ'><span id='F2yuJ'><b id='F2yuJ'><form id='F2yuJ'><ins id='F2yuJ'></ins><ul id='F2yuJ'></ul><sub id='F2yuJ'></sub></form><legend id='F2yuJ'></legend><bdo id='F2yuJ'><pre id='F2yuJ'><center id='F2yuJ'></center></pre></bdo></b><th id='F2yuJ'></th></span></q></dt></tr></i><div id='F2yuJ'><tfoot id='F2yuJ'></tfoot><dl id='F2yuJ'><fieldset id='F2yuJ'></fieldset></dl></div>
                      <bdo id='F2yuJ'></bdo><ul id='F2yuJ'></ul>
                  2. <tfoot id='F2yuJ'></tfoot>

                    1. <small id='F2yuJ'></small><noframes id='F2yuJ'>