如何根据另一个 table 中的条件从一个 table 创建一个 select 的存储过程
How do I create a stored procedure that select from one table based on condition in another table
例如,我有 2 个表。
Table A 有 Setting/isActive
Table B 有 Name/Ages/City
所以我只想在 isActive = 1 for Setting ='GetThis' in Table A
时从 B 检索记录
类似
if 'GetThis' = 1 from table A
then get records from table B
不知道您使用的是哪个 DBMS,但请查看命令 IF EXISTS
如果它应该从table B 获取所有记录,你可以试试这个;
Select b.name, b.ages, b.city from table_B b, table_A a where a.isActive = 1 and a.Setting ='GetThis'
我最后做了这个
CREATE PROCEDURE GetAllRecords
@isActive BIT
SET @isActive = (Select isActive FROM TableA WHERE Setting = 'GetThis')
AS
IF @isActive = 1
BEGIN
SELECT * FROM TableB
END
GO;
例如,我有 2 个表。
Table A 有 Setting/isActive
Table B 有 Name/Ages/City
所以我只想在 isActive = 1 for Setting ='GetThis' in Table A
时从 B 检索记录类似
if 'GetThis' = 1 from table A
then get records from table B
不知道您使用的是哪个 DBMS,但请查看命令 IF EXISTS
如果它应该从table B 获取所有记录,你可以试试这个;
Select b.name, b.ages, b.city from table_B b, table_A a where a.isActive = 1 and a.Setting ='GetThis'
我最后做了这个
CREATE PROCEDURE GetAllRecords
@isActive BIT
SET @isActive = (Select isActive FROM TableA WHERE Setting = 'GetThis')
AS
IF @isActive = 1
BEGIN
SELECT * FROM TableB
END
GO;