[英]if exists in sql within a subquery
I am trying to execute the following query to check the records from the 4 tables and then calling a function,but i get an error near the brackets.It works fine if I use case,but exits when the first condition is met. 我试图执行以下查询以检查4个表中的记录,然后调用一个函数,但是在方括号附近出现错误。如果使用case,它可以正常工作,但在满足第一个条件时退出。 I need to evaluate for all the tables in the 4 IF's:
我需要评估4个IF中的所有表:
select account_id,
(
if exists (select account_id from [dbo].[TEST_R6])
Begin
select dbo.make_indicator(cent,chem,co,dim,lg,pl,strs,vis) + space(1) + 'rr'
End
if exists (select account_id from tbl_noR6)
begin
select dbo.make_indicator(acent,chem,co,dim,lg,pl,str,vis) + space(2) + 'cc'
end
if exists (select account_id from tbl_acct)
begin
select dbo.make_indicator(cent,chem,co,dim,lg,pl,str,vis) + space(3) + 'pp'
end
if exists (select account_id from test_con)
begin
select dbo.make_indicator(cent,chem,co,dim,lg,pl,str,vis) + space(4) + 'no'
end
)as value from CRS_PRODLINE
code works partially with case statement and gives output only for the first case is met and does not check for the others: 代码部分地与case语句一起工作,并且仅在满足第一种情况时才提供输出,而不检查其他情况:
DECLARE @cols AS NVARCHAR(MAX),
@query AS NVARCHAR(MAX);
SET @cols = STUFF((SELECT distinct ',' + QUOTENAME(c.A_NAME)
FROM TEST_DEL c
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
SET @query = 'SELECT account_id, ' + @cols + ' from
(select account_id,
(case
when exists (select account_id from [dbo].[TEST_R6])
then
dbo.make_indicator(acent,chem,co,dim,lg,pl,str,vis) + space(1)
when exists (select account_id from tbl_noR6)
then
dbo.make_indicator(acent,chem,co,dim,lg,pl,str,vis) + space(2)
when exists (select account_id from tbl_acct)
then
dbo.make_indicator(acent,chem,co,dim,lg,pl,str,vis) + space(2)
end) as value,
assay_name
from CRS_PRODLINE
) x
pivot
(
MAX(newvalue)
for a_name in (' + @cols + ')
) p '
execute(@query)
First of all, if there is any record in the table your exists clause will return true. 首先,如果表中有任何记录,则exist子句将返回true。 I will assume that this is intentional and you are not trying to match on a specific account_id.
我将假定这是有意的,您没有尝试在特定的account_id上进行匹配。
If you want to use this structure you will have to use dynamic SQL. 如果要使用此结构,则必须使用动态SQL。 Something like this:
像这样:
DECLARE @Sql VARCHAR(8000)
IF EXISTS (SELECT account_id FROM tbl_noR6)
BEGIN
SET @Sql = 'select dbo.make_indicator(acent,chem,co,dim,lg,pl,str,vis) + space(2) + ''cc'''
SET @Sql = @Sql + ')as value from CRS_PRODLINE'
EXEC @Sql
END
IF EXISTS (SELECT account_id FROM tbl_acct)
BEGIN
SET @Sql = 'select dbo.make_indicator(cent,chem,co,dim,lg,pl,str,vis) + space(3) + ''pp'''
SET @Sql = @Sql + ')as value from CRS_PRODLINE'
EXEC @Sql
END
IF EXISTS (SELECT account_id FROM test_con)
BEGIN
SET @Sql = 'select dbo.make_indicator(cent,chem,co,dim,lg,pl,str,vis) + space(4) + ''no'''
SET @Sql = @Sql + ')as value from CRS_PRODLINE'
EXEC @Sql
END
Based on your comment that CASE only evaluates once being a problem, this should build a string of all the applicable cases. 根据您的评论,即CASE仅在出现问题时才进行评估,因此应该构建所有适用案例的字符串。
select account_id,
(
''
+
case when exists (select account_id from dbo.[TEST_R6]) THEN select dbo.make_indicator(cent,chem,co,dim,lg,pl,strs,vis) + space(1) + 'rr' ELSE '' END
+
case when exists (select account_id from dbo.tbl_noR6) THEN select dbo.make_indicator(acent,chem,co,dim,lg,pl,str,vis) + space(2) + 'cc' ELSE '' END
+
case when exists (select account_id from dbo.tbl_acct) THEN select dbo.make_indicator(cent,chem,co,dim,lg,pl,str,vis) + space(3) + 'pp' ELSE '' END
+
case when exists (select account_id from dbo.test_con) THEN select dbo.make_indicator(cent,chem,co,dim,lg,pl,str,vis) + space(4) + 'no' ELSE '' END
) as value
from CRS_PRODLINE
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.