Bueno, me ha llevado un rato encontrar la solución (los problemas salen solos)
Situación:
- Tenía dos procedimientos, uno llamaba al otro los dos retornaba un valor. En el segundo controlaba el @@NESTLEVEL para que al llamar al primero el resultado fuera el suyo y no el del segundo.
- Por supuesto el server management funcionaba todo correctamente
- Cuando lo ejecutaba por código, al llamar solo al segundo el @@NESTLEVEL tenía como valor 3 y no lo entendía
Mi problema estaba en como se hacía la consulta por código y el problema estaba aquí. Cuando llamas a un procedimiento usando CommandType.Text y asociando parámetros el @@NESTLEVEL es 3 : documentación del
SQLServer lo dice “sp_executesql, el valor devuelto es 2 +”
Por si lo quieres probar
SQL
Server
CREATE PROCEDURE [dbo].[usp_InnerProc] @miparametro INT AS
SELECT @@NESTLEVEL AS 'Inner Level';
Codigo
Dim
connectionString, sServidor, sBase, cUsuario, cPassword As String
connectionString = "data source = " & sServidor & "; initial catalog = " & sBase &
"; user id = " & cUsuario
& "; password = " &
cPassword & ";"
Dim
cad1 As String
Dim
cad2 As String
Dim
cad3 As String
Using
connection As New
SqlClient.SqlConnection(connectionString)
connection.Open()
Dim
command As SqlCommand
= New SqlCommand("", connection)
Dim
drDatosSql As SqlClient.SqlDataReader
command.CommandText = "usp_InnerProc 1 "
command.CommandType = CommandType.Text
drDatosSql = command.ExecuteReader()
drDatosSql.Read()
cad1 = drDatosSql.GetValue(0)
command.Dispose()
drDatosSql.Close()
' cad =1
Dim
command2 As SqlCommand
= New SqlCommand("", connection)
Dim
drDatosSql2 As SqlClient.SqlDataReader
command2.CommandText = "usp_InnerProc"
command2.CommandType = CommandType.StoredProcedure
Dim
idParam As SqlParameter
= New SqlParameter("@miparametro", SqlDbType.Int) With {.Value = 1}
command2.Parameters.Add(idParam)
drDatosSql2 =
command2.ExecuteReader()
drDatosSql2.Read()
cad2 = drDatosSql2.GetValue(0)
drDatosSql2.Close()
' cad2 =1
Dim
command3 As SqlCommand
= New SqlCommand("", connection)
Dim
drDatosSql3 As SqlClient.SqlDataReader
command3.CommandText = "usp_InnerProc @miparametro"
command3.CommandType = CommandType.Text
Dim
idParam3 As SqlParameter
= New SqlParameter("@miparametro", SqlDbType.Int)
With {.Value = 1}
command3.Parameters.Add(idParam3)
drDatosSql3 =
command3.ExecuteReader()
drDatosSql3.Read()
cad3 = drDatosSql3.GetValue(0)
' cad3 =3
End Using