是的,如果您使用的是SQL Server 2008+,那麼GarethD指出您可以使用Table Valued Parameter。
如果使用的是SQL Server 2005中,可以你可以試試這個:
--SPLIT Function
CREATE FUNCTION [dbo].[SplitUsingXML]
(
@String VARCHAR(MAX),
--If your delimiter is multi character
--then change it as VARCHAR with appropriate length
@Delimiter CHAR(1)
)
RETURNS @Results TABLE
(
parsedValue VARCHAR(MAX)
)
AS
BEGIN
DECLARE @xml XML
--Below line seems to get screwed up once i post it in Blogger :(
--please use the line as shown in the attached image. Sorry about that.
SET @XML = N'<Content><row>' + REPLACE(@String, @Delimiter, '</row><row>') + '</row></Content>'
--If your individual value length within the CSV can be more than 25 characters
--then you might want to increase it in the below statement
--pls note it won't throw an error if its more than 25 characters
--just that it would truncate and show only the first 25 character :)
INSERT INTO @Results(parsedValue)
SELECT row.value('.','VARCHAR(25)') as parsedValue
FROM @xml.nodes('//Content/row') AS RECORDS(row)
RETURN
END
GO
SELECT [name], date, para1
FROM
(
SELECT
[name], date, para1, ROW_NUMBER() OVER(PARTITION BY [date] ORDER BY [date] DESC) AS RowNum
FROM #test a
WHERE a.[Name] IN (SELECT parsedValue FROM dbo.SplitUsingXML('N1,N2', ','))
) AS WorkTable
WHERE RowNum = 1
您正在使用什麼數據庫管理系統? – GarethD 2012-07-16 09:06:57
如果您問:如何在'in'子句中設置變量'@ name'的參數,然後看到這個帖子:[參數化SQL IN子句?](http://stackoverflow.com/questions/337704/parameterizing -an-sql-in-clause) – 2012-07-16 09:18:39
作爲[this one]的類似問題(http://stackoverflow.com/a/5614275/183955),更新你的where語句。 – ndrix 2012-07-16 09:11:06