-- ╔════════╗
-- =============================== ║ if语句使用示例 ║
-- ╚════════╝
declare @a int
set @a=12
if @a>100
begin
print @a
end
else
begin
print 'no'
end
-- ╔══════════╗
-- =============================== ║ while语句使用示例 ║
-- ╚══════════╝
declare @i int
set @i=1
while @i<30
begin
insert into test (userid) values(@i)
set @i=@i+1
end
-- 设置重复执行 SQL 语句或语句块的条件。只要指定的条件为真,就重复执行语句。可以使用 BREAK 和 CONTINUE 关键字在循环内部控制 WHILE 循环中语句的执行。 本条为以前从网上查找获取!
-- ╔════════╗
-- ================================ ║ 临时表和try ║
-- ╚════════╝
-- 增加临时表
select * into #csj_temp from csj
-- 删除临时表 用到try
begin try -- 检测代码开始
drop table #csj_temp
end try
begin catch -- 错误开始
end catch
-- ╔═════════╗
-- =============================== ║ 游标循环读记录 ║
-- ╚═════════╝
declare @temp_temp int
--declare @Cur_Name
--@Cur_Name="aaa"
--------------------------------- 创建游标 --Local(本地游标)
DECLARE aaa CURSOR for select House_Id from House_House where Deleted=0 or deleted is null
----------------------------------- 打开游标
Open aaa
----------------------------------- 遍历和获取游标
fetch next from aaa into @temp_temp
--print @temp_temp
while @@fetch_status=0
begin
--做你要做的事
select * from House_monthEnd where House_Id=@temp_temp
fetch next from aaa into @temp_temp -- 取值赋给变量
--
end
----------------------------------- 关闭游标
Close aaa
----------------------------------- 删除游标
Deallocate aaa
--
<li class="alt"><span><span> </span><span class="comment">-- ╔════════╗ </span><span> </span></span></li><li class=""><span> <span class="comment">-- =============================== ║ if语句使用示例 ║ </span><span> </span></span></li><li class="alt"><span> <span class="comment">-- ╚════════╝ </span><span> </span></span></li><li class=""><span> <span class="keyword">declare</span><span> @a </span><span class="keyword">int</span><span> </span></span></li><li class="alt"><span> <span class="keyword">set</span><span> @a=12 </span></span></li><li class=""><span> if @a>100 </span></li><li class="alt"><span> <span class="keyword">begin</span><span> </span></span></li><li class=""><span> print @a </span></li><li class="alt"><span> <span class="keyword">end</span><span> </span></span></li><li class=""><span> <span class="keyword">else</span><span> </span></span></li><li class="alt"><span> <span class="keyword">begin</span><span> </span></span></li><li class=""><span> print <span class="string">'no'</span><span> </span></span></li><li class="alt"><span> <span class="keyword">end</span><span> </span></span></li><li class=""><span> <span class="comment">-- ╔══════════╗ </span><span> </span></span></li><li class="alt"><span> <span class="comment">-- =============================== ║ while语句使用示例 ║ </span><span> </span></span></li><li class=""><span> <span class="comment">-- ╚══════════╝</span><span> </span></span></li><li class="alt"><span><span class="keyword">declare</span><span> @i </span><span class="keyword">int</span><span> </span></span></li><li class=""><span><span class="keyword">set</span><span> @i=1 </span></span></li><li class="alt"><span>while @i<30 </span></li><li class=""><span> <span class="keyword">begin</span><span> </span></span></li><li class="alt"><span> <span class="keyword">insert</span><span> </span><span class="keyword">into</span><span> test (userid) </span><span class="keyword">values</span><span>(@i) </span></span></li><li class=""><span> <span class="keyword">set</span><span> @i=@i+1 </span></span></li><li class="alt"><span><span class="keyword">end</span><span> </span></span></li><li class=""><span> </span></li><li class="alt"><span><span class="comment">-- 设置重复执行 SQL 语句或语句块的条件。只要指定的条件为真,就重复执行语句。可以使用 BREAK 和 CONTINUE 关键字在循环内部控制 WHILE 循环中语句的执行。 本条为以前从网上查找获取!</span><span> </span></span></li><li class=""><span> </span></li><li class="alt"><span> </span></li><li class=""><span> <span class="comment">-- ╔════════╗ </span><span> </span></span></li><li class="alt"><span> <span class="comment">-- ================================ ║ 临时表和try ║ </span><span> </span></span></li><li class=""><span> <span class="comment">-- ╚════════╝ </span><span> </span></span></li><li class="alt"><span> </span></li><li class=""><span> <span class="comment">-- 增加临时表 </span><span> </span></span></li><li class="alt"><span> <span class="keyword">select</span><span> * </span><span class="keyword">into</span><span> #csj_temp </span><span class="keyword">from</span><span> csj </span></span></li><li class=""><span> </span></li><li class="alt"><span> <span class="comment">-- 删除临时表 用到try </span><span> </span></span></li><li class=""><span> <span class="keyword">begin</span><span> try </span><span class="comment">-- 检测代码开始 </span><span> </span></span></li><li class="alt"><span> <span class="keyword">drop</span><span> </span><span class="keyword">table</span><span> #csj_temp </span></span></li><li class=""><span> <span class="keyword">end</span><span> try </span></span></li><li class="alt"><span> </span></li><li class=""><span> <span class="keyword">begin</span><span> catch </span><span class="comment">-- 错误开始 </span><span> </span></span></li><li class="alt"><span> <span class="keyword">end</span><span> catch </span></span></li><li class=""><span> </span></li><li class="alt"><span> </span></li><li class=""><span> <span class="comment">-- ╔═════════╗ </span><span> </span></span></li><li class="alt"><span> <span class="comment">-- =============================== ║ 游标循环读记录 ║ </span><span> </span></span></li><li class=""><span> <span class="comment">-- ╚═════════╝ </span><span> </span></span></li><li class="alt"><span> </span></li><li class=""><span> <span class="keyword">declare</span><span> @temp_temp </span><span class="keyword">int</span><span> </span></span></li><li class="alt"><span> <span class="comment">--declare @Cur_Name </span><span> </span></span></li><li class=""><span> <span class="comment">--@Cur_Name="aaa" </span><span> </span></span></li><li class="alt"><span> <span class="comment">--------------------------------- 创建游标 --Local(本地游标) </span><span> </span></span></li><li class=""><span> <span class="keyword">DECLARE</span><span> aaa </span><span class="keyword">CURSOR</span><span> </span><span class="keyword">for</span><span> </span><span class="keyword">select</span><span> House_Id </span><span class="keyword">from</span><span> House_House </span><span class="keyword">where</span><span> Deleted=0 </span><span class="op">or</span><span> deleted </span><span class="keyword">is</span><span> </span><span class="op">null</span><span> </span></span></li><li class="alt"><span> <span class="comment">----------------------------------- 打开游标 </span><span> </span></span></li><li class=""><span> <span class="keyword">Open</span><span> aaa </span></span></li><li class="alt"><span> <span class="comment">----------------------------------- 遍历和获取游标 </span><span> </span></span></li><li class=""><span> </span></li><li class="alt"><span> <span class="keyword">fetch</span><span> </span><span class="keyword">next</span><span> </span><span class="keyword">from</span><span> aaa </span><span class="keyword">into</span><span> @temp_temp </span></span></li><li class=""><span> <span class="comment">--print @temp_temp </span><span> </span></span></li><li class="alt"><span> while @@fetch_status=0 </span></li><li class=""><span> <span class="keyword">begin</span><span> </span></span></li><li class="alt"><span> <span class="comment">--做你要做的事 </span><span> </span></span></li><li class=""><span> <span class="keyword">select</span><span> * </span><span class="keyword">from</span><span> House_monthEnd </span><span class="keyword">where</span><span> House_Id=@temp_temp </span></span></li><li class="alt"><span> </span></li><li class=""><span> <span class="keyword">fetch</span><span> </span><span class="keyword">next</span><span> </span><span class="keyword">from</span><span> aaa </span><span class="keyword">into</span><span> @temp_temp </span><span class="comment">-- 取值赋给变量 </span><span> </span></span></li><li class="alt"><span> </span></li><li class=""><span> <span class="comment">-- </span><span> </span></span></li><li class="alt"><span> <span class="keyword">end</span><span> </span></span></li><li class=""><span> </span></li><li class="alt"><span> <span class="comment">----------------------------------- 关闭游标 </span><span> </span></span></li><li class=""><span> <span class="keyword">Close</span><span> aaa </span></span></li><li class="alt"><span> <span class="comment">----------------------------------- 删除游标 </span><span> </span></span></li><li class=""><span> <span class="keyword">Deallocate</span><span> aaa </span></span></li><li class="alt"><span> <span class="comment">-- </span><span> </span></span></li><li class=""><span> </span></li><li class="alt"><span> </span></li><li class=""><span> </span></li><li class="alt"><span> </span></li><li class=""><span> </span></li><li class="alt"><span> </span></li><li class=""><span> </span></li><li class="alt"><span> </span></li><li class=""><span> </span></li><li class="alt"><span> </span></li><li class=""><span> </span></li><li class="alt"><span> </span></li>