Inserting results from extended stored proc into table variable within UD function generates error

  • Hi,

    We have a mixture of 32-bit and 64-bit database servers, all running SS2K5 SP2.

    In various scripts and procedures (particulary when executing an SSIS package using DTExec), I need to know which platform I am running from (ie 32-bit or 64-bit).

    I am trying to create a function that will simply tell me whether it is 64-bit or 32-bit. Here is the code:

    CREATE FUNCTION dbo.fnS_Is_64bit_SQL ()

    RETURNS bit

    AS

    BEGIN

    DECLARE @Is64bit bit

    DECLARE @MSVer table(

    [Index] int not null,

    [Name] varchar(255) not null,

    Internal_Value int null,

    Character_Value varchar(1024) null

    )

    INSERT INTO @MSVer ([Index], [Name], Internal_Value, Character_Value)

    EXEC xp_msver 'FileDescription'

    SELECT @Is64bit = CASE WHEN charindex('64', Character_Value, 1) > 0 THEN 1 ELSE 0 END

    FROM @MSVer

    RETURN @Is64Bit

    END

    It generates an error message when trying to create the function:

    "Invalid use of side-effecting or time-dependent operator in 'INSERT EXEC' within a function."

    According to BOL, the following are all valid within UDFs

    "UPDATE, INSERT, and DELETE statements modifying table variables that are local to the function."

    "EXECUTE statements calling an extended stored procedure."

    Why am I unable to create this function?

    Any assistance gratefully received!

    Thanks,

    Simon

  • Well both Functions and INSERT..EXEC have a lot of restrictions on them. So many in fact that AFAIK, you cannot use INSERT_EXEC in a user-defined function, because:

    - INSERT..EXEC cannot be used to write to table variables, and...

    - User-Defined functions cannot write to any other kind of table.

    [font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
    Proactive Performance Solutions, Inc.
    [/font]
    [font="Verdana"] "Performance is our middle name."[/font]

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply