TSQL Functions Used in the Stored Procedure Push Column to Read

Setup: loading DataReader into DataTable. Trying to change a column in a DataTable after loading.

Problem: ReadOnlyException thrown while trying to modify a column.

Conditions:

  • When a function (udf or system) is applied to a column with an alias in a stored procedure, the column becomes ReadOnly.
  • The error does not start if the column is simply an alias without using a function.
  • The error does not start if the selection is transferred to the function table, then proc selects from this function.
  • The error (obviously) does not occur when setting the column property in ReadOnly in C #.

Question: Is there a way to change the procedure so that the column with the alias with the function used is not ReadOnly? I am looking for an alternative to modify C # or create a function to accomplish what proc already does.

WITH#:

var dt = new DataTable();
using( var sqlDR = objDocsFBO.GetActiveDocsMerged(KeyID) )
{
    dt.Load(sqlDR);
}
foreach( DataRow dr in dt.Rows )
{
    //Testing Alias Alone - Pass
    dr["DocumentPathAlias"] = "file:///" + Server.UrlEncode(dr["DocumentPathAlias"].ToString()).Replace("+", "%20");
    //Testing Function Applied - Fail
    //dr["DocumentPath"] = "file:///" + Server.UrlEncode(dr["DocumentPath"].ToString()).Replace("+", "%20");
}

SQL:

GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[usp_ActiveDocs_RetrieveMerged]
@KeyID INT
AS 
BEGIN
--Testing Select From Function
--SELECT * FROM dbo.ufn_ActiveDocs_RetrieveMerged(@KeyID) --Pass
SELECT AD.ADMergeLogID
, AD.TemplateName
, CONVERT(NVARCHAR(10), AD.InitiatedOn, 101) [CreatedOn]
, (SELECT fn.UserName FROM dbo.ufn_User_GetFullName(AD.InitiatedBy) fn) [CreatedBy]
, AD.DocumentName
, AD.DocumentPath [DocumentPathAlias] --Pass
--, REPLACE(AD.DocumentPath, '\\', '\') [DocumentPath] --Fail
--, dbo.udf_VerifyPath(AD.DocumentPath) [DocumentPath] --Fail
FROM dbo.ActiveDocsMergeLog AD
WHERE AD.DocumentPath != 'DocumentPath not found.'
AND AD.KeyID = @KeyID
END
+5
source share
1 answer

If you put the query in a temporary table, it will override any properties of the SQL table schema.

+1
source

All Articles