Click here to Skip to main content
15,301,841 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I have an expression stored in string such as :

@myString='1 * (1 + 0)'

I want to evaluate this,how can i do it?

Using
SQL
EXEC('SELECT 1*(1+0)')
it is possible ,but now the expression is in string variable @myString then what should be the steps??
Posted

Hi,

Check This


SQL
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
   
Comments
NiteshPanj 12-Aug-14 3:04am
   
How to display result of EXEC(@@SQL)??It is not showing
Magic Wonder 12-Aug-14 3:08am
   
If you want to display the query....use Print(@@SQL)
NiteshPanj 12-Aug-14 3:06am
   
done thanxx
Magic Wonder 12-Aug-14 3:08am
   
Your Welcome
NiteshPanj 12-Aug-14 3:30am
   
I want to use if condition on result obtained of exec(@@sql) like
if(Exec(@@sql)>0)
then
so....
else
so.....

can u help in this??

thanxx
Magic Wonder 12-Aug-14 3:51am
   
Check new Solution
Hi,

Check below...


SQL
--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
   
Comments
NiteshPanj 12-Aug-14 4:07am
   
exactly wat i wanted thnx
Magic Wonder 12-Aug-14 4:59am
   
Welcome a..Gain.
NiteshPanj 12-Aug-14 8:39am
   
final que can u help me in assigning exec(@@sql) value to sql variable as i dant want to create a table
Magic Wonder 12-Aug-14 9:20am
   
Check Solution 3.
Hi,

Try this one....


SQL
--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 


Hope this will help you.

Also, go through this sp_executesql[^]

Cheers
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900