如何在IF语句中进行区分大小写的匹配

时间:2015-08-04 19:49:52

标签: sql-server tsql

我正在做一个IF声明,想要完全匹配。

IF ( @a_Action <> 'Power On' AND @a_Action <> 'Power Off' AND @a_Action <> 'Reset' )
BEGIN
    -- Invalid action - an IPMI command. 
    -- Set call as failed.
    SELECT @ErrorSw = 'Y'
    SELECT @ApiResultOut = 0
    SELECT @ApiResultCodeOut = 1610
    SELECT @ApiMessageOut = 'Warning - action type "' + @a_Action + '" is not a valid server (IPMI) command.'
END

如果@a_Action参数作为“开机”发送,它将通过而不是错误输出。只有完全匹配才能通过。

2 个答案:

答案 0 :(得分:5)

DECLARE @a_Action VARCHAR(20)
SET @a_Action = 'Power on' COLLATE SQL_Latin1_General_CP1_CS_AS

IF (@a_Action = 'Power On' COLLATE SQL_Latin1_General_CP1_CS_AS)
    PRINT 'match'
ELSE
    PRINT 'not match'

答案 1 :(得分:1)

DECLARE @A varchar(10) = 'Power on' 

IF @A COLLATE SQL_Latin1_General_CP1_CI_AS NOT IN ( 'Power On', 'Power Off', 'Reset' ) 
BEGIN
    Print 'Invalid command'
END
ELSE
BEGIN
    PRINT 'OK command'
END ;
相关问题