[英]How do I create a stored procedure that select from one table based on condition in another table
For Example, I have 2 tables.例如,我有 2 个表。
Table A has Setting/isActive表 A 有 Setting/isActive
Table B has Name/Ages/City表 B 有姓名/年龄/城市
So I want to retrieve records from B only when isActive = 1 for Setting ='GetThis' in Table A所以我只想在 isActive = 1 for Setting ='GetThis' in Table A 时从 B 检索记录
something like就像是
if 'GetThis' = 1 from table A
then get records from table B
Don't know which DBMS you're using, but look into the command IF EXISTS
不知道您使用的是哪个 DBMS,但查看命令IF EXISTS
If it should get all records from table B, you can try this;如果它应该从表 B 中获取所有记录,你可以试试这个;
Select b.name, b.ages, b.city from table_B b, table_A a where a.isActive = 1 and a.Setting ='GetThis'
I ended up doing this我最终这样做了
CREATE PROCEDURE GetAllRecords
@isActive BIT
SET @isActive = (Select isActive FROM TableA WHERE Setting = 'GetThis')
AS
IF @isActive = 1
BEGIN
SELECT * FROM TableB
END
GO;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.