我有一个简单的表格如下
CREATE TABLE [accounting].[ExtractControl](
[SourceSchema] [varchar](50) NOT NULL,
[SourceTable] [varchar](150) NOT NULL,
[SourceDatabase] [varchar](50) NOT NULL)
我想使用 SourceSchema 和 SourceTable 值选择 SourceDatabase
目前我正在进行多项查询,如下所示
Select @s1 = SourceDatabase from accounting.ExtractControl where SourceSchema = 'xxx' and SourceTable = 'xxx'
Select @s2 = SourceDatabase from accounting.ExtractControl where SourceSchema = 'yyy' and SourceTable = 'yyy'
Select @s3 = SourceDatabase from accounting.ExtractControl where SourceSchema = 'xxx' and SourceTable = 'yyy'
我相信这可以用更优雅的方式完成!谢谢你的帮助!
答案 0 :(得分:1)
你在问这样的事吗?
select SourceDatabase from accounting.ExtractControl
where SourceSchema in ('xxx', 'yyy') and SourceTable in ('xxx', 'yyy');
这将返回SourceDatabase
,其中SourceSchema
为'xxx'或'yyy',SourceTable
为'xxx'或'yyy'。
答案 1 :(得分:1)
选项
SELECT @s1 = MAX(CASE WHEN SourceSchema = 'xxx' and SourceTable = 'xxx' THEN SourceDatabase END),
@s2 = MAX(CASE WHEN SourceSchema = 'yyy' and SourceTable = 'yyy' THEN SourceDatabase END),
@s3 = MAX(CASE WHEN SourceSchema = 'xxx' and SourceTable = 'yyy' THEN SourceDatabase END)
FROM accounting.ExtractControl
SQL Fiddle上的演示
答案 2 :(得分:0)
select * from accounting.ExtractControl
where SourceSchema = 'xxx' or SourceSchema = 'yyy'
答案 3 :(得分:0)
这个查询怎么样......
select SourceDatabase
from accounting.ExtractControl
where SourceSchema in ('xxx', 'yyy')
and SourceTable in ('xxx', 'yyy')
and SourceTable = SourceSchema
我没试过,但我认为这就是你所需要的