if…then.. example

Here is a sampel of using a if … them within a SQL query.

set nocount on
SELECT soft.ARPDisplayName0 as ‘Ansys Software’, soft.VersionMajor0 as ‘Major Version’,
COUNT(soft.VersionMajor0) AS ‘Count’, @dep AS ‘Department’
into #tmp
FROM

dbo.v_RA_System_SystemOUName ou,
dbo.v_GS_INSTALLED_SOFTWARE soft LEFT JOIN dbo.v_GS_System sys ON sys.ResourceID = soft.ResourceID
WHERE
ou.ResourceID = sys.ResourceID
–and soft.ARPDisplayName0 like ‘Ansys%’
GROUP BY soft.ARPDisplayName0, soft.VersionMajor0
ORDER BY soft.ARPDisplayName0, soft.VersionMajor0
set nocount off

declare @v as varchar(250)
set @v = (select top 1 Department from #tmp)
If isnull(@v,’zzzzzzzz’) = ‘zzzzzzzz’
begin
Select ‘n/a’ as ‘Ansys Software’, ‘n/a’ as ‘Major Version’, ‘n/a’ AS ‘Count’, ‘n/a’ AS ‘Department’
end
Else
begin
Select * from #tmp
end

set nocount on
drop table #tmp
set nocount off

Leave a Reply

Your email address will not be published. Required fields are marked *