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

沒有留言:

張貼留言

pdf.js 無法顯示部份字

有個檔案在pdf viewer套件中無法顯示內容,但下載檔案後使用工具又可以正常顯示。 本來以為是套件版本太舊的原因,於是去下載pdf viewer套件 https://github.com/mozilla/pdf.js 更新後還是一樣。 覺得應是字型缺漏的問題,於是用PDF-X...