我知道这很脏但是有一种方法可以用这种确切的格式发送csv数据(回车分隔):
1,2,"Bla1"
3,4,"Bla2"
到存储过程并将其插入到#temp表中。
骨架:
IF EXISTS (SELECT *
FROM sys.objects
WHERE type = 'P' AND name = 'USP_Test')
DROP PROCEDURE USP_Test
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE USP_Test
@CsvData NVARCHAR(MAX)
AS
BEGIN
SET NOCOUNT ON;
CREATE TABLE #Temp
(
Number1 FLOAT,
Number2 FLOAT,
String NVARCHAR(255)
)
-- insert @CsvData into #Temp
END
我喜欢这种情况的表值参数,但我只能控制发送nvarchar
。
答案 0 :(得分:2)
您可以使用XML进行创建,但如果输入数据很大,则可能会出现性能问题。
DECLARE @CsvData nvarchar(max) =
'1,2,"Bla1"
3,4,"Bla2"
5,6,"Bla3"
7,8,"Bla4"
9,10,"Bla5"
11,12,"Bla6"
13,14,"Bla7"
15,16,"Bla8"
16,18,"Bla9"'
DECLARE @xml xml
SELECT @xml = CAST('<row>' + REPLACE('<s>' + REPLACE(@CsvData,',','</s><s>')+'</s>',CHAR(13)+CHAR(10),'</s></row><row><s>') + '</row>' as xml)
SELECT t.v.value('s[1]','float') as Number1,
t.v.value('s[2]','float') as Number2,
t.v.value('s[3]','nvarchar(255)') as String
FROM @xml.nodes('/row') as t(v)
输出:
Number1 Number2 String
1 2 "Bla1"
3 4 "Bla2"
5 6 "Bla3"
7 8 "Bla4"
9 10 "Bla5"
11 12 "Bla6"
13 14 "Bla7"
15 16 "Bla8"
16 18 "Bla9"