SSMS:如何从Excel导入(复制/粘贴)数据 [英] SSMS: How to import (Copy/Paste) data from excel
本文介绍了SSMS:如何从Excel导入(复制/粘贴)数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何从excel复制/粘贴以下定界数据(默认情况下,使用制表符分隔):
How to Copy/Paste following delimited data (by default delimited with tab) from excel:
declare @t_values nvarchar(max) =
N'
NULL 490366 NULL NULL
NULL 490400 NULL NULL
NULL 490402 NULL NULL
483061 490404 10 abc1
NULL 490406 NULL NULL
9766167 490408 3 abc2'
;
到我的临时表:
CREATE TABLE #insertTable
( transaction_id int
,user_id int
,purchase_price decimal(8,2)
,mess varchar(8)
);
?
NB!不使用 OPENROWSET
INSERT INTO
:
SOLUTIONS FOR INSERT INTO
:
- QUERY
- PROCEDURE
- SNIPPET (fast way to creae snippets HERE)
选择进入
的解决方案:
SOLUTIONS FOR SELECT INTO
:
SNIPPET (创建creae片段的快速方法 HERE )
SNIPPET (fast way to creae snippets HERE)
PS 如果您使用的是旧版SQL Server(数据库兼容性设置低于130),则需要分别 STRING_SPLIT
函数。我建议遵循解决方案。
P.S. If you have old SQL Server version (database compatibility set is under 130), you will need separately STRING_SPLIT
function. I recommend following solution.
推荐答案
请尝试以下操作:
-- NB! Script do NOT write any ifo to any table
-- to perform insert please copy/paste script output and execute
declare @tbl_name nvarchar(128) = '#insertTable'; -- Destination Table Name + [(column names)]
declare @dlm nvarchar(128) = char(9); -- Delimiter: HT = char(9) = HorizontalTab
declare @tbl nvarchar(max) = -- Paste Table Values here (from excel for instance)
N'NULL 490366 NULL NULL
NULL 490400 NULL NULL
NULL 490402 NULL NULL
483061 490404 10 abc1
NULL 490406 NULL NULL
9766167 490408 3 abc2'
;
select N'insert into '
+ @tbl_name -- table name
+ ' VALUES('
+ replace -- replace 'NULL' by NULL
( ''''
+ replace -- surround values by quotes 'value1','value2'...
( value
, @dlm -- Delimiter: HT = char(9) = HorizontalTab
, ''','''
)
+ ''''
, '''NULL'''
, 'NULL'
)
+ ');' as insertquery
--into #t
from fn_split_string -- insert Line per Row into table
( replace -- replace CR + LF by LF (CR - Carriage Return, LF - Line Feed)
( @tbl -- Paste Table Values here (from excel for instance)
, char(13)+char(10) -- CR + LF
, char(10) -- LF
)
,char(10)
)
where len(value)>0 -- skip empty rows
这篇关于SSMS:如何从Excel导入(复制/粘贴)数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文