Question

I had made a dynamic stored procedure like this

CREATE PROCEDURE [dbo].[MyProcedure]
    @pSelect nvarchar(max)
AS
BEGIN
    SET NOCOUNT ON;

    DECLARE @SQL nvarchar(max)

    SET @SQL = 'select ' + @pSelect + ' from tabel1';

    EXEC (@SQL)
END

And on updating my entitydatamodel the in context.cs the above stored procedure is in the form of

 virtual int MyProcedure(string pSelect)
            {
                var pSelectParameter = pSelect != null ?
                    new ObjectParameter("pSelect", pSelect) :
                    new ObjectParameter("pSelect", typeof(string));

                return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction("MyProcedure", pSelectParameter);
            }

on calling the stored procedure from c# code

var result = myDataModel.MyProcedure("Select * From table1").tolist();

the above code is showing error because MyProcedure is returning a integer return type
so how could i set the return type of the stored procedure according to tje select query I am passing to it

HOW DO I MODIFY MY STORED PROCEDURE SO THAT ITS RETURN TYPE IS OF ANY SPECIFIC TABLE TYPE

Was it helpful?

Solution

In this case you have to trick the code.

CREATE PROCEDURE [dbo].[MyProcedure]
    @pSelect nvarchar(max)
AS
BEGIN
    SET NOCOUNT ON;

    DECLARE @SQL nvarchar(max)

    SET @SQL = 'select ' + @pSelect + ' from tabel1';

    EXEC (@SQL)

   --Remove the below line once you have added the stored procedure to the dbml file.
    select * from table1
END

After creating the sp, drag and drop to the c# dbml file. then you can alter the sp by removing the line " select * from table1".

NOTE : if you dont have those columns in the table1, the direct values(any datatype) in the select statement like "select 1 as colmumn1, 'string' as colmumn2, cast('10/01/1900' as datetime) as colmumn3 from table1"

OTHER TIPS

just add @ sign in your parameter.

 var pSelectParameter = pSelect != null ?
                new ObjectParameter("@pSelect", pSelect) :
                new ObjectParameter("@pSelect", typeof(string));

may be this should work and i believe your are passing only column name in this parameter.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top