欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費電子書(shū)等14項超值服

開(kāi)通VIP
SQL SERVER幾種數據遷移/導出導入的實(shí)踐

該篇博文轉載自: http://www.cnblogs.com/fishparadise/p/4592789.html

 

SQLServer提供了多種數據導出導入的工具和方法,在此,分享我實(shí)踐的經(jīng)驗(只涉及數據庫與Excel、數據庫與文本文件、數據庫與數據庫之間的導出導入)。

(一)數據庫與Excel

方法1:

使用數據庫客戶(hù)端(SSMS)的界面工具。右鍵選擇要導出數據的數據庫,選擇“任務(wù)”——“導出數據”,下圖1,按照向導一步一步操作即可。而導入則相反,導入時(shí),SQLServer會(huì )默認創(chuàng )建一張新表,字段名也默認跟導入的Excel標題一樣,并且會(huì )默認字段數據類(lèi)型等。當然在可以在向導進(jìn)行修改。需要注意的是如果標題不是英文而是中文,默認創(chuàng )建字段名也是中文,這將給后面數據更新操作帶來(lái)麻煩,所以最好還是以有意義的英文字段名。把數據導入后,再通過(guò)執行語(yǔ)句,把數據插入/更新到業(yè)務(wù)表。

 

figure-1:任務(wù)——導出數據

 

方法2:

從SQLServer2005開(kāi)始,可以直接在SSMS上查詢(xún)出來(lái)的結果復制,然后粘貼到Excel上,對于少量數據來(lái)說(shuō),是非??焖俜奖愕?,需要注意的是長(cháng)數字可能會(huì )變成科學(xué)記數法的形式,提前在Excel上指定列的格式為文本即可。

導入的話(huà),ctrl + c 復制Excel上的數據,然后在選擇相關(guān)表,編輯數據,把數據直接粘貼上去即可。但是不建議直接粘貼到業(yè)務(wù)表(如果表是空白沒(méi)有數據,并且字段順序對應,可以這樣操作),而是建議先粘貼到一個(gè)新建的中間表中,然后再通過(guò)語(yǔ)句,把數據插入/更新到業(yè)務(wù)表。

這種方法的導出導入,適合于少量的數據,如5000行以?xún)鹊挠涗?,大?000行以上就不建議了,速度較慢,如果數據過(guò)大,還一定成功。

 

 

(二)數據庫與文本文件、數據庫與數據庫

數據庫之間的數據遷移或導出導入其實(shí)是比較方便的,比如備份數據庫后,在新的機器上做恢復。但是需要注意的是SQL2008之前的版本的備份無(wú)法在SQL2012或以上版本上直接恢復的,而是通過(guò)中間的SQL2008做一個(gè)過(guò)渡,把舊版本的數據庫恢復到SQL2008,然后做備份,最后在SQL2012上恢復。

如果是新版本(下面以SQL2012為例)的備份文件恢復到舊版本(以SQL2008為例)上就比較麻煩了,一般是不支持新版本備份文件在舊版本中恢復的。只能通過(guò)編寫(xiě)腳本,把新版本的數據導入到舊版本中。

 

方法1:

首先推薦使用的是數據不落地的“鏈接服務(wù)器”。使用SQL2012的SSMS,同時(shí)連接到SQL2012和SQL2008的實(shí)例,通過(guò)編寫(xiě)腳本把SQL2012的數據導入到SQL2008中。兩個(gè)實(shí)例的可以通過(guò)鏈接服務(wù)器來(lái)連接。以下是設置步驟。

figure-2:新建鏈接服務(wù)器

 

figure-3:鏈接服務(wù)器和數據源

 

figure-4:認證

 

 

figure-5:創(chuàng )建成功后,可以直接瀏覽鏈接服務(wù)器的目錄,也可以使用語(yǔ)句查詢(xún)了。

也可以使用腳本來(lái)創(chuàng )建鏈接服務(wù)器。

  1. --創(chuàng )建鏈接服務(wù)器
  2. EXEC sp_addlinkedserver
  3. @server='LINKED_SERVER_TEST2',--被訪(fǎng)問(wèn)的服務(wù)器別名
  4. @srvproduct='',
  5. @provider='SQLOLEDB',
  6. @datasrc='192.168.88.6,11433'--數據源
  7. GO
  8. --創(chuàng )建登錄名和密碼
  9. EXEC sys.sp_addlinkedsrvlogin
  10. @rmtsrvname = 'LINKED_SERVER_TEST2', -- 被訪(fǎng)問(wèn)的服務(wù)器別名
  11. @useself = 'false',
  12. @locallogin = NULL,
  13. @rmtuser = 'sa', -- 數據源登錄名
  14. @rmtpassword = 'psd123456' -- 數據源登錄密碼
  15. GO
  16. --設置數據可以訪(fǎng)問(wèn)
  17. EXEC sys.sp_serveroption
  18. @server = 'LINKED_SERVER_TEST2',
  19. @optname = 'data access',
  20. @optvalue = N'true'
  21. GO


 

code-1:創(chuàng )建鏈接服務(wù)器的腳本

 

創(chuàng )建成功后,可以直接查詢(xún)數據。

 

figure-6:查詢(xún)鏈接服務(wù)器的數據

 

通過(guò)視圖sys.servers可以查詢(xún)所有服務(wù)器及相關(guān)的屬性。

 

figure-7:查詢(xún)所有鏈接服務(wù)器

 

在SSMS上或運行以下腳本可以刪除指定的鏈接服務(wù)器。

  1. --刪除鏈接服務(wù)器及所有登錄
  2. EXEC sys.sp_dropserver
  3. @server = 'LINKED_SERVER_TEST2',
  4. @droplogins = 'droplogins'
  5. GO


 

 code-2:刪除鏈接服務(wù)器及所有登錄

 

詳細請參考:https://technet.microsoft.com/zh-cn/library/ff772782%28v=sql.105%29.aspx

 

方法2:

如果兩個(gè)實(shí)例不能連接,只能在SQL2012上導出數據,再到SQL2008上導入。SQLServer提供生成包含數據的腳本工具,下圖2。在第三步的“高級”選項里有一項“Types of data to scripts”有三個(gè)選擇:Data only,Schema and data,Schema only,分別是只生成數據、生成表(對象)和數據,表(對象)。還有生成腳本的版本“Script for Server Version”,下圖3。其他選項,按實(shí)際需要選擇。

 figure-8:任務(wù)——生成腳本

figure-9:生成腳本的高級選項

也可以使用存儲過(guò)程生成包含數據的腳本。這里介紹一個(gè)別人已經(jīng)做寫(xiě)好存儲過(guò)程:sp_generate_inserts。運行之后,會(huì )按表每條記錄生成一條insert的語(yǔ)句

  1. CREATE PROC [dbo].[sp_generate_inserts]
  2. (
  3. @table_name VARCHAR(776) , -- The table/view for which the INSERT statements will be generated using the existing data
  4. @target_table VARCHAR(776) = NULL , -- Use this parameter to specify a different table name into which the data will be inserted
  5. @include_column_list BIT = 1 , -- Use this parameter to include/ommit column list in the generated INSERT statement
  6. @from VARCHAR(800) = NULL , -- Use this parameter to filter the rows based on a filter condition (using WHERE)
  7. @include_timestamp BIT = 0 , -- Specify 1 for this parameter, if you want to include the TIMESTAMP/ROWVERSION column's data in the INSERT statement
  8. @debug_mode BIT = 0 , -- If @debug_mode is set to 1, the SQL statements constructed by this procedure will be printed for later examination
  9. @owner VARCHAR(64) = NULL , -- Use this parameter if you are not the owner of the table
  10. @ommit_images BIT = 0 , -- Use this parameter to generate INSERT statements by omitting the 'image' columns
  11. @ommit_identity BIT = 1 , -- Use this parameter to ommit the identity columns
  12. @top INT = NULL , -- Use this parameter to generate INSERT statements only for the TOP n rows
  13. @cols_to_include VARCHAR(8000) = NULL , -- List of columns to be included in the INSERT statement
  14. @cols_to_exclude VARCHAR(8000) = NULL , -- List of columns to be excluded from the INSERT statement
  15. @disable_constraints BIT = 0 , -- When 1, disables foreign key constraints and enables them after the INSERT statements
  16. @ommit_computed_cols BIT = 1 -- When 1, computed columns will not be included in the INSERT statement
  17. )
  18. AS
  19. BEGIN
  20. /***********************************************************************************************************
  21. Procedure: sp_generate_inserts (Build 22)
  22. (Copyright ?2002 Narayana Vyas Kondreddi. All rights reserved.)
  23. Purpose: To generate INSERT statements from existing data.
  24. These INSERTS can be executed to regenerate the data at some other location.
  25. This procedure is also useful to create a database setup, where in you can
  26. script your data along with your table definitions.
  27. Written by: Narayana Vyas Kondreddi
  28. http://vyaskn.tripod.com
  29. Acknowledgements:
  30. Divya Kalra -- For beta testing
  31. Mark Charsley -- For reporting a problem with scripting uniqueidentifier columns with NULL values
  32. Artur Zeygman -- For helping me simplify a bit of code for handling non-dbo owned tables
  33. Joris Laperre -- For reporting a regression bug in handling text/ntext columns
  34. Tested on: SQL Server 7.0 and SQL Server 2000
  35. Date created: January 17th 2001 21:52 GMT
  36. Date modified: May 1st 2002 19:50 GMT
  37. Email: vyaskn@hotmail.com
  38. NOTE: This procedure may not work with tables with too many columns.
  39. Results can be unpredictable with huge text columns or SQL Server 2000's sql_variant data types
  40. Whenever possible, Use @include_column_list parameter to ommit column list in the INSERT statement, for better results
  41. IMPORTANT: This procedure is not tested with internation data (Extended characters or Unicode). If needed
  42. you might want to convert the datatypes of character variables in this procedure to their respective unicode counterparts
  43. like nchar and nvarchar
  44. Example 1: To generate INSERT statements for table 'titles':
  45. EXEC sp_generate_inserts 'titles'
  46. Example 2: To ommit the column list in the INSERT statement: (Column list is included by default)
  47. IMPORTANT: If you have too many columns, you are advised to ommit column list, as shown below,
  48. to avoid erroneous results
  49. EXEC sp_generate_inserts 'titles', @include_column_list = 0
  50. Example 3: To generate INSERT statements for 'titlesCopy' table from 'titles' table:
  51. EXEC sp_generate_inserts 'titles', 'titlesCopy'
  52. Example 4: To generate INSERT statements for 'titles' table for only those titles
  53. which contain the word 'Computer' in them:
  54. NOTE: Do not complicate the FROM or WHERE clause here. It's assumed that you are good with T-SQL if you are using this parameter
  55. EXEC sp_generate_inserts 'titles', @from = "from titles where title like '%Computer%'"
  56. Example 5: To specify that you want to include TIMESTAMP column's data as well in the INSERT statement:
  57. (By default TIMESTAMP column's data is not scripted)
  58. EXEC sp_generate_inserts 'titles', @include_timestamp = 1
  59. Example 6: To print the debug information:
  60. EXEC sp_generate_inserts 'titles', @debug_mode = 1
  61. Example 7: If you are not the owner of the table, use @owner parameter to specify the owner name
  62. To use this option, you must have SELECT permissions on that table
  63. EXEC sp_generate_inserts Nickstable, @owner = 'Nick'
  64. Example 8: To generate INSERT statements for the rest of the columns excluding images
  65. When using this otion, DO NOT set @include_column_list parameter to 0.
  66. EXEC sp_generate_inserts imgtable, @ommit_images = 1
  67. Example 9: To generate INSERT statements excluding (ommiting) IDENTITY columns:
  68. (By default IDENTITY columns are included in the INSERT statement)
  69. EXEC sp_generate_inserts mytable, @ommit_identity = 1
  70. Example 10: To generate INSERT statements for the TOP 10 rows in the table:
  71. EXEC sp_generate_inserts mytable, @top = 10
  72. Example 11: To generate INSERT statements with only those columns you want:
  73. EXEC sp_generate_inserts titles, @cols_to_include = "'title','title_id','au_id'"
  74. Example 12: To generate INSERT statements by omitting certain columns:
  75. EXEC sp_generate_inserts titles, @cols_to_exclude = "'title','title_id','au_id'"
  76. Example 13: To avoid checking the foreign key constraints while loading data with INSERT statements:
  77. EXEC sp_generate_inserts titles, @disable_constraints = 1
  78. Example 14: To exclude computed columns from the INSERT statement:
  79. EXEC sp_generate_inserts MyTable, @ommit_computed_cols = 1
  80. ***********************************************************************************************************/
  81. SET NOCOUNT ON
  82. --Making sure user only uses either @cols_to_include or @cols_to_exclude
  83. IF ( ( @cols_to_include IS NOT NULL )
  84. AND ( @cols_to_exclude IS NOT NULL )
  85. )
  86. BEGIN
  87. RAISERROR('Use either @cols_to_include or @cols_to_exclude. Do not use both the parameters at once',16,1)
  88. RETURN -1 --Failure. Reason: Both @cols_to_include and @cols_to_exclude parameters are specified
  89. END
  90. --Making sure the @cols_to_include and @cols_to_exclude parameters are receiving values in proper format
  91. IF ( ( @cols_to_include IS NOT NULL )
  92. AND ( PATINDEX('''%''', @cols_to_include) = 0 )
  93. )
  94. BEGIN
  95. RAISERROR('Invalid use of @cols_to_include property',16,1)
  96. PRINT 'Specify column names surrounded by single quotes and separated by commas'
  97. PRINT 'Eg: EXEC sp_generate_inserts titles, @cols_to_include = "''title_id'',''title''"'
  98. RETURN -1 --Failure. Reason: Invalid use of @cols_to_include property
  99. END
  100. IF ( ( @cols_to_exclude IS NOT NULL )
  101. AND ( PATINDEX('''%''', @cols_to_exclude) = 0 )
  102. )
  103. BEGIN
  104. RAISERROR('Invalid use of @cols_to_exclude property',16,1)
  105. PRINT 'Specify column names surrounded by single quotes and separated by commas'
  106. PRINT 'Eg: EXEC sp_generate_inserts titles, @cols_to_exclude = "''title_id'',''title''"'
  107. RETURN -1 --Failure. Reason: Invalid use of @cols_to_exclude property
  108. END
  109. --Checking to see if the database name is specified along wih the table name
  110. --Your database context should be local to the table for which you want to generate INSERT statements
  111. --specifying the database name is not allowed
  112. IF ( PARSENAME(@table_name, 3) ) IS NOT NULL
  113. BEGIN
  114. RAISERROR('Do not specify the database name. Be in the required database and just specify the table name.',16,1)
  115. RETURN -1 --Failure. Reason: Database name is specified along with the table name, which is not allowed
  116. END
  117. --Checking for the existence of 'user table' or 'view'
  118. --This procedure is not written to work on system tables
  119. --To script the data in system tables, just create a view on the system tables and script the view instead
  120. IF @owner IS NULL
  121. BEGIN
  122. IF ( ( OBJECT_ID(@table_name, 'U') IS NULL )
  123. AND ( OBJECT_ID(@table_name, 'V') IS NULL )
  124. )
  125. BEGIN
  126. RAISERROR('User table or view not found.',16,1)
  127. PRINT 'You may see this error, if you are not the owner of this table or view. In that case use @owner parameter to specify the owner name.'
  128. PRINT 'Make sure you have SELECT permission on that table or view.'
  129. RETURN -1 --Failure. Reason: There is no user table or view with this name
  130. END
  131. END
  132. ELSE
  133. BEGIN
  134. IF NOT EXISTS ( SELECT 1
  135. FROM INFORMATION_SCHEMA.TABLES
  136. WHERE TABLE_NAME = @table_name
  137. AND ( TABLE_TYPE = 'BASE TABLE'
  138. OR TABLE_TYPE = 'VIEW'
  139. )
  140. AND TABLE_SCHEMA = @owner )
  141. BEGIN
  142. RAISERROR('User table or view not found.',16,1)
  143. PRINT 'You may see this error, if you are not the owner of this table. In that case use @owner parameter to specify the owner name.'
  144. PRINT 'Make sure you have SELECT permission on that table or view.'
  145. RETURN -1 --Failure. Reason: There is no user table or view with this name
  146. END
  147. END
  148. --Variable declarations
  149. DECLARE @Column_ID INT ,
  150. @Column_List NVARCHAR(MAX) ,
  151. @Column_Name VARCHAR(128) ,
  152. @Start_Insert NVARCHAR(MAX) ,
  153. @Data_Type VARCHAR(128) ,
  154. @Actual_Values NVARCHAR(MAX) , --This is the string that will be finally executed to generate INSERT statements
  155. @IDN VARCHAR(128) --Will contain the IDENTITY column's name in the table
  156. --Variable Initialization
  157. SET @IDN = ''
  158. SET @Column_ID = 0
  159. SET @Column_Name = ''
  160. SET @Column_List = ''
  161. SET @Actual_Values = ''
  162. IF @owner IS NULL
  163. BEGIN
  164. SET @Start_Insert = 'INSERT INTO ' + '['
  165. + RTRIM(COALESCE(@target_table, @table_name)) + ']'
  166. END
  167. ELSE
  168. BEGIN
  169. SET @Start_Insert = 'INSERT ' + '[' + LTRIM(RTRIM(@owner))
  170. + '].' + '[' + RTRIM(COALESCE(@target_table, @table_name))
  171. + ']'
  172. END
  173. --To get the first column's ID
  174. SELECT @Column_ID = MIN(ORDINAL_POSITION)
  175. FROM INFORMATION_SCHEMA.COLUMNS (NOLOCK)
  176. WHERE TABLE_NAME = @table_name
  177. AND ( @owner IS NULL
  178. OR TABLE_SCHEMA = @owner
  179. )
  180. --Loop through all the columns of the table, to get the column names and their data types
  181. WHILE @Column_ID IS NOT NULL
  182. BEGIN
  183. SELECT @Column_Name = QUOTENAME(COLUMN_NAME) ,
  184. @Data_Type = DATA_TYPE
  185. FROM INFORMATION_SCHEMA.COLUMNS (NOLOCK)
  186. WHERE ORDINAL_POSITION = @Column_ID
  187. AND TABLE_NAME = @table_name
  188. AND ( @owner IS NULL
  189. OR TABLE_SCHEMA = @owner
  190. )
  191. IF @cols_to_include IS NOT NULL --Selecting only user specified columns
  192. BEGIN
  193. IF CHARINDEX('''' + SUBSTRING(@Column_Name, 2,
  194. LEN(@Column_Name) - 2)
  195. + '''', @cols_to_include) = 0
  196. BEGIN
  197. GOTO SKIP_LOOP
  198. END
  199. END
  200. IF @cols_to_exclude IS NOT NULL --Selecting only user specified columns
  201. BEGIN
  202. IF CHARINDEX('''' + SUBSTRING(@Column_Name, 2,
  203. LEN(@Column_Name) - 2)
  204. + '''', @cols_to_exclude) <> 0
  205. BEGIN
  206. GOTO SKIP_LOOP
  207. END
  208. END
  209. --Making sure to output SET IDENTITY_INSERT ON/OFF in case the table has an IDENTITY column
  210. IF ( SELECT COLUMNPROPERTY(OBJECT_ID(QUOTENAME(COALESCE(@owner,
  211. USER_NAME()))
  212. + '.' + @table_name),
  213. SUBSTRING(@Column_Name, 2,
  214. LEN(@Column_Name) - 2),
  215. 'IsIdentity')
  216. ) = 1
  217. BEGIN
  218. IF @ommit_identity = 0 --Determing whether to include or exclude the IDENTITY column
  219. SET @IDN = @Column_Name
  220. ELSE
  221. GOTO SKIP_LOOP
  222. END
  223. --Making sure whether to output computed columns or not
  224. IF @ommit_computed_cols = 1
  225. BEGIN
  226. IF ( SELECT COLUMNPROPERTY(OBJECT_ID(QUOTENAME(COALESCE(@owner,
  227. USER_NAME()))
  228. + '.'
  229. + @table_name),
  230. SUBSTRING(@Column_Name, 2,
  231. LEN(@Column_Name)
  232. - 2),
  233. 'IsComputed')
  234. ) = 1
  235. BEGIN
  236. GOTO SKIP_LOOP
  237. END
  238. END
  239. --Tables with columns of IMAGE data type are not supported for obvious reasons
  240. IF ( @Data_Type IN ( 'image' ) )
  241. BEGIN
  242. IF ( @ommit_images = 0 )
  243. BEGIN
  244. RAISERROR('Tables with image columns are not supported.',16,1)
  245. PRINT 'Use @ommit_images = 1 parameter to generate INSERTs for the rest of the columns.'
  246. PRINT 'DO NOT ommit Column List in the INSERT statements. If you ommit column list using @include_column_list=0, the generated INSERTs will fail.'
  247. RETURN -1 --Failure. Reason: There is a column with image data type
  248. END
  249. ELSE
  250. BEGIN
  251. GOTO SKIP_LOOP
  252. END
  253. END
  254. --Determining the data type of the column and depending on the data type, the VALUES part of
  255. --the INSERT statement is generated. Care is taken to handle columns with NULL values. Also
  256. --making sure, not to lose any data from flot, real, money, smallmomey, datetime columns
  257. SET @Actual_Values = @Actual_Values
  258. + CASE WHEN @Data_Type IN ( 'char', 'varchar', 'nchar','nvarchar' )
  259. THEN 'COALESCE(''N'''''' + REPLACE(RTRIM('
  260. + @Column_Name
  261. + '),'''''''','''''''''''')+'''''''',''NULL'')'
  262. WHEN @Data_Type IN ( 'datetime', 'smalldatetime',
  263. 'DATE','time' )
  264. THEN 'COALESCE('''''''' + RTRIM(CONVERT(char,'
  265. + @Column_Name + ',120))+'''''''',''NULL'')'
  266. WHEN @Data_Type IN ( 'uniqueidentifier' )
  267. THEN 'COALESCE('''''''' + REPLACE(CONVERT(char(255),RTRIM('
  268. + @Column_Name
  269. + ')),'''''''','''''''''''')+'''''''',''NULL'')'
  270. WHEN @Data_Type IN ( 'text', 'ntext' )
  271. THEN 'COALESCE(''N'''''' + REPLACE(CONVERT(char(8000),'
  272. + @Column_Name
  273. + '),'''''''','''''''''''')+'''''''',''NULL'')'
  274. WHEN @Data_Type IN ( 'binary', 'varbinary' )
  275. THEN 'COALESCE(RTRIM(CONVERT(char,'
  276. + 'CONVERT(int,' + @Column_Name
  277. + '))),''NULL'')'
  278. WHEN @Data_Type IN ( 'timestamp', 'rowversion' )
  279. THEN CASE WHEN @include_timestamp = 0
  280. THEN '''DEFAULT'''
  281. ELSE 'COALESCE(RTRIM(CONVERT(char,'
  282. + 'CONVERT(int,' + @Column_Name
  283. + '))),''NULL'')'
  284. END
  285. WHEN @Data_Type IN ( 'hierarchyid' )
  286. THEN 'COALESCE(''CAST(''''''+LTRIM(RTRIM('
  287. + 'CONVERT(char, ' + @Column_Name + ')'
  288. + ')),''NULL'')' + '+''''''AS hierarchyid)'''
  289. WHEN @Data_Type IN ( 'float', 'real', 'money',
  290. 'smallmoney' )
  291. THEN 'COALESCE(LTRIM(RTRIM(' + 'CONVERT(char, '
  292. + @Column_Name + ',2)' + ')),''NULL'')'
  293. ELSE 'COALESCE(LTRIM(RTRIM(' + 'CONVERT(char, '
  294. + @Column_Name + ')' + ')),''NULL'')'
  295. END + '+' + ''',''' + ' + '
  296. --Generating the column list for the INSERT statement
  297. SET @Column_List = @Column_List + @Column_Name + ','
  298. SKIP_LOOP: --The label used in GOTO
  299. SELECT @Column_ID = MIN(ORDINAL_POSITION)
  300. FROM INFORMATION_SCHEMA.COLUMNS (NOLOCK)
  301. WHERE TABLE_NAME = @table_name
  302. AND ORDINAL_POSITION > @Column_ID
  303. AND ( @owner IS NULL
  304. OR TABLE_SCHEMA = @owner
  305. )
  306. --Loop ends here!
  307. END
  308. --To get rid of the extra characters that got concatenated during the last run through the loop
  309. SET @Column_List = LEFT(@Column_List, LEN(@Column_List) - 1)
  310. SET @Actual_Values = LEFT(@Actual_Values, LEN(@Actual_Values) - 6)
  311. IF LTRIM(@Column_List) = ''
  312. BEGIN
  313. RAISERROR('No columns to select. There should at least be one column to generate the output',16,1)
  314. RETURN -1 --Failure. Reason: Looks like all the columns are ommitted using the @cols_to_exclude parameter
  315. END
  316. --Forming the final string that will be executed, to output the INSERT statements
  317. IF ( @include_column_list <> 0 )
  318. BEGIN
  319. SET @Actual_Values = 'SELECT ' + CASE WHEN @top IS NULL
  320. OR @top < 0 THEN ''
  321. ELSE ' TOP '
  322. + LTRIM(STR(@top))
  323. + ' '
  324. END + ''''
  325. + RTRIM(@Start_Insert) + ' ''+' + '''('
  326. + RTRIM(@Column_List) + '''+' + ''')'''
  327. + ' +''VALUES(''+ ' + @Actual_Values + '+'')''' + ' '
  328. + COALESCE(@from,
  329. ' FROM ' + CASE WHEN @owner IS NULL THEN ''
  330. ELSE '[' + LTRIM(RTRIM(@owner))
  331. + '].'
  332. END + '[' + RTRIM(@table_name) + ']'
  333. + '(NOLOCK)')
  334. END
  335. ELSE
  336. IF ( @include_column_list = 0 )
  337. BEGIN
  338. SET @Actual_Values = 'SELECT '
  339. + CASE WHEN @top IS NULL
  340. OR @top < 0 THEN ''
  341. ELSE ' TOP ' + LTRIM(STR(@top)) + ' '
  342. END + '''' + RTRIM(@Start_Insert)
  343. + ' '' +''VALUES(''+ ' + @Actual_Values + '+'')'''
  344. + ' ' + COALESCE(@from,
  345. ' FROM '
  346. + CASE WHEN @owner IS NULL THEN ''
  347. ELSE '[' + LTRIM(RTRIM(@owner))
  348. + '].'
  349. END + '[' + RTRIM(@table_name)
  350. + ']' + '(NOLOCK)')
  351. END
  352. --Determining whether to ouput any debug information
  353. IF @debug_mode = 1
  354. BEGIN
  355. PRINT '/*****START OF DEBUG INFORMATION*****'
  356. PRINT 'Beginning of the INSERT statement:'
  357. PRINT @Start_Insert
  358. PRINT ''
  359. PRINT 'The column list:'
  360. --PRINT @Column_List
  361. PRINT ''
  362. PRINT 'The SELECT statement executed to generate the INSERTs'
  363. PRINT @Actual_Values
  364. PRINT ''
  365. PRINT '*****END OF DEBUG INFORMATION*****/'
  366. PRINT ''
  367. END
  368. --PRINT '--INSERTs generated by ''sp_generate_inserts'' stored procedure written by Vyas'
  369. --PRINT '--Build number: 22'
  370. --PRINT '--Problems/Suggestions? Contact Vyas @ vyaskn@hotmail.com'
  371. --PRINT '--http://vyaskn.tripod.com'
  372. --PRINT ''
  373. --PRINT 'SET NOCOUNT ON'
  374. --PRINT ''
  375. --Determining whether to print IDENTITY_INSERT or not
  376. IF ( @IDN <> '' )
  377. BEGIN
  378. PRINT 'SET IDENTITY_INSERT ' + QUOTENAME(COALESCE(@owner,
  379. USER_NAME()))
  380. + '.' + QUOTENAME(@table_name) + ' ON'
  381. PRINT 'GO'
  382. PRINT ''
  383. END
  384. IF @disable_constraints = 1
  385. AND ( OBJECT_ID(QUOTENAME(COALESCE(@owner, USER_NAME())) + '.'
  386. + @table_name, 'U') IS NOT NULL )
  387. BEGIN
  388. IF @owner IS NULL
  389. BEGIN
  390. SELECT 'ALTER TABLE '
  391. + QUOTENAME(COALESCE(@target_table,
  392. @table_name))
  393. + ' NOCHECK CONSTRAINT ALL' AS '--Code to disable constraints temporarily'
  394. END
  395. ELSE
  396. BEGIN
  397. SELECT 'ALTER TABLE ' + QUOTENAME(@owner) + '.'
  398. + QUOTENAME(COALESCE(@target_table,
  399. @table_name))
  400. + ' NOCHECK CONSTRAINT ALL' AS '--Code to disable constraints temporarily'
  401. END
  402. PRINT 'GO'
  403. END
  404. PRINT ''
  405. PRINT 'PRINT ''Inserting values into ' + '['
  406. + RTRIM(COALESCE(@target_table, @table_name)) + ']' + ''''
  407. --All the hard work pays off here!!! You'll get your INSERT statements, when the next line executes!
  408. EXEC (@Actual_Values)
  409. PRINT 'PRINT ''Done'''
  410. PRINT ''
  411. IF @disable_constraints = 1
  412. AND ( OBJECT_ID(QUOTENAME(COALESCE(@owner, USER_NAME())) + '.'
  413. + @table_name, 'U') IS NOT NULL )
  414. BEGIN
  415. IF @owner IS NULL
  416. BEGIN
  417. SELECT 'ALTER TABLE '
  418. + QUOTENAME(COALESCE(@target_table,
  419. @table_name))
  420. + ' CHECK CONSTRAINT ALL' AS '--Code to enable the previously disabled constraints'
  421. END
  422. ELSE
  423. BEGIN
  424. SELECT 'ALTER TABLE ' + QUOTENAME(@owner) + '.'
  425. + QUOTENAME(COALESCE(@target_table,
  426. @table_name))
  427. + ' CHECK CONSTRAINT ALL' AS '--Code to enable the previously disabled constraints'
  428. END
  429. PRINT 'GO'
  430. END
  431. PRINT ''
  432. IF ( @IDN <> '' )
  433. BEGIN
  434. PRINT 'SET IDENTITY_INSERT ' + QUOTENAME(COALESCE(@owner,
  435. USER_NAME()))
  436. + '.' + QUOTENAME(@table_name) + ' OFF'
  437. PRINT 'GO'
  438. END
  439. --PRINT 'SET NOCOUNT OFF'
  440. SET NOCOUNT OFF
  441. RETURN 0 --Success. We are done!
  442. END
  443. GO

code-3:sp_generate_inserts腳本源代碼

 

在我的實(shí)際使用中,只有兩三個(gè)參數比較常用,分別是@table_name、@from和@owner,如果表的架構使用默認的dbo,則可以省略。以下是一個(gè)使用的例子:

figure-10:使用sp_generate_inserts的一個(gè)例子

 

其他參數的用法,這里就不一一解釋了。我經(jīng)常使用這個(gè)存儲過(guò)程做一些簡(jiǎn)單而少量(如數萬(wàn)行記錄以?xún)龋┑臄祿С鰧?,比前面介紹的方法方便快捷許多。但這個(gè)存儲過(guò)程支持處理一般常用的數據類(lèi)型,像XML這種類(lèi)型則不支持。還有,如果生成的數據太多太大,SSMS返回數據會(huì )很慢,甚至SSMS會(huì )掛了,這時(shí)還是使用SSMS自帶的導出腳本到文件穩妥些。如果使用生成的數據腳本文件很大,幾百MB甚至上GB,在導入時(shí),就不能直接使用SSMS直接打開(kāi)來(lái)執行了??梢允褂肧QLCMD實(shí)用工具來(lái)在執行腳本。如下面的一個(gè)例子,在D盤(pán)下有一個(gè)腳本1.sql,內容為:

  1. USE AdventureWorks2008R2
  2. GO
  3. SELECT * FROM Person.CountryRegion;
  4. GO


 

code-4:SQLMCD的測試腳本

 

在運行下輸入CMD,輸入:

 

sqlcmd -S localhost -d AdventureWorks2008R2 -i D:\1.sql


 

code-5:SQLMCD的命令

 

回車(chē)執行后如下圖,SQLCMD的詳細用法,請參考:https://msdn.microsoft.com/zh-cn/library/ms180944.aspx 和   https://msdn.microsoft.com/zh-cn/library/ms162773%28v=sql.105%29.aspx

 figure-11:SQLCMD的測試例子

 

 

方法3:

使用BCP導出導入大容量數據??梢詤㈤單业牧硪黄┛汀?a target="_blank" >BCP導出導入大容量數據實(shí)踐》。

 

以上幾種方法是我在日常工作比較常使用的數據導出導入的工具,每一種方法都有各自的優(yōu)勢和不同的使用場(chǎng)景,使用不同的方法組合,可以節省不少時(shí)間,提高工作效率,希望對您的有所幫助。如果您有更好的建議或方法歡迎告訴我!

 

 

 

 

 

本站僅提供存儲服務(wù),所有內容均由用戶(hù)發(fā)布,如發(fā)現有害或侵權內容,請點(diǎn)擊舉報。
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
一個(gè)非常有用的函數
關(guān)于long類(lèi)型的轉換
Oracle 中查詢(xún)字段詳細信息的sql 語(yǔ)句
幾個(gè)SQL小知識
DiscuzX3.1QQ登錄顯示Unknown column 'conuintoken' in 'field list'的解決辦法
sql教程SQL(StructuredQueryLanguage)是一
更多類(lèi)似文章 >>
生活服務(wù)
分享 收藏 導長(cháng)圖 關(guān)注 下載文章
綁定賬號成功
后續可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服

欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久