SQL Server - ...Need help in below situation. - Asked By shah zeb on 17-Nov-11 02:57 AM

I have an sp written like below.

@var1 = 19
@var2 = 34
 @res = '@var1 * @var2'
//In above statement @var1 + @var2 is stored as variable in db.
//The @res need to execute the above statement.

I need to execute the @res and store value in them.
and then do some operation on them.
Hope you people got my point.

regards,
sz
Chintan Vaghela replied to shah zeb on 17-Nov-11 03:18 AM
Hello

Try this

DECLARE   @var1 as int = 19

DECLARE @var2 as int = 34

 DECLARE @res  as varchar(max)

 

Select  @res = CONVERT(Varchar(max), @var1*@var2)

   

 

   

    print(@res)

    END

shah zeb replied to Chintan Vaghela on 17-Nov-11 04:56 AM
Hi,You specified this.

set QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[TestSP]
AS
BEGIN
DECLARE @var1  int
dECLARE @var2 int
SET @var1 = 10
set @var2 = 23
DECLARE @res  as varchar(max)
//Select  @res = CONVERT(Varchar(max), @var1*@var2)//You specified this if '@var1*@var2' is dealed as a varchar and i want to execute. How that should be done?

print(@res)
END
Chintan Vaghela replied to shah zeb on 17-Nov-11 05:54 AM
Try this

DECLARE   @var1 as varchar(10) = '19'

DECLARE @var2 as varchar(10) = '34'

 DECLARE @res  as varchar(max)

 

Select  @res = CONVERT(Varchar(max),CONVERT(int,@var1)*Convert(int,@var2))

   

 

   

    print(@res)