set ANSI_NULLS ON set QUOTED_IDENTIFIER ON GO -- <><><><><><><><><><><><><> -- <> <> -- <> SQL语句--经典收集 <> -- <> <> -- <><><><><><><><><><><><><> ALTER PROCEDURE [dbo].[Sql_语句经典收集] -- @tp_class varchar(5) = '0', -- @t_rq varchar(5) = '0' @date_1 Datetime = '2011-02-26 00:00:00', @date_2 Datetime = '2012-03-25 23:59:59', @temp_bh int = '120957857' -- 库存物料编号 As BEGIN SET NOCOUNT ON; -- ╔════════════════╗ -- ======================================================== ║ 游标循环读记录 ║ -- ╚════════════════╝ /* 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 -- -- ╔════════════════╗ -- =========================================================== ║ 临时表和try ║ -- ╚════════════════╝ -- 增加临时表 select * into #csj_temp from csj -- 删除临时表 用到try begin try -- 检测代码开始 drop table #csj_temp end try begin catch -- 错误开始 end catch -- ╔════════════════╗ -- ======================================================== ║ 检查表是否存在 ║ -- ╚════════════════╝ use master go -- 批处理的结束 --在[master]中查询[myDBName]是否存在 if exists(select * from sysdatabases where name='myDBName') drop database myDBName -- ╔════════════════╗ -- ======================================================== ║ if语句使用示例 ║ -- ╚════════════════╝ declare @a int set @a=12 if @a>100 begin print @a end else begin print 'no' end -- ╔════════════════╗ -- ======================================================= ║ 变中含有单引号 ║ -- ╚════════════════╝ declare @temp_temp varchar(100) set @temp_temp=''''+'csj'+'''' print @temp_temp -- ╔════════════════════╗ -- =============================================== ║ Exec sp_executesql ║ -- ╚════════════════════╝ declare @Sql_Return int -- 返回值 declare @Sql_Sql nvarchar(2000) -- Sql执行语句 set @Sql_Return = 0 -- 返回值 set @Sql_Sql = '' -- Sql执行语句 set @Sql_Sql='Select @Sql_Return=Sum(AddInVentory) from Fm_MonthEnd' EXEC sp_executesql @Sql_Sql,N'@Sql_Return int output',@Sql_Return OUTPUT print @Sql_Return */ END
作为本人备忘录,随时可拿来用!