Can you SET ANSI_NULLS in dynamic sql?

  • I have a stored procedure that can persist queries it generates to either stored procs or multi-statement table valued function. It's working fine except I have been unable to get the procs and functions I create by the dynamic sql to accept ANSI_NULLS ON. It seems that you can do it with QUOTED_IDENTIFIER ON just by doing this right before the dynamic sql to create the object:

    EXEC sp_executesql N'SET QUOTED_IDENTIFIER ON'

    But ANSI_NULLS doesn't "stick" using this same technique. Is there another way to do it? I care about this because I NEED ANSI_NULLS on as these functions are referencing indexed views.

    Thanks,

    -Chad

  • Ahh, figured it out. You can do it by nesting the EXECs

    EXEC ('SET ANSI_NULLS ON EXEC(''' + @sql + ''')')

    cool.

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

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