JCM0165
asked on
assign value of results from a function within a stored procedure to a variable
in MS SQL 2005
in stored procedure:
DECLARE @Achieved int
SELECT @Function = ROUTINE_SCHEMA+'.' +@Function
FROM information_Schema.Routine s
WHERE ROUTINE_NAME = @Function
SET @FunctionText = N'SELECT @Achieved = dbo.' + @Function + N'("' + cast(@Customer_ID as varchar(5)) + N'", "' + cast(@date as varchar(12)) + N'")' ------
print @FunctionText
SET @Achieved = (??????)
How do I set @achieved to the value returned by the function? It's not working now
Thanks in advance!
Chris
in stored procedure:
DECLARE @Achieved int
SELECT @Function = ROUTINE_SCHEMA+'.' +@Function
FROM information_Schema.Routine
WHERE ROUTINE_NAME = @Function
SET @FunctionText = N'SELECT @Achieved = dbo.' + @Function + N'("' + cast(@Customer_ID as varchar(5)) + N'", "' + cast(@date as varchar(12)) + N'")' ------
print @FunctionText
SET @Achieved = (??????)
How do I set @achieved to the value returned by the function? It's not working now
Thanks in advance!
Chris
ASKER
@Achieved is declare as INT yes
are you trying to assign it a string value on this peice???
@Achieved = dbo.'
the string is intended to query the function which produces an integer:
to simplify:
ALTER FUNCTION [fasterfitness].[F_SELECT_ FITPOINT_A DHERENCE]
(
@Customer_ID int,
@date datetime
)
RETURNS TABLE AS
RETURN
(
-- Add the SELECT statement with parameter references here
SELECT 1 as accomp
)
are you trying to assign it a string value on this peice???
@Achieved = dbo.'
the string is intended to query the function which produces an integer:
to simplify:
ALTER FUNCTION [fasterfitness].[F_SELECT_
(
@Customer_ID int,
@date datetime
)
RETURNS TABLE AS
RETURN
(
-- Add the SELECT statement with parameter references here
SELECT 1 as accomp
)
DECLARE @Achieved int
SELECT @Function = ROUTINE_SCHEMA+'.' +@Function
FROM information_Schema.Routine s
WHERE ROUTINE_NAME = @Function
SET @FunctionText = N'SELECT @Achieved = dbo.' + @Function + N'("' + cast(@Customer_ID as varchar(5)) + N'", "' + cast(@date as varchar(12)) + N'")' ------
print @FunctionText
exec sp_executesql @FunctionText, N'@Achieved int OUTPUT', @Achieved output
SELECT @Function = ROUTINE_SCHEMA+'.' +@Function
FROM information_Schema.Routine
WHERE ROUTINE_NAME = @Function
SET @FunctionText = N'SELECT @Achieved = dbo.' + @Function + N'("' + cast(@Customer_ID as varchar(5)) + N'", "' + cast(@date as varchar(12)) + N'")' ------
print @FunctionText
exec sp_executesql @FunctionText, N'@Achieved int OUTPUT', @Achieved output
ASKER
tried angellll's solution and got error
Cannot find either column "fasterfitness" or the user-defined function or aggregate "fasterfitness.F_SELECT_FI TPOINT_ADH ERENCE", or the name is ambiguous.
(the function name is fasterfitness.F_SELECT_FIT POINT_ADHE RENCE)
Cannot find either column "fasterfitness" or the user-defined function or aggregate "fasterfitness.F_SELECT_FI
(the function name is fasterfitness.F_SELECT_FIT
several things:
* what does print @FunctionText actually print?
* as you use dynamic sql, the permissions needed will be those of the caller of the procedure, and not the ones of the owner of the procedure.
* are you sure that you are in the correct database?
* what does print @FunctionText actually print?
* as you use dynamic sql, the permissions needed will be those of the caller of the procedure, and not the ones of the owner of the procedure.
* are you sure that you are in the correct database?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
are you trying to assign it a string value on this peice???
@Achieved = dbo.'