博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
在sql语法里面定义变量
阅读量:5060 次
发布时间:2019-06-12

本文共 2132 字,大约阅读时间需要 7 分钟。

               DECLARE @EmployeeName varchar(50)

                set @EmployeeName = '{0}'
                DECLARE @departmentGuid varchar(50)
                declare @Leader int
                select @departmentGuid = g_id_bm,@Leader = bLeader  from sz_bm_ywy where s_mch = @EmployeeName 
                if (@@rowcount = 0) return

                if ( @Leader = 1 )

                    begin
                  declare @Depth int
                  select @Depth =  i_depth  from  sz_tree_bm where g_id = @departmentGuid
                  if (@@rowcount = 0) return
                  if(@Depth = 1)
                  select g_id from  sz_bm_ywy where g_id_bm in ( select g_id_expand from sz_tree_extend where g_id_parent1 = @departmentGuid)
                  if(@Depth = 2)
                  select g_id from  sz_bm_ywy where g_id_bm in ( select g_id_expand from sz_tree_extend where g_id_parent2 = @departmentGuid)
                  if(@Depth = 3)
                  select g_id from  sz_bm_ywy where g_id_bm in ( select g_id_expand from sz_tree_extend where g_id_parent3 = @departmentGuid)
                  if(@Depth = 4)
                  select g_id from  sz_bm_ywy where g_id_bm in ( select g_id_expand from sz_tree_extend where g_id_parent4 = @departmentGuid)
                  if(@Depth = 5)
                  select g_id from  sz_bm_ywy where g_id_bm in ( select g_id_expand from sz_tree_extend where g_id_parent5 = @departmentGuid)
                  if(@Depth = 6)
                  select g_id from  sz_bm_ywy where g_id_bm in ( select g_id_expand from sz_tree_extend where g_id_parent6 = @departmentGuid)
                  if(@Depth = 7)
                  select g_id from  sz_bm_ywy where g_id_bm in ( select g_id_expand from sz_tree_extend where g_id_parent7 = @departmentGuid)
                  if(@Depth = 8)
                  select g_id from  sz_bm_ywy where g_id_bm in ( select g_id_expand from sz_tree_extend where g_id_parent8 = @departmentGuid)
                  if(@Depth = 9)
                  select g_id from  sz_bm_ywy where g_id_bm in ( select g_id_expand from sz_tree_extend where g_id_parent9 = @departmentGuid)
                  if(@Depth = 10)
                  select g_id from  sz_bm_ywy where g_id_bm in ( select g_id_expand from sz_tree_extend where g_id_parent10 = @departmentGuid)
                    end
                if ( @Leader = 0 )

                select g_id from  sz_bm_ywy where  s_mch = @EmployeeName

注解:

1.

begin

end

相当于C#里面的

{ }

2. (@@rowcount 是全局变量 系统自带的 返回执行的行数

3. 判断是否为空 使用下面的例子

if (@ReceiveMoney is null) set @ReceiveMoney = 0;

4. IF 分支可以这样使用

 if @InvoiceDate_old is null

     begin

       //do something

    end

else

 begin

       //do something

    end

 

 

转载于:https://www.cnblogs.com/zhwl/archive/2011/02/24/1964138.html

你可能感兴趣的文章
dwz ie10一直提示数据加载中
查看>>
Windows Phone开发(4):框架和页 转:http://blog.csdn.net/tcjiaan/article/details/7263146
查看>>
Windows Phone Marketplace 发布软件全攻略
查看>>
Unity3D研究院之打开Activity与调用JAVA代码传递参数(十八)【转】
查看>>
语义web基础知识学习
查看>>
hexo个人博客添加宠物/鼠标点击效果/博客管理
查看>>
python asyncio 异步实现mongodb数据转xls文件
查看>>
关于WPF的2000件事 02--WPF界面是如何渲染的?
查看>>
单元测试、、、
查看>>
SVN使用教程总结
查看>>
JS 浏览器对象
查看>>
TestNG入门
查看>>
【ul开发攻略】HTML5/CSS3菜单代码 阴影+发光+圆角
查看>>
虚拟中没有eth0
查看>>
Unity 3D游戏开发学习路线(方法篇)
查看>>
BZOJ2049[Sdoi2008]Cave 洞穴勘测(LCT模板)
查看>>
vuex插件
查看>>
2011年12月09日
查看>>
[ZJOI2007]棋盘制作 【最大同色矩形】
查看>>
合并单元格
查看>>