Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL CLR stored procedure output

Tags:

sql

clr

sqlclr

there is a simple class called User and List of its objects

  public class User
{
public int ID;
public string UserName;
public string UserPassword;
}
...
List userList = new List();
   

Can i make this list of User objects as result of execution SLQ CLR stored procedure ? e.g. i want to get this

 
ID   UserName  UserPassword
1    Ted       SomePassword
2    Sam       Password2
3    Bill      dsdsd


[SqlProcedure]
public static void GetAllocations()
{
    // what here ??
}

P.S. Please do not advice me to use Sql functions. It does not suit me because it does not support output parameters

P.S.2 i will be very appreciated for any help !

like image 900
BadEnglish Avatar asked Feb 15 '26 13:02

BadEnglish


1 Answers

Try to create a virtual table with SqlDataRecord and send it over the Pipe property of SqlContext object:

[SqlProcedure]
public static void GetAllocations()
{
    // define table structure
    SqlDataRecord rec = new SqlDataRecord(new SqlMetaData[] {
        new SqlMetaData("ID", SqlDbType.Int),
        new SqlMetaData("UserName", SqlDbType.VarChar),
        new SqlMetaData("UserPassword", SqlDbType.VarChar),
    });

    // start sending and tell the pipe to use the created record
    SqlContext.Pipe.SendResultsStart(rec);
    {
        // send items step by step
        foreach (User user in GetUsers())
        {
            int id = user.ID;
            string userName = user.UserName;
            string userPassword = user.UserPassword;

            // set values
            rec.SetSqlInt32(0, id);
            rec.SetSqlString(1, userName);
            rec.SetSqlString(2, userPassword);

            // send new record/row
            SqlContext.Pipe.SendResultsRow(rec);
        }
    }
    SqlContext.Pipe.SendResultsEnd();    // finish sending
}

Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!