是否可以从SQL中的函数调用存储过程?

时间:2017-10-04 20:14:09

标签: sql sql-server-2012

ASP.NET论坛上有一个answer似乎意味着它可以绕过“函数”中没有存储过程。不使用OPENROWSET的规则。链接中的代码如下:

create function demofunc(@input varchar(200))
returns table
as
begin
    declare @string1 varchar(100);
    declare @finalstring as varchar(100);

    set @string1 = '%';
    set @finalstring = @input + @string1;

    declare @table as table (define your table here)

    insert into @table
        EXEC sp_FindStringKeyInTable '', 'dbo', 'resultCustKeywordSearchView'

    select * from @table

    return
  end

我尝试使用它时出现此错误:

  

无效使用副作用操作符' INSERT EXEC'在一个函数内。

这是我的代码:

CREATE FUNCTION dbo.crds_GetFormAnswer
     (@FieldName varchar(max), 
      @TableName varchar(max), 
      @PrimaryKeyColumnName varchar(max), 
      @DataRecordId int)  
RETURNS varchar(max)
AS
BEGIN
    DECLARE @temp_table AS TABLE (Form_Answers varchar(max))

    INSERT INTO @temp_table (Form_Answers)
    --VALUES ( '12345' ) //This code works
       EXEC crds_sp_GetFormAnswer @FieldName = 'Submission_Data', @TableName = 'cr_Submissions',@PrimaryKeyColumnName = 'Submission_ID', @DataRecordId = 15;

    RETURN (select TOP 1 Form_Answers from @temp_table)
END 
GO  

SELECT [dbo].crds_GetFormAnswer('Submission_Data', 'cr_Submissions', 'Submission_ID', 15)

0 个答案:

没有答案