使用SQL将整个数据从一个表复制到另一个表的存储过程

时间:2016-10-03 19:25:20

标签: sql-server sql-server-2012

我有一个表Persons (ID, Name, Address)和另一个表Persons_twin (ID, Name, Address),其中我要从Persons复制整行。

哪种方法是复制数据的最短最好的方法?下面是我尝试的代码,但没有执行。您的建议最有价值。谢谢。

BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    SET NOCOUNT ON;

    -- Insert statements for procedure here
    INSERT INTO [dbo].[Persons_twin](ID, Name, Address)
    VALUES(SELECT ID, Name, Address FROM [dbo].[Persons])
END

3 个答案:

答案 0 :(得分:2)

删除<UserControl x:Class="ucWorkOrderActivity" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" mc:Ignorable="d" Height="74" Width="570" Background="White" BorderBrush="Gray" BorderThickness="1" Loaded="UserControl_Loaded" MouseDoubleClick="UserControl_MouseDoubleClick"> <Grid> <DockPanel Name="dkpControl" Margin="10,0,25,0" > <StackPanel Name="stpMoveButtons" Orientation="Vertical" DockPanel.Dock="Right" HorizontalAlignment="Right" Margin="5,0,0,0"> <Button Name="btnUp" Click="btnUp_Click" Height="25" Width="25" Content="▲" ToolTip="Move Action Up" IsEnabled="{Binding Path=IsUpEnabled}" Margin="0,5,5,10" /> <Button Name="btnDown" Click="btnDown_Click" Height="25" Width="25" Content="▼" ToolTip="Move Action Down" Margin="0,0,5,10" IsEnabled="{Binding Path=IsDownEnabled}" /> </StackPanel> <DockPanel DockPanel.Dock="Top" Margin="0,0,5,0" > <TextBlock Name="txbDesc" Text="{Binding Path=Description}" Width="150" TextTrimming="CharacterEllipsis" Margin="10,5,5,5" VerticalAlignment="Top" /> <DockPanel Margin="5,0" HorizontalAlignment="Right" DockPanel.Dock="Right" Width="358" > <TextBlock Name="txbCostLabel" Text="Cost: " DockPanel.Dock="Left" VerticalAlignment="Top" Margin="46,5,0,5" /> <TextBlock Name="txbCost" Text="{Binding Path=AmountCharged}" TextAlignment="Right" DockPanel.Dock="Left" Width="50" Margin="5" VerticalAlignment="Top" /> <DatePicker Name="dtpCompleted" DockPanel.Dock="Right" Height="25" VerticalAlignment="Top" Margin="0,1,0,0" SelectedDate="{Binding Path=EndDate}" Width="90" /> <TextBlock Name="txbStatus" Text="{Binding Path=Status}" DockPanel.Dock="right" HorizontalAlignment="Left" Width="60" Margin="45,5,30,5" TextAlignment="Left" VerticalAlignment="Top" /> </DockPanel> </DockPanel> <DockPanel DockPanel.Dock="Bottom" Margin="0,0,5,0" > <Image Name="imgLocationChange" Margin="25,0" DockPanel.Dock="Left" Height="24" Source="/SMS_Main;component/Resources/UnitMoveLoc_24.png" Visibility="Hidden" /> <TextBlock Name="txbCharge" Text="{Binding Path=ChargeWhen}" Margin="70,0,0,0" VerticalAlignment="Center" /> <Button Name="btnComplete" Height="26" Width="26" DockPanel.Dock="Right" HorizontalAlignment="Right" Click="btnComplete_Click" ToolTip="Complete Action" > <Image Source="/SMS_Main;component/Resources/OK_24.png" Margin="2" /> </Button> <Button Name="btnCancel" Height="26" Width="26" DockPanel.Dock="Right" HorizontalAlignment="Right" Click="btnCancel_Click" ToolTip="Remove Action"> <Image Source="/SMS_Main;component/Resources/Cancel_24.png" Margin="2" /> </Button> <Button Name="btnRevert" Height="26" Width="26" DockPanel.Dock="Right" HorizontalAlignment="Right" Click="btnRevert_Click" IsEnabled="{Binding Path=CurrentlySaved}" ToolTip="Revert Change"> <Image Source="/SMS_Main;component/Resources/Undo_24.png" Margin="2" /> </Button> <Button Name="btnEdit" Height="26" Width="26" DockPanel.Dock="Right" HorizontalAlignment="Right" Click="btnEdit_Click" ToolTip="Edit Action" > <Image Margin="2" Source="/SMS_Main;component/Resources/Config_24.png" /> </Button> <TextBlock Name="txbAssignedTo" Text="{Binding Path=AssignedUser}" VerticalAlignment="Center" DockPanel.Dock="Right" HorizontalAlignment="Left" Margin="95,0,10,0" /> </DockPanel> </DockPanel> </Grid> </UserControl> 子句并直接使用values查询

select

答案 1 :(得分:0)

如果两个表都相同,则可以使用此查询:

INSERT INTO Persons_twin
SELECT * FROM Persons

答案 2 :(得分:0)

您甚至可以创建第二个表即时。在这种情况下,您不必担心表的结构:

SELECT * INTO persons_twin FROM Persons