1

我有一个简单的表格如下

CREATE TABLE [accounting].[ExtractControl](
    [SourceSchema] [varchar](50) NOT NULL,
    [SourceTable] [varchar](150) NOT NULL,
    [SourceDatabase] [varchar](50) NOT NULL)

我想使用SourceSchemaSourceTable值选择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'

我相信这可以以更优雅的方式完成!谢谢您的帮助!

4

4 回答 4

1

你问这样的事情吗?

select SourceDatabase from accounting.ExtractControl
where SourceSchema in ('xxx', 'yyy') and SourceTable in ('xxx', 'yyy');

这将返回SourceDatabase' SourceSchemaxxx' 或 'yyy' 和SourceTable'xxx' 或 'yyy' 的位置。

于 2012-12-13T10:37:14.737 回答
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演示

于 2012-12-14T10:20:07.860 回答
0

这个查询呢...

select SourceDatabase
from accounting.ExtractControl
where SourceSchema in ('xxx', 'yyy')
      and SourceTable in ('xxx', 'yyy')
      and SourceTable = SourceSchema

我没有尝试过,但我认为这是您所需要的

于 2012-12-14T10:34:27.110 回答
0
select * from accounting.ExtractControl
where SourceSchema = 'xxx' or SourceSchema = 'yyy'
于 2012-12-13T10:40:07.837 回答