0

我在 Varchar 变量中写了一些逻辑语句,如下所示:

declare @P1 as varchar(max)
   set @P1 = ' (select (Convert(float,(SELECT COUNT(*) FROM  AttendanceMaster AS innerAM WHERE
                                            (AttendanceTypeId IN (SELECT  TypeId FROM AttendanceTypeMaster AS innerATM WHERE 
                                            innerAM.ParshadId = '+@ParshadId+'
                                            and (AttendanceSubjectId ='+Convert(varchar(5),@ASubjectId)+' ))) and innerAM.Attendance=''P1'''+ @innerWhereCondition +'))/6))'

现在我在 Exec 函数中执行了上面的@P1,如下所示

    exec(@P1)

现在我想得到上面 @P1 语句的结果,该语句已经在 EXEC 函数中执行并希望存储在 int 变量中。我在用户定义的函数中编写了上面的两个语句,并希望从上面的两个语句中返回 int。

我怎样才能做到这一点?是否可以分配这样的结果?

Declare @result int

set @result = exec(@P1)

它抛出一个错误说:incorrect syntext near Exec

如何在 SQL Server 的用户定义函数中从 Exec 函数返回结果?

谢谢。

4

2 回答 2

0

您不能EXEC在用户定义的函数中使用。你为什么要这样做呢?

于 2012-09-10T14:03:03.950 回答
0

声明@a varchar(10)

声明@b int

设置@a='2 * 3';

执行('选择' + @a)

声明@res int / ??? /;

声明@sql nvarchar(max);

设置@sql = N'SELECT @res = ' + @a;

exec sp_executesql @sql, N'@res int OUTPUT', @res = @res OUTPUT;

设置@b=@res

选择@b

于 2013-05-29T16:42:12.927 回答