以这个小脚本为例
create table Customers
(CustomerId int not null
,StoreId int
,primary key(CustomerId)
)
go
create table Stores
(StoreId int
,StoreName varchar(50)
,primary key(StoreId)
)
go
alter table Customers
add foreign key (StoreId)
references Stores(StoreId)
go
insert into Stores
values (1,'Biggest Store')
,(2,'Mediumest Store')
,(3,'Smaller Store')
,(4,'Smallest Store')
go
insert into Customers
values
(1,1)
,(2,1)
,(3,2)
,(4,3)
,(5,4)
假设我想使用一个名为的存储更新spUpdateCustomerInformation
,它带有两个参数:一个是 the CustomerId
,另一个是用户希望更新的商店的名称(来自 Stores 表的 StoreName)。所以在Customers
表中,记录(1,1)
意味着客户 1 从 购买了东西Biggest Store
。我想将两个参数传递给存储过程,例如spUpdateCustomerInformation 1,'Smallest Store'
,在运行存储过程之后,记录(1,1)
就是现在
(1,4)
。我的尝试
create proc spUpdateCustomerInformation
@CustomerId int
,@StoreName varchar(50)
as begin
update c
set c.StoreId = s.StoreId
from customers as c
inner join Stores as s
on s.StoreId = c.StoreId
where c.CustomerID = @CustomerId
and s.StoreName = @StoreName --failing here
end
返回 0 行更新。我知道我可以简单地将 StoreId 作为第二个参数传递给存储过程,但我想知道这样做是否明智/可能,将其StoreName
作为第二个参数传递。假设这不是一个人为的场景,并且CustomerId
也必须作为参数传递。