views:

43

answers:

1

I would like to turn this query to regular inline sql without using stored procedures

declare @nod hierarchyid
select @nod = DepartmentHierarchyNode
from Organisation
where DepartmentHierarchyNode = 0x6BDA

select * 
from Organisation
where @nod.IsDescendantOf(DepartmentHierarchyNode) = 1

Is there a way to do it?

+3  A: 

Sure, no problem at all....

using(SqlConnection con = new SqlConnection(......))
{
   string stmt = 
      "declare @nod hierarchyid; " + 
      "select @nod = DepartmentHierarchyNode from Organisation where DepartmentHierarchyNode = 0x6BDA; " + 
      "select * from Organisation where @nod.IsDescendantOf(DepartmentHierarchyNode) = 1";

   using(SqlCommand cmd = new SqlCommand(stmt, con))
   {
      con.Open();
      using(SqlDataReader rdr = cmd.ExecuteReader())
      {
         // here, read your values back
      }
      con.Close();
   }
}

That should do it. I don't see any trouble having multiple statements in your inline SQL query, really.

marc_s
what the heck. so basically its pretty much the same thing then? I should have checked before asking this. I appreciate your help.
Luke101
I'm not totally sure, but I gather he might have been asking how to parameterize the `hierarchyid`, which would involve using the `SqlHierarchyId` class (I think). Mind you, the whole thing doesn't make much sense, since he's just selecting the same hierarchyid that he already has in that first query... that whole first half seems redundant.
Aaronaught
@Aaronaught: ok, well, the he could use the "SqlHierarchyId" type from the `Microsoft.SqlServer.Types` assembly and somehow provide a valid value for that parameter
marc_s