简体   繁体   English

如何根据另一个表中的条件从一个表中创建 select 的存储过程

[英]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.

相关问题 Select 基于条件的一个表中的所有记录,而不是另一个表中的所有记录 - Select all records from one table not in another table based on a condition 如何 select 一个表中的所有记录在另一个表中不存在于另一个表中的某些条件下? - How to select all records from one table that do not exist in another table for certain condition in another table? 如何从一个表中选择行作为CSV并根据键插入另一个表? - How do I select rows from one table as CSV and insert into another table based on a key? 如何在存储过程中创建临时表和 select? - How can I create temp table and select it in a stored procedure? 如何将表变量从一个存储过程传递到另一个 - How to pass a table variable from one stored procedure to another 如何从一个表中选择具有基于第二个表的条件的值? - How to select values from one table with a condition based on the second table? 如何在MySQL中存储一个将ID从一个表插入到另一个表的过程? - How to do a procedure stored in MySQL that inserts the ID from one table to another? 如何从存储过程创建索引或在MySQL中的每个表上创建索引? - How do I create an index from a stored procedure or create index on every table in MySQL? 如何将 SQL Server 存储过程的结果集从一台服务器导出到另一台服务器上的表中? - How can i export the result set of an SQL Server stored procedure from one server into a table on another server? 从A表中选择结果并插入另一个表-存储过程 - Select results from A table & insert into another table - Stored Procedure
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM