sql 傳入引數為逗號分隔的字串處理方法

2022-05-02 08:00:10 字數 948 閱讀 6999

寫了個儲存過程,中間用到了類似這種寫法

select

*from

user

where id in('

1,2,3

')

其中'1,2,3'是從外面傳進來的引數,就這樣執行報錯:'1,2,3'轉換為int型別出錯,因為id是int型別的

想了個比較笨的解決方法:思路-迴圈將傳進來的引數'1,2,3'分割並轉換為int然後儲存到臨時表,之後在in裡面select id這樣就不報錯了

declare

@idnvarchar(500

);set

@id=

'4,11064';

declare

@table_userid

table

( id

int)

while(charindex('

,',@id)<>0)

begin

insert

into

@table_userid(id) values(convert(int,substring(@id,1,charindex('

,',@id)-

1)));

set@id

=stuff(@id,1,charindex('

,',@id),''

);end

insert

into

@table_userid (id) values(convert(int,@id

));select

*from

@table_userid

select

*from

user

where id in(select id from

@table_user)

結果:

歡迎指正

SQL轉換列為以逗號分隔的字串

使用for xml path來將表中某一列的資料轉換為用逗號分隔的字串,例子如下 create table rowconcat rowno int primary key,rowcode varchar 30 insert into rowconcat values 1,one insert int...

sql欄位中逗號分隔字串的判斷

例如,資料表t1中有乙個欄位playtheme存放的數值類似如下 第一行 1,2,12 第二行 22,222,2222 第三行 1,2 第四行 2,12 第五行 2 如果你想取出playtheme欄位包含 2 的行,在構造sql 引數形式 時,要是寫成下面這種形式的話,則會將五行一起取出來,顯然達不...

oracle如何拆分以逗號分隔的字串為多行

最近遇到乙個問題,需要把乙個帶有,的字串拆分成多行。通過查詢資料,這個操作需要使用以下2個關鍵知識 1.regexp substr函式 這個函式的作用是正則分隔字串,用法為 function regexp substr string,pattern,position,occurrence,modif...