繁体   English   中英

SQL Server存储过程使用临时表字段作为select的where子句

[英]SQL Server stored procedure using a temp table field as the where clause for a select

我在存储过程中有一个单列临时表#colors 该列称为color 我想用这样的SELECT语句查询另一个表tblMain

SELECT userid
FROM tblMain
WHERE color = 'red' OR color = 'white' or color = 'blue'

但我不希望WHERE子句是静态的(如上例所示),我希望它使用#colors表中的color字段来形成WHERE子句。

这有可能吗?

谢谢!

您可以在in子句中使用select:

SELECT userid FROM tblMain 
WHERE color IN (SELECT color FROM colors)

或者只是(内部)加入:

SELECT userid FROM tblMain 
INNER JOIN colors ON colors.color = tblMain.color
you can use like this

SELECT userid FROM tblMain 
WHERE color IN (SELECT distinct color FROM #colors)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM