[SQL] 複製一筆資料並自行指定主鍵值

  • 6325
  • 0
  • 2013-06-25

[SQL]複製一筆資料並自行指定主鍵值

最近剛好有用到... ↓

 

若[Index]具識別屬性:


                SET IDENTITY_INSERT [dbo].[Table] OFF
                GO
                INSERT INTO [Table] 
                (
                    [index]
                    ,[A]
                    ,[B]
                )
                SELECT 
                    3 AS [index]
                    ,[A]
                    ,[B]
                  FROM [Table]
                  WHERE [index] = 2
                GO  
                SET IDENTITY_INSERT [dbo].[Table] ON
                GO

 

若[Index]不具識別屬性:


                INSERT INTO [Table] 
                (
	                [index]
                    ,[A]
                    ,[B]
                )
                SELECT 
                    3 AS [index]
                    ,[A]
                    ,[B]
                  FROM [Table]
                  WHERE [index] = 2

 

參考討論串:http://social.msdn.microsoft.com/Forums/zh-TW/240/thread/717a2c79-a594-4462-9651-87d0b36df3c0/

--

不斷學習,精進自我

Herbert Fang