Urgent Function and Dynamics SQL

  • Hello,

    Sorry for my english.... 😎

    I want execute a dynamics Command sql in "Function Table".

    DECLARE @myFirstRequest VARCHAR(8000);

    select @myFirstRequest=Request_Text from T_REQUEST

    where id_Request = @IdRequest

    insert into @tt

    SELECT E_D.ID from E_D where E_D.IDRETURN in (@myFirstRequest)

    How may i do to keep the value of @myFirstRequest like a SQL command and not like a parameters ?

    Thank you for your answer...

  • The function EXEC is impossible on a SQL function table....

  • My function Table :

    ALTER FUNCTION [dbo].[Synchro_GetIds]

    (

    @IdRequest int;

    )

    RETURNS @tt TABLE

    (

    Ids int NOT NULL

    )

    AS

    Begin

    DECLARE @myFirstRequest VARCHAR(8000);

    -- for obtain the filter request exemple : 'SELECT IDRETURN FROM DB_TEST where IDRETURN>1000'

    select @myFirstRequest=Request_Text from T_REQUEST

    where id_Request = @IdRequest

    insert into @tt

    SELECT E_D.ID from E_D where E_D.IDRETURN in (@myFirstRequest)

    return;

    END

  • i know logically, it makes sense to get the id's you are looking for in one table, adn then do a seperate step;

    but from a good TSQL point of view, you could do it all in one step;

    you really need to do it this way, since dynamic SQL is not permitted in a function.a functions not allowed to do any "side affecting" operations. since you could potentially write dynamic SQL that could change DDL, it's easier to simply no permit dynamic SQL insiode functions.

    ALTER FUNCTION [dbo].[Synchro_GetIds]

    (

    @IdRequest int

    )

    RETURNS @tt TABLE

    (

    Ids int NOT NULL

    )

    AS

    Begin

    insert into @tt

    SELECT

    E_D.ID

    from E_D

    where E_D.IDRETURN in (select Request_Text

    from T_REQUEST

    where id_Request = @IdRequest)

    return;

    END

    i'd consider taking this one step further and changing this to an inline table value function.

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

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

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