开发者

SQL Server: Print Generated SQL Query

Does anyone know how to print the output of an SQL Query once it has been executed in SQL Server?

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    

Abov开发者_JS百科e shows an SQL query, however, once it is executed I would like to see the actual date values that are created at the following sections

DateAdd(yy,-49,getDate())
DateAdd(yy,-39,getDate())

In other words, once the query is executed, can I print the SQL Query output so that it shows something like this

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

As always, any feedback would be greatly appreciated.

Thanks Everyone.


Add the two dates to your select list:

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 


The only way i can think is messy. You would build the sql up as a string then execute it using the exec in built function.

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)   
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜