How can I save the EXEC Result to a Variable.
For example:
declare @.myString as varchar(50)
declare @.myValue as decimal(12,2)
set @.myString='Select ' + '10-5'
EXEC (@.myString)
Print @.myalue <-- Should print 5you can use sp_ExecuteSql. Check BOL for more info on this. or google it.
--
Av.
http://dotnetjunkies.com/WebLog/avnrao
http://www28.brinkster.com/avdotnet
"Luqman" <pearlsoft@.cyber.net.pk> wrote in message
news:uP38IDcDFHA.3340@.TK2MSFTNGP10.phx.gbl...
> How can I save the EXEC Result to a Variable.
> For example:
> declare @.myString as varchar(50)
> declare @.myValue as decimal(12,2)
> set @.myString='Select ' + '10-5'
> EXEC (@.myString)
> Print @.myalue <-- Should print 5
>
>
>|||You can also do this using either temp tables or a user-defined function:
CREATE FUNCTION udf_test( @.value1 DECIMAL( 12, 2 ), @.value2 DECIMAL( 12, 2 )
)
RETURNS DECIMAL( 12, 2 )
AS
BEGIN
DECLARE @.result DECIMAL( 12, 2 )
SET @.result = @.value1 - @.value2
RETURN @.result
END
GO
DECLARE @.myString VARCHAR(50)
DECLARE @.myValue DECIMAL( 12, 2 )
-- Temp table way
CREATE TABLE #result ( result DECIMAL( 12, 2 ) )
SET @.myString = 'INSERT INTO #result SELECT ' + '10-5'
EXEC( @.myString )
SELECT * FROM #result
DROP TABLE #result
-- User defined function way (Make sure you set the database owner dbo to
whatever you need)
SET @.myValue = dbo.udf_test( 10, 5 )
PRINT @.myValue
DROP FUNCTION udf_test
GO
"Luqman" wrote:
> How can I save the EXEC Result to a Variable.
> For example:
> declare @.myString as varchar(50)
> declare @.myValue as decimal(12,2)
> set @.myString='Select ' + '10-5'
> EXEC (@.myString)
> Print @.myalue <-- Should print 5
>
>
>
>sql
Showing posts with label declare. Show all posts
Showing posts with label declare. Show all posts
Monday, March 26, 2012
Friday, March 9, 2012
Sargable Rewrite
Is there a way to rewrite the following Where clause to make it Sargable?
DECLARE @.dtmNow datetime
DELCARE @.days int
WHERE DateDiff(d, cr.CreatedAt, @.dtmNow) >= @.days
--
Message posted via SQLMonster.com
http://www.sqlmonster.com/Uwe/Forums.aspx/sql-server/200702/1"cbrichards" <u3288@.uwe> wrote in message news:6d5e19f0998c9@.uwe...
> Is there a way to rewrite the following Where clause to make it Sargable?
> DECLARE @.dtmNow datetime
> DELCARE @.days int
>
> WHERE DateDiff(d, cr.CreatedAt, @.dtmNow) >= @.days
>
something like
WHERE cr.CreatedAt <= dateadd(d,-5,@.dtmNow)
David|||On Feb 5, 4:24 pm, "cbrichards" <u3288@.uwe> wrote:
> Is there a way to rewrite the following Where clause to make it Sargable?
> DECLARE @.dtmNow datetime
> DELCARE @.days int
> WHERE DateDiff(d, cr.CreatedAt, @.dtmNow) >= @.days
> --
> Message posted via SQLMonster.comhttp://www.sqlmonster.com/Uwe/Forums.aspx/sql-server/200702/1
WHERE cr.CreatedAt < DATEADD(day, @.days, @.dtmNow)
DECLARE @.dtmNow datetime
DELCARE @.days int
WHERE DateDiff(d, cr.CreatedAt, @.dtmNow) >= @.days
--
Message posted via SQLMonster.com
http://www.sqlmonster.com/Uwe/Forums.aspx/sql-server/200702/1"cbrichards" <u3288@.uwe> wrote in message news:6d5e19f0998c9@.uwe...
> Is there a way to rewrite the following Where clause to make it Sargable?
> DECLARE @.dtmNow datetime
> DELCARE @.days int
>
> WHERE DateDiff(d, cr.CreatedAt, @.dtmNow) >= @.days
>
something like
WHERE cr.CreatedAt <= dateadd(d,-5,@.dtmNow)
David|||On Feb 5, 4:24 pm, "cbrichards" <u3288@.uwe> wrote:
> Is there a way to rewrite the following Where clause to make it Sargable?
> DECLARE @.dtmNow datetime
> DELCARE @.days int
> WHERE DateDiff(d, cr.CreatedAt, @.dtmNow) >= @.days
> --
> Message posted via SQLMonster.comhttp://www.sqlmonster.com/Uwe/Forums.aspx/sql-server/200702/1
WHERE cr.CreatedAt < DATEADD(day, @.days, @.dtmNow)
Subscribe to:
Posts (Atom)