欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

sqlserver 递归删除组织结构树

程序员文章站 2024-02-22 23:01:52
...
/// <summary>
        /// 递归删除岗位节点
        /// </summary>
        /// <param name="PK_ROLE_ID"></param>
        /// <param name="isPost"></param>
        /// <returns></returns>
        public bool DeletePosts(int PK_ROLE_ID)
		{
			
			StringBuilder strSql=new StringBuilder();


             strSql.Append("   WITH post(PK_ROLE_ID, ROLE_FATHER, ROLE_NAME) AS ");
             strSql.Append("       (    ");
             strSql.Append("           SELECT  R.PK_ROLE_ID, R.ROLE_FATHER, R.ROLE_NAME");
             strSql.Append("           FROM [ROLE] R WHERE [email protected]_ROLE_ID");
             strSql.Append("           UNIon ALL");
             strSql.Append("           SELECT sub.PK_ROLE_ID, sub.ROLE_FATHER, sub.ROLE_NAME");
             strSql.Append("           FROM [ROLE] sub, post super");
             strSql.Append("           WHERE sub.ROLE_FATHER = super.PK_ROLE_ID");
             strSql.Append("       )");
             strSql.Append("   delete from [ROLE] where PK_ROLE_ID in(");
             strSql.Append("   select PK_ROLE_ID from post");
             strSql.Append("   )");

			SqlParameter[] parameters = {
					new SqlParameter("@PK_ROLE_ID", SqlDbType.Int,4)
};
			parameters[0].Value = PK_ROLE_ID;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}