我的问题是我有带表的DataSet,我想返回值
'where DocumentType IN (@list)'
,但@list就像:
string.Join(",",DocTypes.List1);
我试过用它
SELECT........ WHERE DocumentType IN (@list)
(在DataSet表适配器向导中),但是当我执行Fill方法时,有安娜错误:
adapter1.Fill(ds.allDocuments, string.Join(",",DocTypes.List1);
参数2应为int,而不是string。 声明也无法在数据集中工作..
任何帮助?
答案 0 :(得分:2)
您需要Split
功能,然后才能运行:
WHERE
([DocumentType IN] IN
(SELECT Item
FROM dbo.Split(@list, ',') AS DocumentSplit))
这是表值函数的可能实现:
CREATE FUNCTION [dbo].[Split]
(
@ItemList NVARCHAR(MAX),
@delimiter CHAR(1)
)
RETURNS @IDTable TABLE (Item VARCHAR(50))
AS
BEGIN
DECLARE @tempItemList NVARCHAR(MAX)
SET @tempItemList = @ItemList
DECLARE @i INT
DECLARE @Item NVARCHAR(4000)
SET @tempItemList = REPLACE (@tempItemList, ' ', '')
SET @i = CHARINDEX(@delimiter, @tempItemList)
WHILE (LEN(@tempItemList) > 0)
BEGIN
IF @i = 0
SET @Item = @tempItemList
ELSE
SET @Item = LEFT(@tempItemList, @i - 1)
INSERT INTO @IDTable(Item) VALUES(@Item)
IF @i = 0
SET @tempItemList = ''
ELSE
SET @tempItemList = RIGHT(@tempItemList, LEN(@tempItemList) - @i)
SET @i = CHARINDEX(@delimiter, @tempItemList)
END
RETURN
END
也许你需要在tableadapter的参数中手动确保DbType
是字符串: