Guest User

Untitled

a guest
Oct 15th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. create table with_id (
  2. id int identity(1,1),
  3. val varchar(30)
  4. );
  5.  
  6. select * into copy_from_with_id_1 from with_id;
  7.  
  8. select id, val into copy_from_with_id_2 from with_id;
  9.  
  10. create table without_id (
  11. id int,
  12. val varchar(30)
  13. );
  14.  
  15. select id, val
  16. into copy_from_with_id_2
  17. from with_id
  18.  
  19. union all
  20.  
  21. select 0, 'test_row'
  22. where 1 = 0;
  23.  
  24. select * into without_id from with_id where 1 = 0
  25. union all
  26. select * from with_id where 1 = 0
  27. ;
  28. insert into without_id select * from with_id;
  29.  
  30. select * into without_id from with_id
  31. union all
  32. select * from with_id where 1 = 0
  33. ;
  34.  
  35. SELECT
  36. t.*
  37. INTO
  38. dbo.NewTable
  39. FROM
  40. dbo.TableWithIdentity AS t
  41. LEFT JOIN dbo.TableWithIdentity ON 1 = 0
  42. ;
  43.  
  44. SELECT isnull(Tablename_old.IDENTITYCOL + 0, -1) AS 'New Identity Column'
  45. INTO dbo.TableName_new
  46. FROM dbo.TableName_old
  47.  
  48. SELECT *
  49. INTO without_id
  50. FROM [linked_server].[source_db].dbo.[with_id];
  51.  
  52. DECLARE @LocalServer SYSNAME
  53. SET @LocalServer = @@SERVERNAME;
  54. EXEC master.dbo.sp_addlinkedserver @server = N'localserver'
  55. , @srvproduct = ''
  56. , @provider = 'SQLNCLI'
  57. , @datasrc = @LocalServer;
  58. EXEC master.dbo.sp_addlinkedsrvlogin @rmtsrvname=N'localserver'
  59. , @useself = N'True'
  60. , @locallogin = NULL
  61. , @rmtuser = NULL
  62. , @rmtpassword = NULL;
  63.  
  64. SELECT *
  65. INTO without_id
  66. FROM [localserver].[source_db].dbo.[with_id];
  67.  
  68. EXEC sp_dropserver @server = 'localserver'
  69. , @droplogins = 'droplogins';
  70.  
  71. SELECT *
  72. INTO without_id
  73. FROM OPENQUERY([linked_server], 'SELECT * FROM [source_db].dbo.[with_id]');
  74.  
  75. select a.* into without_id from with_id a inner join with_id b on 1 = 0;
  76.  
  77. insert into without_id select * from with_id;
  78.  
  79. --temp table has IDENTITY
  80. select ID, Name
  81. into #t
  82. from dbo.Employee
  83.  
  84. --no IDENTITY
  85. select ID = ID + 0, Name
  86. into #t
  87. from dbo.Employee
Add Comment
Please, Sign In to add comment