在sql中计算字符串表达式 [英] Evaluate a string expression in sql
本文介绍了在sql中计算字符串表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个存储在字符串中的表达式,例如:
@ myString ='1 *(1 + 0)'
我想评估这个,我该怎么做?
使用
I have an expression stored in string such as :
@myString='1 * (1 + 0)'
I want to evaluate this,how can i do it?
Using
EXEC('SELECT 1*(1+0)')
这是可能的,但现在表达式是字符串变量@myString然后应该是什么步骤?
it is possible ,but now the expression is in string variable @myString then what should be the steps??
推荐答案
>
查看此
Hi,
Check This
declare @@SQL VARCHAR(200)
DECLARE @myString VARCHAR(200)
SET @myString='1 * (1 + 0)'
SET @@SQL='SELECT ' + @myString
EXEC(@@SQL)
希望这会对你有所帮助。
干杯
Hope this will help you.
Cheers
您好,
查看以下内容......
Hi,
Check below...
--PR_TEST_OP '1 * (1 + 0)'
CREATE PROC PR_TEST_OP
(
@myString as varchar(100)
)
AS
BEGIN
declare @@SQL VARCHAR(200)
DECLARE @CNT INT
--SET @myString='1 * (1 + 0)'
SET @@SQL=' SELECT ' + @myString
PRINT (@@SQL)
CREATE TABLE #TMP_1
(col1 int)
INSERT INTO #TMP_1
EXEC(@@SQL)
SELECT @CNT=col1 FROM #TMP_1
IF @CNT >0
BEGIN
PRINT('OK')
END
ELSE
BEGIN
PRINT('NOT OK')
END
END
希望这会对你有所帮助。
干杯
Hope this will help you.
Cheers
尝试这个....
Hi,
Try this one....
--PR_TEST_OP_NEW '1 * (1 + 0)'
CREATE PROC PR_TEST_OP_NEW
(
@myString as varchar(100)
)
AS
BEGIN
declare @mSQL NVARCHAR(200)
DECLARE @CNT AS INT
SET @mSQL='
SELECT @CNT= ' + @myString
Print(@mSQL)
EXEC sp_executesql
@mSQL,N'@CNT INT OUTPUT',@CNT OUTPUT
SELECT @CNT
if @CNT > 0
BEGIN
Print('Done')
END
ELSE
BEGIN
Print('Not Done')
END
END
希望这会对你有所帮助。
此外,请仔细阅读 sp_executesql [ ^ ]
干杯
Hope this will help you.
Also, go through this sp_executesql[^]
Cheers
这篇关于在sql中计算字符串表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文