2009年11月4日 星期三

tsql 多值的參數傳入stored procedure

在tsql中,如果單一條件要符合多個條件值時,都會以 in 的方式
select * from test where col1 in ( '1234','5678,'abcd')

如果這個多值的參數想要傳入做為stored proedure 的input parameter時,看來是不可行的
有二種方法:
1. 用charindex
select * from test where charindex ( col1 , '1234,5678,abcd') > 0

2.寫一個table function,
select * from test
where col1 in ( select valuString from dbo.zfnRtnStringTable ( '1234,5678,abcd' ) )

以下function來自一段不曉得在哪看過的script...,引用一下
CREATE FUNCTION dbo.zfnRtnStringTable ( @key varchar(8000))
RETURNS @rtnTable TABLE ( stringValue varchar(10))
AS
BEGIN
DECLARE @strValue varchar(10), @pos int
SET @key = @key+ ','
SET @pos= charindex(',', @key, 1)
IF replace(@key, ',', '') <> ''
BEGIN
WHILE @pos> 0
BEGIN
SET @strValue = left(@key, @pos- 1)
IF @strValue <> ''
BEGIN
insert into @rtnTable values( @strValue )
END
SET @key = right(@key, len(@key) - @pos)
SET @pos= charindex(',', @key, 1)
END
END
RETURN
END

沒有留言:

張貼留言

IIS 網站無法下載中文檔名檔案因為取消高位元字元

 網址+中文檔案名稱方式下載檔案顯示 404 - 找不到檔案或目錄。 但英數字檔案名稱則可下載。 因為設定GCB IIS,其中1項 將高位元字元預設取消勾選了。 27 TWGCB-04-014-0028 要求篩選與其他限制模組 允許高位元字元 這項原則設定決定查詢...