SELECT A.GAS_ID, A.PAY_TYPE, B.INV_NO, B.INV_NAME
FROM PAY_RECORD A
INNER JOIN #TEMP_GASGUNTOINV B ON (A.GAS_ID=B.GAS_ID AND A.GUN_NO=B.GUN_NO)
WHERE A.GAS_ID IN
( CASE WHEN (@DeptType=1) THEN (SELECT GAS_ID FROM GAS)
WHEN (@DeptType=2) THEN (SELECT GAS_ID FROM GAS WHERE SUPER_DEPT_ID=@DeptNo) ELSE @DeptNo
END)
我这个语句怎么不对,提示是:
子查询返回的值多于一个。当子查询跟随在 =、!=、<、<=、>、>= 之后,或子查询用作表达式时,这种情况是不允许的。
语句已终止。
我怀疑是
( CASE WHEN (@DeptType=1) THEN (SELECT GAS_ID FROM GAS)
WHEN (@DeptType=2) THEN (SELECT GAS_ID FROM GAS WHERE SUPER_DEPT_ID=@DeptNo) ELSE @DeptNo
END)
里的问题,大家帮我看看是吗,该怎么改,谢谢了!
13 个解决方案
#1
在then 后不能使用结果集合,参考一下:
CASE
计算条件列表并返回多个可能结果表达式之一。
CASE 具有两种格式:
简单 CASE 函数将某个表达式与一组简单表达式进行比较以确定结果。
CASE 搜索函数计算一组布尔表达式以确定结果。
两种格式都支持可选的 ELSE 参数。
语法
简单 CASE 函数:
CASE input_expression
WHEN when_expression THEN result_expression
[ ...n ]
[
ELSE else_result_expression
END
CASE 搜索函数:
CASE
WHEN Boolean_expression THEN result_expression
[ ...n ]
[
ELSE else_result_expression
END
参数
input_expression
是使用简单 CASE 格式时所计算的表达式。Input_expression 是任何有效的 Microsoft® SQL Server™ 表达式。
WHEN when_expression
使用简单 CASE 格式时 input_expression 所比较的简单表达式。When_expression 是任意有效的 SQL Server 表达式。Input_expression 和每个 when_expression 的数据类型必须相同,或者是隐性转换。
n
占位符,表明可以使用多个 WHEN when_expression THEN result_expression 子句或 WHEN Boolean_expression THEN result_expression 子句。
THEN result_expression
当 input_expression = when_expression 取值为 TRUE,或者 Boolean_expression 取值为 TRUE 时返回的表达式。result expression 是任意有效的 SQL Server 表达式。
ELSE else_result_expression
当比较运算取值不为 TRUE 时返回的表达式。如果省略此参数并且比较运算取值不为 TRUE,CASE 将返回 NULL 值。Else_result_expression 是任意有效的 SQL Server 表达式。Else_result_expression 和所有 result_expression 的数据类型必须相同,或者必须是隐性转换。
WHEN Boolean_expression
使用 CASE 搜索格式时所计算的布尔表达式。Boolean_expression 是任意有效的布尔表达式。
结果类型
从 result_expressions 和可选 else_result_expression 的类型集合中返回最高的优先规则类型。
CASE
计算条件列表并返回多个可能结果表达式之一。
CASE 具有两种格式:
简单 CASE 函数将某个表达式与一组简单表达式进行比较以确定结果。
CASE 搜索函数计算一组布尔表达式以确定结果。
两种格式都支持可选的 ELSE 参数。
语法
简单 CASE 函数:
CASE input_expression
WHEN when_expression THEN result_expression
[ ...n ]
[
ELSE else_result_expression
END
CASE 搜索函数:
CASE
WHEN Boolean_expression THEN result_expression
[ ...n ]
[
ELSE else_result_expression
END
参数
input_expression
是使用简单 CASE 格式时所计算的表达式。Input_expression 是任何有效的 Microsoft® SQL Server™ 表达式。
WHEN when_expression
使用简单 CASE 格式时 input_expression 所比较的简单表达式。When_expression 是任意有效的 SQL Server 表达式。Input_expression 和每个 when_expression 的数据类型必须相同,或者是隐性转换。
n
占位符,表明可以使用多个 WHEN when_expression THEN result_expression 子句或 WHEN Boolean_expression THEN result_expression 子句。
THEN result_expression
当 input_expression = when_expression 取值为 TRUE,或者 Boolean_expression 取值为 TRUE 时返回的表达式。result expression 是任意有效的 SQL Server 表达式。
ELSE else_result_expression
当比较运算取值不为 TRUE 时返回的表达式。如果省略此参数并且比较运算取值不为 TRUE,CASE 将返回 NULL 值。Else_result_expression 是任意有效的 SQL Server 表达式。Else_result_expression 和所有 result_expression 的数据类型必须相同,或者必须是隐性转换。
WHEN Boolean_expression
使用 CASE 搜索格式时所计算的布尔表达式。Boolean_expression 是任意有效的布尔表达式。
结果类型
从 result_expressions 和可选 else_result_expression 的类型集合中返回最高的优先规则类型。
#2
单 CASE 函数:
计算 input_expression,然后按指定顺序对每个 WHEN 子句的 input_expression = when_expression 进行计算。
返回第一个取值为 TRUE 的 (input_expression = when_expression) 的 result_expression。
如果没有取值为 TRUE 的 input_expression = when_expression,则当指定 ELSE 子句时 SQL Server 将返回 else_result_expression;若没有指定 ELSE 子句,则返回 NULL 值。
CASE 搜索函数:
按指定顺序为每个 WHEN 子句的 Boolean_expression 求值。
返回第一个取值为 TRUE 的 Boolean_expression 的 result_expression。
如果没有取值为 TRUE 的 Boolean_expression,则当指定 ELSE 子句时 SQL Server 将返回 else_result_expression;若没有指定 ELSE 子句,则返回 NULL 值。
示例
A. 使用带有简单 CASE 函数的 SELECT 语句
在 SELECT 语句中,简单 CASE 函数仅检查是否相等,而不进行其它比较。下面的示例使用 CASE 函数更改图书分类显示,以使其更易于理解。
USE pubs
GO
SELECT Category =
CASE type
WHEN 'popular_comp' THEN 'Popular Computing'
WHEN 'mod_cook' THEN 'Modern Cooking'
WHEN 'business' THEN 'Business'
WHEN 'psychology' THEN 'Psychology'
WHEN 'trad_cook' THEN 'Traditional Cooking'
ELSE 'Not yet categorized'
END,
CAST(title AS varchar(25)) AS 'Shortened Title',
price AS Price
FROM titles
WHERE price IS NOT NULL
ORDER BY type, price
COMPUTE AVG(price) BY type
GO
下面是结果集:
Category Shortened Title Price
------------------- ------------------------- --------------------------
Business You Can Combat Computer S 2.99
Business Cooking with Computers: S 11.95
Business The Busy Executive's Data 19.99
Business Straight Talk About Compu 19.99
avg
==========================
13.73
Category Shortened Title Price
------------------- ------------------------- --------------------------
Modern Cooking The Gourmet Microwave 2.99
Modern Cooking Silicon Valley Gastronomi 19.99
avg
==========================
11.49
Category Shortened Title Price
------------------- ------------------------- --------------------------
Popular Computing Secrets of Silicon Valley 20.00
Popular Computing But Is It User Friendly? 22.95
avg
==========================
21.48
Category Shortened Title Price
------------------- ------------------------- --------------------------
Psychology Life Without Fear 7.00
Psychology Emotional Security: A New 7.99
Psychology Is Anger the Enemy? 10.95
Psychology Prolonged Data Deprivatio 19.99
Psychology Computer Phobic AND Non-P 21.59
avg
==========================
13.50
Category Shortened Title Price
------------------- ------------------------- --------------------------
Traditional Cooking Fifty Years in Buckingham 11.95
Traditional Cooking Sushi, Anyone? 14.99
Traditional Cooking Onions, Leeks, and Garlic 20.95
avg
==========================
15.96
(21 row(s) affected)
计算 input_expression,然后按指定顺序对每个 WHEN 子句的 input_expression = when_expression 进行计算。
返回第一个取值为 TRUE 的 (input_expression = when_expression) 的 result_expression。
如果没有取值为 TRUE 的 input_expression = when_expression,则当指定 ELSE 子句时 SQL Server 将返回 else_result_expression;若没有指定 ELSE 子句,则返回 NULL 值。
CASE 搜索函数:
按指定顺序为每个 WHEN 子句的 Boolean_expression 求值。
返回第一个取值为 TRUE 的 Boolean_expression 的 result_expression。
如果没有取值为 TRUE 的 Boolean_expression,则当指定 ELSE 子句时 SQL Server 将返回 else_result_expression;若没有指定 ELSE 子句,则返回 NULL 值。
示例
A. 使用带有简单 CASE 函数的 SELECT 语句
在 SELECT 语句中,简单 CASE 函数仅检查是否相等,而不进行其它比较。下面的示例使用 CASE 函数更改图书分类显示,以使其更易于理解。
USE pubs
GO
SELECT Category =
CASE type
WHEN 'popular_comp' THEN 'Popular Computing'
WHEN 'mod_cook' THEN 'Modern Cooking'
WHEN 'business' THEN 'Business'
WHEN 'psychology' THEN 'Psychology'
WHEN 'trad_cook' THEN 'Traditional Cooking'
ELSE 'Not yet categorized'
END,
CAST(title AS varchar(25)) AS 'Shortened Title',
price AS Price
FROM titles
WHERE price IS NOT NULL
ORDER BY type, price
COMPUTE AVG(price) BY type
GO
下面是结果集:
Category Shortened Title Price
------------------- ------------------------- --------------------------
Business You Can Combat Computer S 2.99
Business Cooking with Computers: S 11.95
Business The Busy Executive's Data 19.99
Business Straight Talk About Compu 19.99
avg
==========================
13.73
Category Shortened Title Price
------------------- ------------------------- --------------------------
Modern Cooking The Gourmet Microwave 2.99
Modern Cooking Silicon Valley Gastronomi 19.99
avg
==========================
11.49
Category Shortened Title Price
------------------- ------------------------- --------------------------
Popular Computing Secrets of Silicon Valley 20.00
Popular Computing But Is It User Friendly? 22.95
avg
==========================
21.48
Category Shortened Title Price
------------------- ------------------------- --------------------------
Psychology Life Without Fear 7.00
Psychology Emotional Security: A New 7.99
Psychology Is Anger the Enemy? 10.95
Psychology Prolonged Data Deprivatio 19.99
Psychology Computer Phobic AND Non-P 21.59
avg
==========================
13.50
Category Shortened Title Price
------------------- ------------------------- --------------------------
Traditional Cooking Fifty Years in Buckingham 11.95
Traditional Cooking Sushi, Anyone? 14.99
Traditional Cooking Onions, Leeks, and Garlic 20.95
avg
==========================
15.96
(21 row(s) affected)
#3
B. 使用带有简单 CASE 函数和 CASE 搜索函数的 SELECT 语句
在 SELECT 语句中,CASE 搜索函数允许根据比较值在结果集内对值进行替换。下面的示例根据图书的价格范围将价格(money 列)显示为文本注释。
USE pubs
GO
SELECT 'Price Category' =
CASE
WHEN price IS NULL THEN 'Not yet priced'
WHEN price < 10 THEN 'Very Reasonable Title'
WHEN price >= 10 and price < 20 THEN 'Coffee Table Title'
ELSE 'Expensive book!'
END,
CAST(title AS varchar(20)) AS 'Shortened Title'
FROM titles
ORDER BY price
GO
下面是结果集:
Price Category Shortened Title
--------------------- --------------------
Not yet priced Net Etiquette
Not yet priced The Psychology of Co
Very Reasonable Title The Gourmet Microwav
Very Reasonable Title You Can Combat Compu
Very Reasonable Title Life Without Fear
Very Reasonable Title Emotional Security:
Coffee Table Title Is Anger the Enemy?
Coffee Table Title Cooking with Compute
Coffee Table Title Fifty Years in Bucki
Coffee Table Title Sushi, Anyone?
Coffee Table Title Prolonged Data Depri
Coffee Table Title Silicon Valley Gastr
Coffee Table Title Straight Talk About
Coffee Table Title The Busy Executive's
Expensive book! Secrets of Silicon V
Expensive book! Onions, Leeks, and G
Expensive book! Computer Phobic And
Expensive book! But Is It User Frien
(18 row(s) affected)
C. 使用带有 SUBSTRING 和 SELECT 的 CASE 函数
下面的示例使用 CASE 和 THEN 生成一个有关作者、图书标识号和每个作者所著图书类型的列表。
USE pubs
SELECT SUBSTRING((RTRIM(a.au_fname) + ' '+
RTRIM(a.au_lname) + ' '), 1, 25) AS Name, a.au_id, ta.title_id,
Type =
CASE
WHEN SUBSTRING(ta.title_id, 1, 2) = 'BU' THEN 'Business'
WHEN SUBSTRING(ta.title_id, 1, 2) = 'MC' THEN 'Modern Cooking'
WHEN SUBSTRING(ta.title_id, 1, 2) = 'PC' THEN 'Popular Computing'
WHEN SUBSTRING(ta.title_id, 1, 2) = 'PS' THEN 'Psychology'
WHEN SUBSTRING(ta.title_id, 1, 2) = 'TC' THEN 'Traditional Cooking'
END
FROM titleauthor ta JOIN authors a ON ta.au_id = a.au_id
下面是结果集:
Name au_id title_id Type
------------------------- ----------- -------- -------------------
Johnson White 172-32-1176 PS3333 Psychology
Marjorie Green 213-46-8915 BU1032 Business
Marjorie Green 213-46-8915 BU2075 Business
Cheryl Carson 238-95-7766 PC1035 Popular Computing
Michael O'Leary 267-41-2394 BU1111 Business
Michael O'Leary 267-41-2394 TC7777 Traditional Cooking
Dean Straight 274-80-9391 BU7832 Business
Abraham Bennet 409-56-7008 BU1032 Business
Ann Dull 427-17-2319 PC8888 Popular Computing
Burt Gringlesby 472-27-2349 TC7777 Traditional Cooking
Charlene Locksley 486-29-1786 PC9999 Popular Computing
Charlene Locksley 486-29-1786 PS7777 Psychology
Reginald Blotchet-Halls 648-92-1872 TC4203 Traditional Cooking
Akiko Yokomoto 672-71-3249 TC7777 Traditional Cooking
Innes del Castillo 712-45-1867 MC2222 Modern Cooking
Michel DeFrance 722-51-5454 MC3021 Modern Cooking
Stearns MacFeather 724-80-9391 BU1111 Business
Stearns MacFeather 724-80-9391 PS1372 Psychology
Livia Karsen 756-30-7391 PS1372 Psychology
Sylvia Panteley 807-91-6654 TC3218 Traditional Cooking
Sheryl Hunter 846-92-7186 PC8888 Popular Computing
Anne Ringer 899-46-2035 MC3021 Modern Cooking
Anne Ringer 899-46-2035 PS2091 Psychology
Albert Ringer 998-72-3567 PS2091 Psychology
Albert Ringer 998-72-3567 PS2106 Psychology
(25 row(s) affected)
在 SELECT 语句中,CASE 搜索函数允许根据比较值在结果集内对值进行替换。下面的示例根据图书的价格范围将价格(money 列)显示为文本注释。
USE pubs
GO
SELECT 'Price Category' =
CASE
WHEN price IS NULL THEN 'Not yet priced'
WHEN price < 10 THEN 'Very Reasonable Title'
WHEN price >= 10 and price < 20 THEN 'Coffee Table Title'
ELSE 'Expensive book!'
END,
CAST(title AS varchar(20)) AS 'Shortened Title'
FROM titles
ORDER BY price
GO
下面是结果集:
Price Category Shortened Title
--------------------- --------------------
Not yet priced Net Etiquette
Not yet priced The Psychology of Co
Very Reasonable Title The Gourmet Microwav
Very Reasonable Title You Can Combat Compu
Very Reasonable Title Life Without Fear
Very Reasonable Title Emotional Security:
Coffee Table Title Is Anger the Enemy?
Coffee Table Title Cooking with Compute
Coffee Table Title Fifty Years in Bucki
Coffee Table Title Sushi, Anyone?
Coffee Table Title Prolonged Data Depri
Coffee Table Title Silicon Valley Gastr
Coffee Table Title Straight Talk About
Coffee Table Title The Busy Executive's
Expensive book! Secrets of Silicon V
Expensive book! Onions, Leeks, and G
Expensive book! Computer Phobic And
Expensive book! But Is It User Frien
(18 row(s) affected)
C. 使用带有 SUBSTRING 和 SELECT 的 CASE 函数
下面的示例使用 CASE 和 THEN 生成一个有关作者、图书标识号和每个作者所著图书类型的列表。
USE pubs
SELECT SUBSTRING((RTRIM(a.au_fname) + ' '+
RTRIM(a.au_lname) + ' '), 1, 25) AS Name, a.au_id, ta.title_id,
Type =
CASE
WHEN SUBSTRING(ta.title_id, 1, 2) = 'BU' THEN 'Business'
WHEN SUBSTRING(ta.title_id, 1, 2) = 'MC' THEN 'Modern Cooking'
WHEN SUBSTRING(ta.title_id, 1, 2) = 'PC' THEN 'Popular Computing'
WHEN SUBSTRING(ta.title_id, 1, 2) = 'PS' THEN 'Psychology'
WHEN SUBSTRING(ta.title_id, 1, 2) = 'TC' THEN 'Traditional Cooking'
END
FROM titleauthor ta JOIN authors a ON ta.au_id = a.au_id
下面是结果集:
Name au_id title_id Type
------------------------- ----------- -------- -------------------
Johnson White 172-32-1176 PS3333 Psychology
Marjorie Green 213-46-8915 BU1032 Business
Marjorie Green 213-46-8915 BU2075 Business
Cheryl Carson 238-95-7766 PC1035 Popular Computing
Michael O'Leary 267-41-2394 BU1111 Business
Michael O'Leary 267-41-2394 TC7777 Traditional Cooking
Dean Straight 274-80-9391 BU7832 Business
Abraham Bennet 409-56-7008 BU1032 Business
Ann Dull 427-17-2319 PC8888 Popular Computing
Burt Gringlesby 472-27-2349 TC7777 Traditional Cooking
Charlene Locksley 486-29-1786 PC9999 Popular Computing
Charlene Locksley 486-29-1786 PS7777 Psychology
Reginald Blotchet-Halls 648-92-1872 TC4203 Traditional Cooking
Akiko Yokomoto 672-71-3249 TC7777 Traditional Cooking
Innes del Castillo 712-45-1867 MC2222 Modern Cooking
Michel DeFrance 722-51-5454 MC3021 Modern Cooking
Stearns MacFeather 724-80-9391 BU1111 Business
Stearns MacFeather 724-80-9391 PS1372 Psychology
Livia Karsen 756-30-7391 PS1372 Psychology
Sylvia Panteley 807-91-6654 TC3218 Traditional Cooking
Sheryl Hunter 846-92-7186 PC8888 Popular Computing
Anne Ringer 899-46-2035 MC3021 Modern Cooking
Anne Ringer 899-46-2035 PS2091 Psychology
Albert Ringer 998-72-3567 PS2091 Psychology
Albert Ringer 998-72-3567 PS2106 Psychology
(25 row(s) affected)
#4
case 子段名 when 结果 then ... else 结果 then ... end
#5
那还有什么其它的方法呢,为了一个字段的集合,我是不是要构造一个临时表呢
#6
INSERT INTO #TEMP_QUERY(GAS_ID, PAY_TYPE, INV_NO, INV_NAME)
SELECT A.GAS_ID, A.PAY_TYPE, B.INV_NO, B.INV_NAME
FROM PAY_RECORD A
INNER JOIN #TEMP_GASGUNTOINV B ON (A.GAS_ID=B.GAS_ID AND A.GUN_NO=B.GUN_NO)
WHERE A.GAS_ID IN
( CASE WHEN (@DeptType=1) THEN (SELECT TOP 1 GAS_ID FROM GAS)
--~~~~~这样试试
WHEN (@DeptType=2) THEN (SELECT GAS_ID FROM GAS WHERE SUPER_DEPT_ID=@DeptNo) ELSE @DeptNo
END)
SELECT A.GAS_ID, A.PAY_TYPE, B.INV_NO, B.INV_NAME
FROM PAY_RECORD A
INNER JOIN #TEMP_GASGUNTOINV B ON (A.GAS_ID=B.GAS_ID AND A.GUN_NO=B.GUN_NO)
WHERE A.GAS_ID IN
( CASE WHEN (@DeptType=1) THEN (SELECT TOP 1 GAS_ID FROM GAS)
--~~~~~这样试试
WHEN (@DeptType=2) THEN (SELECT GAS_ID FROM GAS WHERE SUPER_DEPT_ID=@DeptNo) ELSE @DeptNo
END)
#7
INSERT INTO #TEMP_QUERY(GAS_ID, PAY_TYPE, INV_NO, INV_NAME)
SELECT A.GAS_ID, A.PAY_TYPE, B.INV_NO, B.INV_NAME
FROM PAY_RECORD A
INNER JOIN #TEMP_GASGUNTOINV B ON (A.GAS_ID=B.GAS_ID AND A.GUN_NO=B.GUN_NO)
WHERE A.GAS_ID IN
( CASE WHEN (@DeptType=1) THEN (SELECT TOP 1 GAS_ID FROM GAS)
--~~~~~这样试试
WHEN (@DeptType=2) THEN (SELECT GAS_ID FROM GAS WHERE SUPER_DEPT_ID=@DeptNo) ELSE @DeptNo
END)
SELECT A.GAS_ID, A.PAY_TYPE, B.INV_NO, B.INV_NAME
FROM PAY_RECORD A
INNER JOIN #TEMP_GASGUNTOINV B ON (A.GAS_ID=B.GAS_ID AND A.GUN_NO=B.GUN_NO)
WHERE A.GAS_ID IN
( CASE WHEN (@DeptType=1) THEN (SELECT TOP 1 GAS_ID FROM GAS)
--~~~~~这样试试
WHEN (@DeptType=2) THEN (SELECT GAS_ID FROM GAS WHERE SUPER_DEPT_ID=@DeptNo) ELSE @DeptNo
END)
#8
可以使用IF语句!
#9
因为 你的 SELECT GAS_ID FROM GAS
返回的不是一行,
而是多个记录行.所以无法实现...
#10
对于你的需要,
建议 转换一下思路,
应该动态地组合成一个 SQL 语句.
然后用 exec 来执行它..
建议 转换一下思路,
应该动态地组合成一个 SQL 语句.
然后用 exec 来执行它..
#11
INSERT INTO #TEMP_QUERY(GAS_ID, PAY_TYPE, INV_NO, INV_NAME)
SELECT A.GAS_ID, A.PAY_TYPE, B.INV_NO, B.INV_NAME
FROM PAY_RECORD A
INNER JOIN #TEMP_GASGUNTOINV B ON (A.GAS_ID=B.GAS_ID AND A.GUN_NO=B.GUN_NO)
WHERE ((@DeptType=1)and A.GAS_ID IN (SELECT GAS_ID FROM GAS) ) or ((@DeptType=2) and A.GAS_ID IN (SELECT GAS_ID FROM GAS WHERE SUPER_DEPT_ID=@DeptNo)or ((@DeptType<>2 and @DeptType<>1)and A.GAS_ID in @DeptNo)
SELECT A.GAS_ID, A.PAY_TYPE, B.INV_NO, B.INV_NAME
FROM PAY_RECORD A
INNER JOIN #TEMP_GASGUNTOINV B ON (A.GAS_ID=B.GAS_ID AND A.GUN_NO=B.GUN_NO)
WHERE ((@DeptType=1)and A.GAS_ID IN (SELECT GAS_ID FROM GAS) ) or ((@DeptType=2) and A.GAS_ID IN (SELECT GAS_ID FROM GAS WHERE SUPER_DEPT_ID=@DeptNo)or ((@DeptType<>2 and @DeptType<>1)and A.GAS_ID in @DeptNo)
#12
用if语句创建临时表
if (@DeptType=1)
SELECT GAS_ID into #cc FROM GAS
else if (@DeptType=2)
SELECT GAS_ID FROM GAS into #cc WHERE SUPER_DEPT_ID=@DeptNo
else
select @DeptNo as GAS_ID into #cc
INSERT INTO #TEMP_QUERY(GAS_ID, PAY_TYPE, INV_NO, INV_NAME)
SELECT A.GAS_ID, A.PAY_TYPE, B.INV_NO, B.INV_NAME
FROM PAY_RECORD A
INNER JOIN #TEMP_GASGUNTOINV B ON (A.GAS_ID=B.GAS_ID AND A.GUN_NO=B.GUN_NO)
WHERE A.GAS_ID IN
( select GAS_ID from #cc)
if (@DeptType=1)
SELECT GAS_ID into #cc FROM GAS
else if (@DeptType=2)
SELECT GAS_ID FROM GAS into #cc WHERE SUPER_DEPT_ID=@DeptNo
else
select @DeptNo as GAS_ID into #cc
INSERT INTO #TEMP_QUERY(GAS_ID, PAY_TYPE, INV_NO, INV_NAME)
SELECT A.GAS_ID, A.PAY_TYPE, B.INV_NO, B.INV_NAME
FROM PAY_RECORD A
INNER JOIN #TEMP_GASGUNTOINV B ON (A.GAS_ID=B.GAS_ID AND A.GUN_NO=B.GUN_NO)
WHERE A.GAS_ID IN
( select GAS_ID from #cc)
#13
CASE WHEN 返回的是一个值,所以做以下修改:
WHERE A.GAS_ID IN (SELECT GAS_ID
FROM GAS
WHERE SUPER_DEPT_ID=(
CASE WHEN (@DeptType=1) THEN SUPER_DEPT_ID
WHEN (@DeptType=2) THEN @DeptNo EBD
)
UNION ALL
SELECT @DeptNo WHERE @DeptType NOT IN (1,2)
)
WHERE A.GAS_ID IN (SELECT GAS_ID
FROM GAS
WHERE SUPER_DEPT_ID=(
CASE WHEN (@DeptType=1) THEN SUPER_DEPT_ID
WHEN (@DeptType=2) THEN @DeptNo EBD
)
UNION ALL
SELECT @DeptNo WHERE @DeptType NOT IN (1,2)
)
#1
在then 后不能使用结果集合,参考一下:
CASE
计算条件列表并返回多个可能结果表达式之一。
CASE 具有两种格式:
简单 CASE 函数将某个表达式与一组简单表达式进行比较以确定结果。
CASE 搜索函数计算一组布尔表达式以确定结果。
两种格式都支持可选的 ELSE 参数。
语法
简单 CASE 函数:
CASE input_expression
WHEN when_expression THEN result_expression
[ ...n ]
[
ELSE else_result_expression
END
CASE 搜索函数:
CASE
WHEN Boolean_expression THEN result_expression
[ ...n ]
[
ELSE else_result_expression
END
参数
input_expression
是使用简单 CASE 格式时所计算的表达式。Input_expression 是任何有效的 Microsoft® SQL Server™ 表达式。
WHEN when_expression
使用简单 CASE 格式时 input_expression 所比较的简单表达式。When_expression 是任意有效的 SQL Server 表达式。Input_expression 和每个 when_expression 的数据类型必须相同,或者是隐性转换。
n
占位符,表明可以使用多个 WHEN when_expression THEN result_expression 子句或 WHEN Boolean_expression THEN result_expression 子句。
THEN result_expression
当 input_expression = when_expression 取值为 TRUE,或者 Boolean_expression 取值为 TRUE 时返回的表达式。result expression 是任意有效的 SQL Server 表达式。
ELSE else_result_expression
当比较运算取值不为 TRUE 时返回的表达式。如果省略此参数并且比较运算取值不为 TRUE,CASE 将返回 NULL 值。Else_result_expression 是任意有效的 SQL Server 表达式。Else_result_expression 和所有 result_expression 的数据类型必须相同,或者必须是隐性转换。
WHEN Boolean_expression
使用 CASE 搜索格式时所计算的布尔表达式。Boolean_expression 是任意有效的布尔表达式。
结果类型
从 result_expressions 和可选 else_result_expression 的类型集合中返回最高的优先规则类型。
CASE
计算条件列表并返回多个可能结果表达式之一。
CASE 具有两种格式:
简单 CASE 函数将某个表达式与一组简单表达式进行比较以确定结果。
CASE 搜索函数计算一组布尔表达式以确定结果。
两种格式都支持可选的 ELSE 参数。
语法
简单 CASE 函数:
CASE input_expression
WHEN when_expression THEN result_expression
[ ...n ]
[
ELSE else_result_expression
END
CASE 搜索函数:
CASE
WHEN Boolean_expression THEN result_expression
[ ...n ]
[
ELSE else_result_expression
END
参数
input_expression
是使用简单 CASE 格式时所计算的表达式。Input_expression 是任何有效的 Microsoft® SQL Server™ 表达式。
WHEN when_expression
使用简单 CASE 格式时 input_expression 所比较的简单表达式。When_expression 是任意有效的 SQL Server 表达式。Input_expression 和每个 when_expression 的数据类型必须相同,或者是隐性转换。
n
占位符,表明可以使用多个 WHEN when_expression THEN result_expression 子句或 WHEN Boolean_expression THEN result_expression 子句。
THEN result_expression
当 input_expression = when_expression 取值为 TRUE,或者 Boolean_expression 取值为 TRUE 时返回的表达式。result expression 是任意有效的 SQL Server 表达式。
ELSE else_result_expression
当比较运算取值不为 TRUE 时返回的表达式。如果省略此参数并且比较运算取值不为 TRUE,CASE 将返回 NULL 值。Else_result_expression 是任意有效的 SQL Server 表达式。Else_result_expression 和所有 result_expression 的数据类型必须相同,或者必须是隐性转换。
WHEN Boolean_expression
使用 CASE 搜索格式时所计算的布尔表达式。Boolean_expression 是任意有效的布尔表达式。
结果类型
从 result_expressions 和可选 else_result_expression 的类型集合中返回最高的优先规则类型。
#2
单 CASE 函数:
计算 input_expression,然后按指定顺序对每个 WHEN 子句的 input_expression = when_expression 进行计算。
返回第一个取值为 TRUE 的 (input_expression = when_expression) 的 result_expression。
如果没有取值为 TRUE 的 input_expression = when_expression,则当指定 ELSE 子句时 SQL Server 将返回 else_result_expression;若没有指定 ELSE 子句,则返回 NULL 值。
CASE 搜索函数:
按指定顺序为每个 WHEN 子句的 Boolean_expression 求值。
返回第一个取值为 TRUE 的 Boolean_expression 的 result_expression。
如果没有取值为 TRUE 的 Boolean_expression,则当指定 ELSE 子句时 SQL Server 将返回 else_result_expression;若没有指定 ELSE 子句,则返回 NULL 值。
示例
A. 使用带有简单 CASE 函数的 SELECT 语句
在 SELECT 语句中,简单 CASE 函数仅检查是否相等,而不进行其它比较。下面的示例使用 CASE 函数更改图书分类显示,以使其更易于理解。
USE pubs
GO
SELECT Category =
CASE type
WHEN 'popular_comp' THEN 'Popular Computing'
WHEN 'mod_cook' THEN 'Modern Cooking'
WHEN 'business' THEN 'Business'
WHEN 'psychology' THEN 'Psychology'
WHEN 'trad_cook' THEN 'Traditional Cooking'
ELSE 'Not yet categorized'
END,
CAST(title AS varchar(25)) AS 'Shortened Title',
price AS Price
FROM titles
WHERE price IS NOT NULL
ORDER BY type, price
COMPUTE AVG(price) BY type
GO
下面是结果集:
Category Shortened Title Price
------------------- ------------------------- --------------------------
Business You Can Combat Computer S 2.99
Business Cooking with Computers: S 11.95
Business The Busy Executive's Data 19.99
Business Straight Talk About Compu 19.99
avg
==========================
13.73
Category Shortened Title Price
------------------- ------------------------- --------------------------
Modern Cooking The Gourmet Microwave 2.99
Modern Cooking Silicon Valley Gastronomi 19.99
avg
==========================
11.49
Category Shortened Title Price
------------------- ------------------------- --------------------------
Popular Computing Secrets of Silicon Valley 20.00
Popular Computing But Is It User Friendly? 22.95
avg
==========================
21.48
Category Shortened Title Price
------------------- ------------------------- --------------------------
Psychology Life Without Fear 7.00
Psychology Emotional Security: A New 7.99
Psychology Is Anger the Enemy? 10.95
Psychology Prolonged Data Deprivatio 19.99
Psychology Computer Phobic AND Non-P 21.59
avg
==========================
13.50
Category Shortened Title Price
------------------- ------------------------- --------------------------
Traditional Cooking Fifty Years in Buckingham 11.95
Traditional Cooking Sushi, Anyone? 14.99
Traditional Cooking Onions, Leeks, and Garlic 20.95
avg
==========================
15.96
(21 row(s) affected)
计算 input_expression,然后按指定顺序对每个 WHEN 子句的 input_expression = when_expression 进行计算。
返回第一个取值为 TRUE 的 (input_expression = when_expression) 的 result_expression。
如果没有取值为 TRUE 的 input_expression = when_expression,则当指定 ELSE 子句时 SQL Server 将返回 else_result_expression;若没有指定 ELSE 子句,则返回 NULL 值。
CASE 搜索函数:
按指定顺序为每个 WHEN 子句的 Boolean_expression 求值。
返回第一个取值为 TRUE 的 Boolean_expression 的 result_expression。
如果没有取值为 TRUE 的 Boolean_expression,则当指定 ELSE 子句时 SQL Server 将返回 else_result_expression;若没有指定 ELSE 子句,则返回 NULL 值。
示例
A. 使用带有简单 CASE 函数的 SELECT 语句
在 SELECT 语句中,简单 CASE 函数仅检查是否相等,而不进行其它比较。下面的示例使用 CASE 函数更改图书分类显示,以使其更易于理解。
USE pubs
GO
SELECT Category =
CASE type
WHEN 'popular_comp' THEN 'Popular Computing'
WHEN 'mod_cook' THEN 'Modern Cooking'
WHEN 'business' THEN 'Business'
WHEN 'psychology' THEN 'Psychology'
WHEN 'trad_cook' THEN 'Traditional Cooking'
ELSE 'Not yet categorized'
END,
CAST(title AS varchar(25)) AS 'Shortened Title',
price AS Price
FROM titles
WHERE price IS NOT NULL
ORDER BY type, price
COMPUTE AVG(price) BY type
GO
下面是结果集:
Category Shortened Title Price
------------------- ------------------------- --------------------------
Business You Can Combat Computer S 2.99
Business Cooking with Computers: S 11.95
Business The Busy Executive's Data 19.99
Business Straight Talk About Compu 19.99
avg
==========================
13.73
Category Shortened Title Price
------------------- ------------------------- --------------------------
Modern Cooking The Gourmet Microwave 2.99
Modern Cooking Silicon Valley Gastronomi 19.99
avg
==========================
11.49
Category Shortened Title Price
------------------- ------------------------- --------------------------
Popular Computing Secrets of Silicon Valley 20.00
Popular Computing But Is It User Friendly? 22.95
avg
==========================
21.48
Category Shortened Title Price
------------------- ------------------------- --------------------------
Psychology Life Without Fear 7.00
Psychology Emotional Security: A New 7.99
Psychology Is Anger the Enemy? 10.95
Psychology Prolonged Data Deprivatio 19.99
Psychology Computer Phobic AND Non-P 21.59
avg
==========================
13.50
Category Shortened Title Price
------------------- ------------------------- --------------------------
Traditional Cooking Fifty Years in Buckingham 11.95
Traditional Cooking Sushi, Anyone? 14.99
Traditional Cooking Onions, Leeks, and Garlic 20.95
avg
==========================
15.96
(21 row(s) affected)
#3
B. 使用带有简单 CASE 函数和 CASE 搜索函数的 SELECT 语句
在 SELECT 语句中,CASE 搜索函数允许根据比较值在结果集内对值进行替换。下面的示例根据图书的价格范围将价格(money 列)显示为文本注释。
USE pubs
GO
SELECT 'Price Category' =
CASE
WHEN price IS NULL THEN 'Not yet priced'
WHEN price < 10 THEN 'Very Reasonable Title'
WHEN price >= 10 and price < 20 THEN 'Coffee Table Title'
ELSE 'Expensive book!'
END,
CAST(title AS varchar(20)) AS 'Shortened Title'
FROM titles
ORDER BY price
GO
下面是结果集:
Price Category Shortened Title
--------------------- --------------------
Not yet priced Net Etiquette
Not yet priced The Psychology of Co
Very Reasonable Title The Gourmet Microwav
Very Reasonable Title You Can Combat Compu
Very Reasonable Title Life Without Fear
Very Reasonable Title Emotional Security:
Coffee Table Title Is Anger the Enemy?
Coffee Table Title Cooking with Compute
Coffee Table Title Fifty Years in Bucki
Coffee Table Title Sushi, Anyone?
Coffee Table Title Prolonged Data Depri
Coffee Table Title Silicon Valley Gastr
Coffee Table Title Straight Talk About
Coffee Table Title The Busy Executive's
Expensive book! Secrets of Silicon V
Expensive book! Onions, Leeks, and G
Expensive book! Computer Phobic And
Expensive book! But Is It User Frien
(18 row(s) affected)
C. 使用带有 SUBSTRING 和 SELECT 的 CASE 函数
下面的示例使用 CASE 和 THEN 生成一个有关作者、图书标识号和每个作者所著图书类型的列表。
USE pubs
SELECT SUBSTRING((RTRIM(a.au_fname) + ' '+
RTRIM(a.au_lname) + ' '), 1, 25) AS Name, a.au_id, ta.title_id,
Type =
CASE
WHEN SUBSTRING(ta.title_id, 1, 2) = 'BU' THEN 'Business'
WHEN SUBSTRING(ta.title_id, 1, 2) = 'MC' THEN 'Modern Cooking'
WHEN SUBSTRING(ta.title_id, 1, 2) = 'PC' THEN 'Popular Computing'
WHEN SUBSTRING(ta.title_id, 1, 2) = 'PS' THEN 'Psychology'
WHEN SUBSTRING(ta.title_id, 1, 2) = 'TC' THEN 'Traditional Cooking'
END
FROM titleauthor ta JOIN authors a ON ta.au_id = a.au_id
下面是结果集:
Name au_id title_id Type
------------------------- ----------- -------- -------------------
Johnson White 172-32-1176 PS3333 Psychology
Marjorie Green 213-46-8915 BU1032 Business
Marjorie Green 213-46-8915 BU2075 Business
Cheryl Carson 238-95-7766 PC1035 Popular Computing
Michael O'Leary 267-41-2394 BU1111 Business
Michael O'Leary 267-41-2394 TC7777 Traditional Cooking
Dean Straight 274-80-9391 BU7832 Business
Abraham Bennet 409-56-7008 BU1032 Business
Ann Dull 427-17-2319 PC8888 Popular Computing
Burt Gringlesby 472-27-2349 TC7777 Traditional Cooking
Charlene Locksley 486-29-1786 PC9999 Popular Computing
Charlene Locksley 486-29-1786 PS7777 Psychology
Reginald Blotchet-Halls 648-92-1872 TC4203 Traditional Cooking
Akiko Yokomoto 672-71-3249 TC7777 Traditional Cooking
Innes del Castillo 712-45-1867 MC2222 Modern Cooking
Michel DeFrance 722-51-5454 MC3021 Modern Cooking
Stearns MacFeather 724-80-9391 BU1111 Business
Stearns MacFeather 724-80-9391 PS1372 Psychology
Livia Karsen 756-30-7391 PS1372 Psychology
Sylvia Panteley 807-91-6654 TC3218 Traditional Cooking
Sheryl Hunter 846-92-7186 PC8888 Popular Computing
Anne Ringer 899-46-2035 MC3021 Modern Cooking
Anne Ringer 899-46-2035 PS2091 Psychology
Albert Ringer 998-72-3567 PS2091 Psychology
Albert Ringer 998-72-3567 PS2106 Psychology
(25 row(s) affected)
在 SELECT 语句中,CASE 搜索函数允许根据比较值在结果集内对值进行替换。下面的示例根据图书的价格范围将价格(money 列)显示为文本注释。
USE pubs
GO
SELECT 'Price Category' =
CASE
WHEN price IS NULL THEN 'Not yet priced'
WHEN price < 10 THEN 'Very Reasonable Title'
WHEN price >= 10 and price < 20 THEN 'Coffee Table Title'
ELSE 'Expensive book!'
END,
CAST(title AS varchar(20)) AS 'Shortened Title'
FROM titles
ORDER BY price
GO
下面是结果集:
Price Category Shortened Title
--------------------- --------------------
Not yet priced Net Etiquette
Not yet priced The Psychology of Co
Very Reasonable Title The Gourmet Microwav
Very Reasonable Title You Can Combat Compu
Very Reasonable Title Life Without Fear
Very Reasonable Title Emotional Security:
Coffee Table Title Is Anger the Enemy?
Coffee Table Title Cooking with Compute
Coffee Table Title Fifty Years in Bucki
Coffee Table Title Sushi, Anyone?
Coffee Table Title Prolonged Data Depri
Coffee Table Title Silicon Valley Gastr
Coffee Table Title Straight Talk About
Coffee Table Title The Busy Executive's
Expensive book! Secrets of Silicon V
Expensive book! Onions, Leeks, and G
Expensive book! Computer Phobic And
Expensive book! But Is It User Frien
(18 row(s) affected)
C. 使用带有 SUBSTRING 和 SELECT 的 CASE 函数
下面的示例使用 CASE 和 THEN 生成一个有关作者、图书标识号和每个作者所著图书类型的列表。
USE pubs
SELECT SUBSTRING((RTRIM(a.au_fname) + ' '+
RTRIM(a.au_lname) + ' '), 1, 25) AS Name, a.au_id, ta.title_id,
Type =
CASE
WHEN SUBSTRING(ta.title_id, 1, 2) = 'BU' THEN 'Business'
WHEN SUBSTRING(ta.title_id, 1, 2) = 'MC' THEN 'Modern Cooking'
WHEN SUBSTRING(ta.title_id, 1, 2) = 'PC' THEN 'Popular Computing'
WHEN SUBSTRING(ta.title_id, 1, 2) = 'PS' THEN 'Psychology'
WHEN SUBSTRING(ta.title_id, 1, 2) = 'TC' THEN 'Traditional Cooking'
END
FROM titleauthor ta JOIN authors a ON ta.au_id = a.au_id
下面是结果集:
Name au_id title_id Type
------------------------- ----------- -------- -------------------
Johnson White 172-32-1176 PS3333 Psychology
Marjorie Green 213-46-8915 BU1032 Business
Marjorie Green 213-46-8915 BU2075 Business
Cheryl Carson 238-95-7766 PC1035 Popular Computing
Michael O'Leary 267-41-2394 BU1111 Business
Michael O'Leary 267-41-2394 TC7777 Traditional Cooking
Dean Straight 274-80-9391 BU7832 Business
Abraham Bennet 409-56-7008 BU1032 Business
Ann Dull 427-17-2319 PC8888 Popular Computing
Burt Gringlesby 472-27-2349 TC7777 Traditional Cooking
Charlene Locksley 486-29-1786 PC9999 Popular Computing
Charlene Locksley 486-29-1786 PS7777 Psychology
Reginald Blotchet-Halls 648-92-1872 TC4203 Traditional Cooking
Akiko Yokomoto 672-71-3249 TC7777 Traditional Cooking
Innes del Castillo 712-45-1867 MC2222 Modern Cooking
Michel DeFrance 722-51-5454 MC3021 Modern Cooking
Stearns MacFeather 724-80-9391 BU1111 Business
Stearns MacFeather 724-80-9391 PS1372 Psychology
Livia Karsen 756-30-7391 PS1372 Psychology
Sylvia Panteley 807-91-6654 TC3218 Traditional Cooking
Sheryl Hunter 846-92-7186 PC8888 Popular Computing
Anne Ringer 899-46-2035 MC3021 Modern Cooking
Anne Ringer 899-46-2035 PS2091 Psychology
Albert Ringer 998-72-3567 PS2091 Psychology
Albert Ringer 998-72-3567 PS2106 Psychology
(25 row(s) affected)
#4
case 子段名 when 结果 then ... else 结果 then ... end
#5
那还有什么其它的方法呢,为了一个字段的集合,我是不是要构造一个临时表呢
#6
INSERT INTO #TEMP_QUERY(GAS_ID, PAY_TYPE, INV_NO, INV_NAME)
SELECT A.GAS_ID, A.PAY_TYPE, B.INV_NO, B.INV_NAME
FROM PAY_RECORD A
INNER JOIN #TEMP_GASGUNTOINV B ON (A.GAS_ID=B.GAS_ID AND A.GUN_NO=B.GUN_NO)
WHERE A.GAS_ID IN
( CASE WHEN (@DeptType=1) THEN (SELECT TOP 1 GAS_ID FROM GAS)
--~~~~~这样试试
WHEN (@DeptType=2) THEN (SELECT GAS_ID FROM GAS WHERE SUPER_DEPT_ID=@DeptNo) ELSE @DeptNo
END)
SELECT A.GAS_ID, A.PAY_TYPE, B.INV_NO, B.INV_NAME
FROM PAY_RECORD A
INNER JOIN #TEMP_GASGUNTOINV B ON (A.GAS_ID=B.GAS_ID AND A.GUN_NO=B.GUN_NO)
WHERE A.GAS_ID IN
( CASE WHEN (@DeptType=1) THEN (SELECT TOP 1 GAS_ID FROM GAS)
--~~~~~这样试试
WHEN (@DeptType=2) THEN (SELECT GAS_ID FROM GAS WHERE SUPER_DEPT_ID=@DeptNo) ELSE @DeptNo
END)
#7
INSERT INTO #TEMP_QUERY(GAS_ID, PAY_TYPE, INV_NO, INV_NAME)
SELECT A.GAS_ID, A.PAY_TYPE, B.INV_NO, B.INV_NAME
FROM PAY_RECORD A
INNER JOIN #TEMP_GASGUNTOINV B ON (A.GAS_ID=B.GAS_ID AND A.GUN_NO=B.GUN_NO)
WHERE A.GAS_ID IN
( CASE WHEN (@DeptType=1) THEN (SELECT TOP 1 GAS_ID FROM GAS)
--~~~~~这样试试
WHEN (@DeptType=2) THEN (SELECT GAS_ID FROM GAS WHERE SUPER_DEPT_ID=@DeptNo) ELSE @DeptNo
END)
SELECT A.GAS_ID, A.PAY_TYPE, B.INV_NO, B.INV_NAME
FROM PAY_RECORD A
INNER JOIN #TEMP_GASGUNTOINV B ON (A.GAS_ID=B.GAS_ID AND A.GUN_NO=B.GUN_NO)
WHERE A.GAS_ID IN
( CASE WHEN (@DeptType=1) THEN (SELECT TOP 1 GAS_ID FROM GAS)
--~~~~~这样试试
WHEN (@DeptType=2) THEN (SELECT GAS_ID FROM GAS WHERE SUPER_DEPT_ID=@DeptNo) ELSE @DeptNo
END)
#8
可以使用IF语句!
#9
因为 你的 SELECT GAS_ID FROM GAS
返回的不是一行,
而是多个记录行.所以无法实现...
#10
对于你的需要,
建议 转换一下思路,
应该动态地组合成一个 SQL 语句.
然后用 exec 来执行它..
建议 转换一下思路,
应该动态地组合成一个 SQL 语句.
然后用 exec 来执行它..
#11
INSERT INTO #TEMP_QUERY(GAS_ID, PAY_TYPE, INV_NO, INV_NAME)
SELECT A.GAS_ID, A.PAY_TYPE, B.INV_NO, B.INV_NAME
FROM PAY_RECORD A
INNER JOIN #TEMP_GASGUNTOINV B ON (A.GAS_ID=B.GAS_ID AND A.GUN_NO=B.GUN_NO)
WHERE ((@DeptType=1)and A.GAS_ID IN (SELECT GAS_ID FROM GAS) ) or ((@DeptType=2) and A.GAS_ID IN (SELECT GAS_ID FROM GAS WHERE SUPER_DEPT_ID=@DeptNo)or ((@DeptType<>2 and @DeptType<>1)and A.GAS_ID in @DeptNo)
SELECT A.GAS_ID, A.PAY_TYPE, B.INV_NO, B.INV_NAME
FROM PAY_RECORD A
INNER JOIN #TEMP_GASGUNTOINV B ON (A.GAS_ID=B.GAS_ID AND A.GUN_NO=B.GUN_NO)
WHERE ((@DeptType=1)and A.GAS_ID IN (SELECT GAS_ID FROM GAS) ) or ((@DeptType=2) and A.GAS_ID IN (SELECT GAS_ID FROM GAS WHERE SUPER_DEPT_ID=@DeptNo)or ((@DeptType<>2 and @DeptType<>1)and A.GAS_ID in @DeptNo)
#12
用if语句创建临时表
if (@DeptType=1)
SELECT GAS_ID into #cc FROM GAS
else if (@DeptType=2)
SELECT GAS_ID FROM GAS into #cc WHERE SUPER_DEPT_ID=@DeptNo
else
select @DeptNo as GAS_ID into #cc
INSERT INTO #TEMP_QUERY(GAS_ID, PAY_TYPE, INV_NO, INV_NAME)
SELECT A.GAS_ID, A.PAY_TYPE, B.INV_NO, B.INV_NAME
FROM PAY_RECORD A
INNER JOIN #TEMP_GASGUNTOINV B ON (A.GAS_ID=B.GAS_ID AND A.GUN_NO=B.GUN_NO)
WHERE A.GAS_ID IN
( select GAS_ID from #cc)
if (@DeptType=1)
SELECT GAS_ID into #cc FROM GAS
else if (@DeptType=2)
SELECT GAS_ID FROM GAS into #cc WHERE SUPER_DEPT_ID=@DeptNo
else
select @DeptNo as GAS_ID into #cc
INSERT INTO #TEMP_QUERY(GAS_ID, PAY_TYPE, INV_NO, INV_NAME)
SELECT A.GAS_ID, A.PAY_TYPE, B.INV_NO, B.INV_NAME
FROM PAY_RECORD A
INNER JOIN #TEMP_GASGUNTOINV B ON (A.GAS_ID=B.GAS_ID AND A.GUN_NO=B.GUN_NO)
WHERE A.GAS_ID IN
( select GAS_ID from #cc)
#13
CASE WHEN 返回的是一个值,所以做以下修改:
WHERE A.GAS_ID IN (SELECT GAS_ID
FROM GAS
WHERE SUPER_DEPT_ID=(
CASE WHEN (@DeptType=1) THEN SUPER_DEPT_ID
WHEN (@DeptType=2) THEN @DeptNo EBD
)
UNION ALL
SELECT @DeptNo WHERE @DeptType NOT IN (1,2)
)
WHERE A.GAS_ID IN (SELECT GAS_ID
FROM GAS
WHERE SUPER_DEPT_ID=(
CASE WHEN (@DeptType=1) THEN SUPER_DEPT_ID
WHEN (@DeptType=2) THEN @DeptNo EBD
)
UNION ALL
SELECT @DeptNo WHERE @DeptType NOT IN (1,2)
)