好的。由于您可以添加一个新的存储过程,我建议将所有值打包,并将其作为 XML 传送到您的存储过程。
你可以在这里找到一个例子:http:
//granadacoder.wordpress.com/2009/01/27/bulk-insert-example-using-an-idatareader-to-strong-dataset-to-sql-server-xml/
好消息是,我的示例较旧并且编码为 Sql Server 2000(使用 OPENXML)。
..
这比将 300 个参数发送到存储过程(恕我直言)要好。
另一个优点是,如果您有超过 1 行数据,您也可以将其发送下来。
……
它的“要点”:
首先,您可以在此处获取 2000 个“pubs”数据库:
http://www.microsoft.com/en-us/download/details.aspx?id=23654
现在添加这个存储过程:
/* 美国药典 */
DROP PROCEDURE dbo.uspTitleUpsert
GO
CREATE PROCEDURE dbo.uspTitleUpsert (
@xml_doc TEXT ,
@numberRowsAffected int output --return
)
AS
SET NOCOUNT ON
DECLARE @hdoc INT -- handle to XML doc
DECLARE @errorTracker int -- used to "remember" the @@ERROR
DECLARE @updateRowCount int
DECLARE @insertRowCount int
--Create an internal representation of the XML document.
EXEC sp_xml_preparedocument @hdoc OUTPUT, @XML_Doc
-- build a table (variable table) to store the xml-based result set
DECLARE @titleupdate TABLE (
identityid int IDENTITY (1,1) ,
title_id varchar(6) ,
title varchar(80) ,
type varchar(32) ,
pub_id varchar(32) ,
price money ,
advance money ,
royalty varchar(32) ,
ytd_sales varchar(32) ,
notes TEXT ,
pubdate datetime
)
--the next call will take the info IN the @hdoc(with is the holder for @xml_doc), and put it IN a variableTable
INSERT @titleupdate
(
title_id ,
title ,
type ,
pub_id ,
price ,
advance ,
royalty ,
ytd_sales ,
notes ,
pubdate
)
SELECT
title_id ,
title ,
type ,
pub_id ,
price ,
advance ,
royalty ,
ytd_sales ,
notes ,
getdate() /*dbo.udf_convert_xml_date_to_datetime (pubdate)*/
FROM
-- use the correct XPath .. the second arg ("2" here) distinquishes
-- between textnode or an attribute, most times with
--.NET typed datasets, its a "2"
--This xpath MUST match the syntax of the DataSet
OPENXML (@hdoc, '/TitlesDS/Titles', 2) WITH (
title_id varchar(6) ,
title varchar(80) ,
type varchar(32) ,
pub_id varchar(32) ,
price money ,
advance money ,
royalty varchar(32) ,
ytd_sales varchar(32) ,
notes TEXT ,
pubdate varchar(32)
)
EXEC sp_xml_removedocument @hdoc
select * from @titleupdate
SET NOCOUNT OFF
Update
dbo.titles
set
title = vart.title ,
type = vart.type ,
pub_id = vart.pub_id ,
price = vart.price ,
advance = vart.advance ,
royalty = vart.royalty ,
ytd_sales = vart.ytd_sales ,
notes = vart.notes ,
pubdate = vart.pubdate
FROM
@titleupdate vart , dbo.titles realTable
WHERE
(rtrim(upper(realTable.title_id))) = ltrim(rtrim(upper(vart.title_id)))
and
exists
(
select null from dbo.titles innerRealTable where (rtrim(upper(innerRealTable.title_id))) = ltrim(rtrim(upper(vart.title_id)))
)
Select @updateRowCount = @@ROWCOUNT
INSERT INTO dbo.titles
(
title_id ,
title ,
type ,
pub_id ,
price ,
advance ,
royalty ,
ytd_sales ,
notes ,
pubdate
)
Select
title_id ,
title ,
type ,
pub_id ,
price ,
advance ,
royalty ,
ytd_sales ,
notes ,
pubdate
FROM
@titleupdate tu
WHERE
not exists
(
select null from dbo.titles innerRealTable where (rtrim(upper(innerRealTable.title_id))) = ltrim(rtrim(upper(tu.title_id)))
)
Select @insertRowCount = @@ROWCOUNT
print '/@insertRowCount/'
select @insertRowCount
print ''
print '/@updateRowCount/'
select @updateRowCount
print ''
select @numberRowsAffected = @insertRowCount + @updateRowCount
--select * from titles
SET NOCOUNT OFF
GO
--GRANT EXECUTE on dbo.uspTitleUpsert TO pubsuser
GO
/* 示例用法 */
EXEC dbo.uspTitleUpsert
'
<TitlesDS>
<Titles>
<title_id>PN3333</title_id>
<title>Peanut Cooking</title>
<type>trad_cook</type>
<pub_id>0877</pub_id>
<price>3.33</price>
<advance>4444.00</advance>
<royalty>1</royalty>
<ytd_sales>33</ytd_sales>
<notes>Peanut Cooking Notes</notes>
<pubdate></pubdate>
</Titles>
<Titles>
<title_id>SSMS4444</title_id>
<title>Sql Server Management Studio</title>
<type>programming</type>
<pub_id>0877</pub_id>
<price>13.33</price>
<advance>5444.00</advance>
<royalty>2</royalty>
<ytd_sales>33</ytd_sales>
<notes>Sql Server Management Studio Notes</notes>
<pubdate></pubdate>
</Titles>
</TitlesDS>
'
, 0