在 where 子句中使用 table 值
Use table values in a where clause
我正在处理一个存储过程,它将有一个相当大的 WHERE 子句。为了简化它,我试图利用我可以在我的代码中使用的 SQL table。
在考虑使用 table 之前,我的查询应该是这样的:
Select * from dbo.SomeTable
where (Val1 = 'ME' and val2 = 17 and val3 = 'Apple')
or (Val1 = 'GE' and Val2= 7 and Val3 = 'Google')
我想使用下面的 table 来动态完成类似的事情
Table1
Val1 Val2 Val3
ME 17 Apple
GE 7 Google
有什么好的方法吗?
谢谢。
除非您过于简化了您的意图,否则您只需要加入您的表格
Select st.*
from dbo.SomeTable st
join Table1 t on t.val1=st.val1 and t.val2=st.val2 and t.val3=st.val3
或者您可以使用 exists
select *
from dbo.SomeTable st
where exists (select * from Table1 t where t.val1=st.val1 and t.val2=st.val2 and t.val3=st.val3)
我正在处理一个存储过程,它将有一个相当大的 WHERE 子句。为了简化它,我试图利用我可以在我的代码中使用的 SQL table。
在考虑使用 table 之前,我的查询应该是这样的:
Select * from dbo.SomeTable
where (Val1 = 'ME' and val2 = 17 and val3 = 'Apple')
or (Val1 = 'GE' and Val2= 7 and Val3 = 'Google')
我想使用下面的 table 来动态完成类似的事情
Table1
Val1 Val2 Val3
ME 17 Apple
GE 7 Google
有什么好的方法吗?
谢谢。
除非您过于简化了您的意图,否则您只需要加入您的表格
Select st.*
from dbo.SomeTable st
join Table1 t on t.val1=st.val1 and t.val2=st.val2 and t.val3=st.val3
或者您可以使用 exists
select *
from dbo.SomeTable st
where exists (select * from Table1 t where t.val1=st.val1 and t.val2=st.val2 and t.val3=st.val3)