SELECT
BG_BUG_ID, BG_SUMMARY, BG_STATUS, BG_USER_14, BG_USER_39,
BG_USER_03, BG_PROJECT, BG_DETECTED_BY, BG_USER_09
FROM
QC11_tests_defects_db.td.BUG
WHERE
BG_BUG_ID IN ('" & Replace(Mid(Tmp_List, 1, Len(Tmp_List) - 1), ";", "',')
我收到错误:
将varchar值转换为datatype int
时转换失败
我需要帮助:)
答案 0 :(得分:0)
最佳选择是使用Tablue Valued Parameters。
首先,您需要创建一个表类型以与过程一起使用:
create type dbo.Id_udt as table (Id int not null);
go
然后,创建程序:
create procedure dbo.bug_get_fromlist (
@Id_udt as dbo.Id_udt readonly
) as
begin;
set nocount, xact_abort on;
select bg_bug_id, bg_summary, bg_status, bg_user_14, bg_user_39,
bg_user_03, bg_project, bg_detected_by, bg_user_09
from qc11_tests_defects_db.td.bug
/* or use an inner join */
where bg_bug_id in (
select id
from @Id_udt i
);
end;
go
表值参数参考:
或者,您可以拆分字符串并像表格一样使用它。
在SQL Server 2016+中,您可以使用string_split()
。
在2016年之前的SQL Server中,使用Jeff Moden的CSV Splitter表值函数和条件聚合:
select bg_bug_id, bg_summary, bg_status, bg_user_14, bg_user_39,
bg_user_03, bg_project, bg_detected_by, bg_user_09
from qc11_tests_defects_db.td.bug
where bg_bug_id in (
select s.item
from dbo.delimitedsplit8K(@tmp_list,';') s
)
拆分字符串参考:
string_split()
in SQL Server 2016 : Follow-Up #1 - Aaron Bertrand delimitedsplit8K
。
create function [dbo].[delimitedsplit8K] (
@pstring varchar(8000)
, @pdelimiter char(1)
)
returns table with schemabinding as
return
with e1(N) as (
select 1 union all select 1 union all select 1 union all
select 1 union all select 1 union all select 1 union all
select 1 union all select 1 union all select 1 union all select 1
)
, e2(N) as (select 1 from e1 a, e1 b)
, e4(N) as (select 1 from e2 a, e2 b)
, ctetally(N) as (
select top (isnull(datalength(@pstring),0))
row_number() over (order by (select null)) from e4
)
, ctestart(N1) as (
select 1 union all
select t.N+1 from ctetally t where substring(@pstring,t.N,1) = @pdelimiter
)
, ctelen(N1,L1) as (
select s.N1,
isnull(nullif(charindex(@pdelimiter,@pstring,s.N1),0)-s.N1,8000)
from ctestart s
)
select itemnumber = row_number() over(order by l.N1)
, item = substring(@pstring, l.N1, l.L1)
from ctelen l
;
go