SQL Server administration and T-SQL development, Web Programming with ASP.NET, HTML5 and Javascript, Windows Phone 8 app development, SAP Smartforms and ABAP Programming, Windows 7, Visual Studio and MS Office software
Development resources, articles, tutorials, code samples, tools and downloads for ASP.Net, SQL Server, Reporting Services, T-SQL, Windows, AWS, SAP HANA and ABAP


SQL Server EXEC or SP_EXECUTESQL Commands in User Defined Functions


I developed a user-defined function named ReplaceTextColumn. I build the udf function and coded t-sql statements in order to build dynamic t-sql codes. I tried to use this dynamic sql codes to replace text or ntext sql data type columns in sql databases.

I tried to execute the resultant t-sql code block by calling the EXEC sp_executesql syntax but get the following SQL Server error message, Msg 443

Msg 443, Level 16, State 14, Procedure ReplaceTextColumn, Line 20
Invalid use of a side-effecting operator 'INSERT EXEC' within a function.
Invalid use of a side-effecting operator 'EXECUTE STRING' within a function.
Code




Also calling the user defined function in a select statement resultant with sql engine raise the following error message Msg 557

select dbo.ReplaceTextColumn('','','','','','')
--Msg 557, Level 16, State 2, Line 1
--Only functions and some extended stored procedures can be executed from within a function.
Code

So I changed the structure to use stored procedure instead of using user defined functions (udf) because of the limitations of user functions in SQL Server.



SQL Server

SQL Server 2019 Installation
download SQL Server 2019
download SQL Server 2017
download SQL Server 2016
download SQL Server 2014
download SQL Server 2012
MacOS ve SQL Server 2019


Copyright © 2004 - 2021 Eralper YILMAZ. All rights reserved.