Input format:
In a line of input forms, such as "operand operator operand" expression,
The output format:
In a line of output expressions and calculation results,
Input the sample 1:
Given a set of input here, for example:
21 * 8
The output sample 1:
Here is given the corresponding output, for example:
21 * 8=168
Enter the sample 2:
Given a set of input here, for example:
21! 8
The output sample 1:
Here is given the corresponding output, for example:
Invalid operator
CodePudding user response:
Don't need so many restrictions, only can be able to find the result, please don't come out according to the actual error message:IF OBJECT_ID (' Proc_GetExpressionResult) IS NOT NULL
DROP the PROC Proc_GetExpressionResult
GO
-=============================================
Author: yenange
- the Create date: 2019-12-10
- Description: input expression, and the results
-=============================================
CREATE PROCEDURE Proc_GetExpressionResult
@ expression NVARCHAR (MAX)
AS
The BEGIN
SET NOCOUNT ON;
DECLARE @ SQL NVARCHAR (MAX)
SQL=SET @ 'select' + @ expression
BEGIN the TRY
The EXEC (@ SQL);
END the TRY
The BEGIN CATCH
The SELECT ERROR_MESSAGE () AS ErrorMessage
END the CATCH
END
GO
The EXEC Proc_GetExpressionResult '21 * 8'
/*
168
*/
The EXEC Proc_GetExpressionResult '45/3'
/*
15
*/
The EXEC Proc_GetExpressionResult '21! 8 '
/*
"!" Nearby have a syntax error,
*/
CodePudding user response:
I how also don't understandCodePudding user response:
Get a string into the dynamically generated SQL, then execute,Declare @ SQL nvarchar (Max)
The set @ SQL='select 21 * 8'
Select SQL/@/output select 21 * 8
Exec (@ SQL)//@ defined by the SQL command, output 168
CodePudding user response: