SQL Server:打印生成的SQL查询
有人知道在SQL Server中执行SQL查询后如何打印输出吗SQL Server:打印生成的SQL查询,sql,sql-server,Sql,Sql Server,有人知道在SQL Server中执行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 > DateAdd(yy,-49,getDate()) and eo.date_of_birth < DateAdd(yy,-39,getDate()) and eo.application_
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查询输出以便它显示如下内容吗
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
选择计数(eo.equal_opps_id)作为“40-49岁”
来自dbo.app_equal_opps eo,dbo.app_状态
其中eo.date_of_出生>'1962-05-04 13:00:00.000'
和出生日期<'1972-05-04 13:00:00.000'
和eo.application_id=s.status_id
和s.job_reference_number='33211016'
和s.submitted=1
如往常一样,任何反馈都将不胜感激
谢谢大家。将这两个日期添加到您的选择列表中:
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
选择
算作“40-49岁”
,DateAdd(yy,-49,getDate())作为LesserDate
,DateAdd(yy,-39,getDate())作为更大的日期
从…起
dbo.app_equal_opps eo,dbo.app_状态
哪里
eo.date出生日期>日期添加(yy,-49,getDate())
和eo.date出生日期
我唯一能想到的就是乱七八糟。将sql构建为字符串,然后使用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)
将@sql声明为varchar(max)
选择@sql='1!'
选择计数(eo.equal_opps_id)作为“40-49岁”
来自dbo.app_equal_opps eo,dbo.app_状态
其中eo.date_of_birth>'+(选择cast(DateAdd(yy,-49,getDate())作为varchar(100))+'
和eo.date_of_birth<'+(选择cast(DateAdd(yy,-39,getDate())作为varchar(100))+'
和eo.application_id=s.status_id
美国工作参考号为“33211016”
和s.submitted=1'
打印@sql
exec(@sql)
它不会以文本形式展开这些函数调用-您显示的“生成”表单将永远不存在。
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)