Home > database >  O great god answer
O great god answer

Time:09-28

Enter a form like "operand operator operand" expression, to take two integer aliquot and ask yu (%),

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 understand

CodePudding 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:

refer to the second floor qq_46001229 response:
I how also can't read

Expression into to the parameters of the stored procedure,
Stored procedure using dynamic S Q L check this expression results

There is no difficulty
  • Related