如何将数字数组传递给存储过程?
How to pass an array of numbers to a stored procedure?
我想编写一个存储过程,可以接收一个或多个员工 ID 和基于这些 ID 的 return 数据。每次调用 sproc 时,员工 ID 的数量都会有所不同。
如何将存储过程写入 return 传入的每个员工 ID 的分支地址,其中员工 ID 的数量是可变的?
员工Table
- ID
- 姓名
- Branch_ID
分支机构Table
- ID
- 姓名
- 地址
我们所做的只是传入一个分隔列表。所以 1,2,3,42,55,6666 和 nvarchar(max)。
然后我们有一个用户定义的函数 returns a table 我们加入了它。
Google 搜索 t sql 使用定界符拆分字符串,您会找到示例。
select * 来自 dbo.udfSplit(@inputfromparameters, ",") 内连接....
如果您有 SQL 2008 或更高版本 运行,那么我建议您使用 table 类型。然后您可以将 table 传递给存储过程。这是我制作的一个小例子来证明这一点。
create database tmpDB;
go
use tmpDB;
go
create table tblEmployee
(
ID int identity(1, 1)
primary key
not null,
Name nvarchar(200),
Branch_ID int
);
go
insert into dbo.tblEmployee
(Name, Branch_ID)
values (N'Brian', 1),
(N'Mary', 1),
(N'Peter', 2),
(N'Sonya', 2),
(N'Roland', 1),
(N'Tom', 3),
(N'Sam', 3),
(N'Ryan', 3),
(N'Julia', 3),
(N'Tim', 1),
(N'Eva', 2);
go
select *
from dbo.tblEmployee;
go
create type typEmployee as table
(
Name nvarchar(200),
BranchID int
);
go
grant exec on type::typEmployee to public;
go
create procedure spInsertEmployees
(
@NewEmployees typEmployee readonly
)
as
begin
merge tblEmployee as target
using
(
select Name,
BranchID
from @NewEmployees
) as source (Name, BranchID)
on (
target.Name = source.Name
and target.Branch_ID = source.BranchID
)
when not matched then
insert (Name, Branch_ID)
values (
source.Name,
source.BranchID
);
end;
go
grant execute on spInsertEmployees to public;
go
declare @tmpTable typEmployee;
insert into @tmpTable
(Name, BranchID)
values (N'NewEmployee', 1),
(N'NewEmployee', 2),
(N'NewEmployee', 3),
(N'Sonya', 2),
(N'Tim', 1);
exec dbo.spInsertEmployees
@NewEmployees = @tmpTable;
select *
from dbo.tblEmployee;
go
use master;
go
drop database tmpDB;
go
我想编写一个存储过程,可以接收一个或多个员工 ID 和基于这些 ID 的 return 数据。每次调用 sproc 时,员工 ID 的数量都会有所不同。
如何将存储过程写入 return 传入的每个员工 ID 的分支地址,其中员工 ID 的数量是可变的?
员工Table
- ID
- 姓名
- Branch_ID
分支机构Table
- ID
- 姓名
- 地址
我们所做的只是传入一个分隔列表。所以 1,2,3,42,55,6666 和 nvarchar(max)。
然后我们有一个用户定义的函数 returns a table 我们加入了它。
Google 搜索 t sql 使用定界符拆分字符串,您会找到示例。
select * 来自 dbo.udfSplit(@inputfromparameters, ",") 内连接....
如果您有 SQL 2008 或更高版本 运行,那么我建议您使用 table 类型。然后您可以将 table 传递给存储过程。这是我制作的一个小例子来证明这一点。
create database tmpDB;
go
use tmpDB;
go
create table tblEmployee
(
ID int identity(1, 1)
primary key
not null,
Name nvarchar(200),
Branch_ID int
);
go
insert into dbo.tblEmployee
(Name, Branch_ID)
values (N'Brian', 1),
(N'Mary', 1),
(N'Peter', 2),
(N'Sonya', 2),
(N'Roland', 1),
(N'Tom', 3),
(N'Sam', 3),
(N'Ryan', 3),
(N'Julia', 3),
(N'Tim', 1),
(N'Eva', 2);
go
select *
from dbo.tblEmployee;
go
create type typEmployee as table
(
Name nvarchar(200),
BranchID int
);
go
grant exec on type::typEmployee to public;
go
create procedure spInsertEmployees
(
@NewEmployees typEmployee readonly
)
as
begin
merge tblEmployee as target
using
(
select Name,
BranchID
from @NewEmployees
) as source (Name, BranchID)
on (
target.Name = source.Name
and target.Branch_ID = source.BranchID
)
when not matched then
insert (Name, Branch_ID)
values (
source.Name,
source.BranchID
);
end;
go
grant execute on spInsertEmployees to public;
go
declare @tmpTable typEmployee;
insert into @tmpTable
(Name, BranchID)
values (N'NewEmployee', 1),
(N'NewEmployee', 2),
(N'NewEmployee', 3),
(N'Sonya', 2),
(N'Tim', 1);
exec dbo.spInsertEmployees
@NewEmployees = @tmpTable;
select *
from dbo.tblEmployee;
go
use master;
go
drop database tmpDB;
go