我试图将“存储”添加到报告服务中作为参数。这是我的代码:
DECLARE @Store nvarchar(MAX)
DECLARE @Delimiter char
DECLARE @x XML
IF OBJECT_ID('tempdb..#Store') IS NOT NULL
DROP TABLE #Store
CREATE TABLE #Store
(
Store_No int
)
SET @Delimiter = ','
SET @Store = '3,38'
SELECT @x = CAST('<i>' + REPLACE(@Store, @Delimiter, '</i><i>') + '</i>' AS XML)
INSERT INTO #Store (Store_No)
SELECT
x.value('.','int')
FROM
@x.nodes('/*') n(x)
当我在3中放入1个数字时,这是有效的,但是,当我放2个或更多时,它给出了我称为“替换函数需要3个参数”的错误
任何想法我该如何解决这个问题?非常感谢!
答案 0 :(得分:2)
不确定你的问题是什么,它可以在较长的序列中正常工作
DECLARE @Store nvarchar(MAX)
DECLARE @Delimiter char
DECLARE @x XML
IF OBJECT_ID('tempdb..#Store') IS NOT NULL
DROP TABLE #Store
CREATE TABLE #Store
(
Store_No int
)
SET @Delimiter = ','
SET @Store = '2,3,4,5,6'
SELECT @x = CAST('<i>' + REPLACE(@Store, @Delimiter, '</i><i>') + '</i>' AS XML)
INSERT INTO #Store (Store_No)
SELECT
x.value('.','int')
FROM
@x.nodes('/*') n(x)
select * from #Store
答案 1 :(得分:1)
完全同意@Roman Pekar -
DECLARE
@Store NVARCHAR(MAX)
, @Delimiter CHAR(1)
IF OBJECT_ID('tempdb.dbo.#Store') IS NOT NULL
DROP TABLE #Store
CREATE TABLE #Store (Store_No INT)
SELECT
@Delimiter = ','
, @Store = '2,3,4,5,6'
INSERT INTO #Store (Store_No)
SELECT t.c.value('.', 'INT')
FROM (
SELECT ID = CAST ('<t>' + REPLACE(@Store, @Delimiter, '</t><t>') + '</t>' AS XML)
) r
CROSS APPLY ID.nodes ('/t') t(c)
SELECT *
FROM #Store