我正在开发一个包含商店的客户,产品,时间表等的数据库。我正在处理的问题涉及创建一个将“开/关”列更改为关闭的过程(默认情况下产品可用(1),此过程将其更改为0)我已将程序写入正常:
create proc p_fudgemart_deactivate_product
(
@product_id int
)
as
begin
update fudgemart_products
set product_is_active = 0
where product_id = @product_id
end
但是当我们获得产品名称时会出现问题,并且需要编写一个select语句来将该产品更改为不可用。我知道这需要使用变量,但我无法弄清楚如何将变量设置为该产品的产品ID。我正在思考以下几点:
Declare @prod_name_id int
set @prod_name_id= (select product_id from fudgemart_products
where product_name = 'Slot Screwdriver')
execute p_fudgemart_deactivate_product product_id @prod_name_id
我可以在我的变量声明中使用select吗?
答案 0 :(得分:1)
declare @prod_name_id int
select @prod_name_id = product_id
from fudgemart_products
where product_name = 'Slot Screwdriver'
exec p_fudgemart_deactivate_product
@product_id = @prod_name_id
答案 1 :(得分:0)
如果您使用的是SQL Server 2008或更高版本,则可以在一个语句中声明和分配:
DECLARE @prod_name_id int = ( SELECT product_id
FROM fudgemart_products
WHERE product_name = 'Slot Screwdriver'
);
EXECUTE p_fudgemart_deactivate_product @product_id = @prod_name_id;