[英]SQL Server: Print Generated SQL Query
有谁知道在 SQL 服务器中执行 SQL 查询后如何打印 output 查询?
select count(eo.equal_opps_id) as 'Age 40 - 49'
from dbo.app_equal_opps eo, dbo.app_status s
where eo.date_of_birth > DateAdd(yy,-49,getDate())
and eo.date_of_birth < DateAdd(yy,-39,getDate())
and eo.application_id = s.status_id
and s.job_reference_number = '33211016'
and s.submitted = 1
上面显示了一个 SQL 查询,但是,一旦它被执行,我想看看在以下部分创建的实际日期值
DateAdd(yy,-49,getDate())
DateAdd(yy,-39,getDate())
换句话说,一旦执行查询,我可以打印 SQL 查询 output 以便它显示类似这样的内容
select count(eo.equal_opps_id) as 'Age 40 - 49'
from dbo.app_equal_opps eo, dbo.app_status s
where eo.date_of_birth > '1962-05-04 13:00:00.000'
and eo.date_of_birth < '1972-05-04 13:00:00.000'
and eo.application_id = s.status_id
and s.job_reference_number = '33211016'
and s.submitted = 1
与往常一样,我们将不胜感激任何反馈。
感谢大家。
将这两个日期添加到您的 select 列表中:
select
count(eo.equal_opps_id) as 'Age 40 - 49'
,DateAdd(yy,-49,getDate()) as LesserDate
,DateAdd(yy,-39,getDate()) as GreaterDate
from
dbo.app_equal_opps eo, dbo.app_status s
where
eo.date_of_birth > DateAdd(yy,-49,getDate())
and eo.date_of_birth < DateAdd(yy,-39,getDate())
and eo.application_id = s.status_id
and s.job_reference_number = '33211016'
and s.submitted = 1
我能想到的唯一方法是混乱。 您可以将 sql 构建为字符串,然后使用内置 function 中的 exec 执行它。
declare @sql as varchar(max)
select @sql = '
select count(eo.equal_opps_id) as ''Age 40 - 49''
from dbo.app_equal_opps eo, dbo.app_status s
where eo.date_of_birth > ' + ( select cast(DateAdd(yy,-49,getDate()) as varchar(100)) ) + '
and eo.date_of_birth < ' + ( select cast(DateAdd(yy,-39,getDate()) as varchar(100)) ) + '
and eo.application_id = s.status_id
and s.job_reference_number = ''33211016''
and s.submitted = 1'
print @sql
exec(@sql)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.