spinzr0
asked on
Using a query in an if statement
I need to use an if statement to determine if one value is a 1 or 0. If its a one, the query should be X, otherwise it should be y. The easiest way to explain it is how I assumed the query would be:
IF(SELECT DEFAULT_EMAIL_LIMITS FROM [AD].[dbo].[V_1] = 1)
SELECT [LOGON_ID],[EMAIL_SERVER], [DEFAULT_E MAIL_LIMIT S],[WARNIN G_LIMIT],[ SEND_LIMIT ],[SEND_RE CEIVE_LIMI T] FROM [AD].[dbo].[V_1]
ELSE
SELECT [LOGON_ID],[EMAIL_SERVER], [DEFAULT_E MAIL_LIMIT S],'defaul t' as WARNING_LIMIT,'default' as SEND_LIMIT,'default' as SEND_RECEIVE_LIMIT FROM [AD].[dbo].[V_1]
If this isn't clear please let me know and I can explain more
IF(SELECT DEFAULT_EMAIL_LIMITS FROM [AD].[dbo].[V_1] = 1)
SELECT [LOGON_ID],[EMAIL_SERVER],
ELSE
SELECT [LOGON_ID],[EMAIL_SERVER],
If this isn't clear please let me know and I can explain more
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for the quick help.
A query can only return columns of a specific type. So you cannot have it return string 'default' in one case and 60 in another case (morphing types).
If you are happy for the data to always come back as VARCHAR, you will have to cast the numeric data to varchar.
e.g.
SELECT [LOGON_ID],[EMAIL_SERVER],
convert(varchar(10),[WARNI
convert(varchar(10),[SEND_
convert(varchar(10),[SEND_
FROM [AD].[dbo].[V_1]
WHERE DEFAULT_EMAIL_LIMITS = 1
UNION ALL
SELECT [LOGON_ID],[EMAIL_SERVER],
'default' as WARNING_LIMIT,
'default' as SEND_LIMIT,
'default' as SEND_RECEIVE_LIMIT
FROM [AD].[dbo].[V_1]
WHERE ISNULL(DEFAULT_EMAIL_LIMIT