SQLTeam.com | Weblogs | Forums

How to use all as variables in if condition


#1

Hi,

I have below scenario to be used in the if condition,

BEGIN
@V_VALUE INT = 100 /Assume the value from table 1/
@V_OPERATOR VARCHAR(10)= '<' /Assume the value from table 2/
@V_REFERENCE VARCHAR(20)='10' /Assume the value from table 2/

IF (@V_VALUE @V_OPERATOR @V_REFERENCE)
PRINT 'TRUE'
ELSE
PRINT 'FALSE'
END

How to pass the value of variables in the if condition in this scenario.

Is there anyway to achieve this.

Thanks in advance.


#2
DECLARE	@V_VALUE INT = 100,		/*Assume the value from table 1*/
	@V_OPERATOR VARCHAR(10)= '<',	/*Assume the value from table 2*/ 
	@V_REFERENCE VARCHAR(20)='10'	/*Assume the value from table 2*/
--
DECLARE	@strSQL	nvarchar(4000),
	@intReturnValue	int
--
SELECT	@strSQL = 'SELECT @intReturnValue = CASE WHEN '
			+ CONVERT(varchar(20), @V_VALUE)
			+ @V_OPERATOR
			+ @V_REFERENCE
			+ ' THEN 1 ELSE 0 END'
--
PRINT	'@strSQL=' + @strSQL	-- Debug
--
EXEC	sp_ExecuteSQL
		@strSQL,
		N'@intReturnValue int OUTPUT',
		@intReturnValue = @intReturnValue OUTPUT
--
IF (@intReturnValue = 1)
BEGIN
	PRINT 'TRUE'
END
ELSE 
BEGIN
	PRINT 'FALSE'
END