Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.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 创建临时表的脚本_Sql_Sql Server_Sql Server 2008_Temp Tables - Fatal编程技术网

Sql 创建临时表的脚本

Sql 创建临时表的脚本,sql,sql-server,sql-server-2008,temp-tables,Sql,Sql Server,Sql Server 2008,Temp Tables,我在SQLServer2008R2中有一个全局临时表。我需要一个生成脚本的方法与我们在SSMS中通过脚本表as->Create to生成脚本的方法相同 我实际上并没有创建临时表。我只是尝试使用其模式创建一个表,以便在事务期间将数据永久存储在表中。您不能使用SSMS GUI来执行此操作。但是这个代码是从 你想要一个脚本来做什么?你可以看看这里。。类似的问题@阿伦伯特朗,我道歉。您不能先选择*into,然后使用SSMS生成吗?为什么要使用全局临时表?这比一张永久的桌子有什么优势?这两种方法都将并发性

我在SQLServer2008R2中有一个全局临时表。我需要一个生成脚本的方法与我们在SSMS中通过脚本表as->Create to生成脚本的方法相同


我实际上并没有创建临时表。我只是尝试使用其模式创建一个表,以便在事务期间将数据永久存储在表中。

您不能使用SSMS GUI来执行此操作。但是这个代码是从


你想要一个脚本来做什么?你可以看看这里。。类似的问题@阿伦伯特朗,我道歉。您不能先选择*into,然后使用SSMS生成吗?为什么要使用全局临时表?这比一张永久的桌子有什么优势?这两种方法都将并发性精确地降低到1。从全局临时表中选择前0*到[anotherdb].[dbo].[以前的全局临时表临时表];然后写下来??我想这是我最好的建议。另外,@aaronbertrand的评论+1是正确的链接吗?我在那里没有看到类似的东西。哎呀,我把它换成了正确的
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 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')