sql >> Base de Datos >  >> RDS >> Sqlserver

Devolver varias tablas desde un procedimiento almacenado

La forma normal es conseguir todo a la vez.

simplemente construye tu SELECT 's y tendrá un DataSet lleno con todas las tablas.

using (System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(myConnString))
{
    using (System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand())
    {
        cmd.CommandText = "myMultipleTablesSP";
        cmd.Connection = conn;
        cmd.CommandType = CommandType.StoredProcedure;

        conn.Open();

        System.Data.SqlClient.SqlDataAdapter adapter = new System.Data.SqlClient.SqlDataAdapter(cmd);

        DataSet ds = new DataSet();
        adapter.Fill(ds);

        conn.Close();
    }
}

si, por ejemplo, devuelve 2 tablas en su SP, como:

SELECT * FROM [TableA];
SELECT * FROM [TableB];

accedería a estas tablas como:

DataTable tableA = ds.Tables[0];
DataTable tableB = ds.Tables[1];