本人收藏的Sql server经典t-sql语句(备忘录,持续更新中)

本文档提供了一系列SQL语句的实用技巧,包括设置SQL模式、游标操作、临时表管理和错误处理等,有助于提高SQL查询效率及编程能力。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

作为本人备忘录,随时可拿来用!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值