Assign function result to a table variable

后端 未结 3 1042
猫巷女王i
猫巷女王i 2021-01-15 05:01

The SQL Server (2000/2005) function gets the table name and the field name as parameters and returns results from a dynamic query within the function. The results should be

相关标签:
3条回答
  • 2021-01-15 05:03

    just to close the loop...

    here is the syntax for calling the function and putting those results in a table variable

    small build on @simons solution

    this ran on sql2012 and sql2014.

    [ dont forget to close off the table statement. Easy enough to do if you have the table all on a single line. ]

    declare @t table(field1  nvarchar(100) )
    
    insert @t select * from dbo.Cool_1Field_Function( 'parm1' ,'parm2')
    select * from @t
    
    0 讨论(0)
  • 2021-01-15 05:19

    I'm not sure how this works with functions, but if you have a Stored Procedure that returns a resultset, you can insert that into a table variable using INSERT EXEC statements.

    INSERT @TableVariable
    EXEC spYourProcedure
    

    As long as the fields match that will work. Otherwise you can use:

    INSERT @TableVariable (FieldInSp1, FieldInSp2)
    EXEC spYourProcedure
    

    This way you can pass data between stored procedures. See this page on INSERT EXEC Statements for some extra information.

    0 讨论(0)
  • 2021-01-15 05:23

    You can't use "exec" in a user defined function. UDFs must be side effect free.

    0 讨论(0)
提交回复
热议问题