如何从#temp表生成插入数据脚本?

时间:2015-04-16 05:05:27

标签: sql-server tsql sql-server-2008-r2 ssms

在DB中我有#temp表和数据,我需要生成插入脚本(对于所有数据)。

怎么做?我右键单击tempDB并选择>任务>生成脚本但我无法选择#temp表来生成脚本(它们无法选择)。

我如何使用SQL Server 2008 R2从#temp表生成插入脚本。

4 个答案:

答案 0 :(得分:1)

另一种最好的方法就是更快但更长。

使用SqlPubWiz

去吧:

  

C:\ Program Files(x86)\ Microsoft SQL Server \ 90 \ Tools \ Publishing \ 1.4

运行并填写有关登录数据库的所需信息,然后选择数据库并获取整个数据库脚本,并在保存的脚本文件中找到所需的表格插入脚本。

答案 1 :(得分:0)

您必须右键单击数据库并单击

Tasks -> Generate Scripts

现在你有一个弹出屏幕转到下一次点击

Select specific database objects

现在从“表格”选项中选择您的表格,然后点击“下一步”,然后点击“高级”按钮。

您将有另一个小弹出屏幕可供选择 脚本和选择的数据类型

Data only

单击“确定”,不要忘记将路径视为脚本保存的文件名。

现在单击“下一步”,然后再次单击“下一步”,您的脚本已准备好数据。

答案 2 :(得分:0)

您可以使用以下查询批处理为temp tables生成脚本,并且可以根据条件选择行。我从here得到了这个。在原始源中,作者创建了Stored Procedure来生成脚本。我已经格式化并修改了用条件声明TableName。您需要使用tempdb#temp table name中运行此查询批处理。感谢Neeraj Prasad Sharma

DECLARE @QUERY VARCHAR(MAX) = 'Dbo.#Temp where 1 = 1'

SET NOCOUNT ON

DECLARE @WithStrINdex AS INT
DECLARE @WhereStrINdex AS INT
DECLARE @INDExtouse AS INT

DECLARE @SchemaAndTAble VARCHAR(270)
DECLARE @Schema_name VARCHAR(30)
DECLARE @Table_name VARCHAR(240)
DECLARE @Condition VARCHAR(MAX) 

SELECT @WithStrINdex = 0
SELECT @WithStrINdex = CHARINDEX('WITH', @Query), @WhereStrINdex = CHARINDEX('WHERE', @Query)

IF(@WithStrINdex != 0)
    SELECT @INDExtouse = @WithStrINdex
ELSE
    SELECT @INDExtouse = @WhereStrINdex

SELECT @SchemaAndTAble  =  LEFT(@Query, @INDExtouse - 1) 
SELECT @SchemaAndTAble = LTRIM(RTRIM(@SchemaAndTAble))

SELECT @Schema_name = LEFT(@SchemaAndTAble, CHARINDEX('.', @SchemaAndTAble ) - 1)
,@Table_name  =  SUBSTRING(@SchemaAndTAble, CHARINDEX('.', @SchemaAndTAble ) + 1, LEN(@SchemaAndTAble))
,@CONDITION = SUBSTRING(@Query, @WhereStrINdex + 6, LEN(@Query))--27+6


DECLARE @COLUMNS TABLE([Row_number] SMALLINT, Column_Name VARCHAR(MAX))
DECLARE @CONDITIONS AS VARCHAR(MAX)
DECLARE @Total_Rows AS SMALLINT
DECLARE @Counter AS SMALLINT

DECLARE @ComaCol AS VARCHAR(MAX)
SELECT @ComaCol = '', @Counter = 1, @CONDITIONS = '' 

print @Schema_name
print @Table_name

INSERT INTO @COLUMNS
SELECT ROW_NUMBER() OVER(ORDER BY ORDINAL_POSITION) [Count] ,COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = @Schema_name
AND TABLE_NAME = @Table_name 
AND COLUMN_NAME NOT IN ('SYNCDESTINATION','PENDINGSYNCDESTINATION' ,'SKUID','SALECREDITEDTO')

SELECT @Total_Rows = COUNT(*) FROM @COLUMNS
SELECT @Table_name = '['+@Table_name+']'
SELECT @Schema_name = '['+@Schema_name+']'

WHILE (@Counter< = @Total_Rows )
BEGIN 
    SELECT @ComaCol =  @ComaCol + ' ['+Column_Name+'],' FROM @COLUMNS
    Where [Row_number] = @Counter

    SELECT @CONDITIONS = @CONDITIONS+ ' + CASE WHEN ['+Column_Name+'] IS NULL THEN ''NULL'' ELSE '''''''' + 
                    REPLACE( CONVERT(VARCHAR(MAX),['+Column_Name+']) ,'''''''','''')
                    +'''''''' END +'+''','''
                    FROM @COLUMNS WHERE [Row_number] = @Counter

        SET @Counter = @Counter + 1
END

SELECT @CONDITIONS = RIGHT(@CONDITIONS, LEN(@CONDITIONS) -2)

SELECT @CONDITIONS = LEFT(@CONDITIONS, LEN(@CONDITIONS) -4)

SELECT @ComaCol =  SUBSTRING (@ComaCol, 0, LEN(@ComaCol))

SELECT @CONDITIONS =  '''INSERT INTO ' + @Schema_name + '.' + @Table_name + '(' + @ComaCol + ')' +' VALUES( '+'''' + '+' + @CONDITIONS
SELECT @CONDITIONS = @CONDITIONS + '+' + ''')'''

SELECT @CONDITIONS =  'SELECT' + @CONDITIONS + 'FROM' + @Schema_name + '.' + @Table_name + ' WITH(NOLOCK) ' + ' WHERE ' + @Condition
PRINT(@CONDITIONS)
EXEC(@CONDITIONS)

答案 3 :(得分:0)

您可以将查询结果插入sql表(临时表,它将自动自动创建):

try
{
    var location = await Geolocation.GetLastKnownLocationAsync();

    if (location != null)
    {
        Console.WriteLine($"Latitude: {location.Latitude}, Longitude: {location.Longitude}, Altitude: {location.Altitude}");
    }
}
catch (FeatureNotSupportedException fnsEx)
{
    // Handle not supported on device exception
}
catch (FeatureNotEnabledException fneEx)
{
    // Handle not enabled on device exception
}
catch (PermissionException pEx)
{
    // Handle permission exception
}
catch (Exception ex)
{
    // Unable to get location
}

例如:SELECT * INTO myTempTable FROM (query results)

将在模式SELECT * INTO myTempTable FROM user where condition内创建名为myTempTable的表 然后单击数据库,单击:

dbo

,然后选择表Tasks > Generate Scripts