zoukankan      html  css  js  c++  java
  • sp_executesql返回多个输出参数

    参考下面sp_executesql单个输出参数,@I变量为动态列名,列名后缀n或是c,为了比较更新前后值是否发生了变化,分别做了两次SELECT取值,程代码冗余和产生不容忽视性能的问题。:

    View Code
                    SET @sql = N'SELECT @N = ['+ CONVERT(NVARCHAR(MAX),@I+'n] FROM #inserted'
                    EXECUTE sp_executesql @sql,
                                          N'@N DECIMAL(18,0) OUTPUT',
                                          @Nn OUTPUT;
                    SET @sql = N'SELECT @O = ['+ CONVERT(NVARCHAR(MAX),@I+'n] FROM #deleted'        
                    EXECUTE sp_executesql @sql,
                                          N'@O DECIMAL(18,0) OUTPUT',
                                          @On OUTPUT;    
                    IF (ISNULL(@Nn,0<> ISNULL(@On,0))                
                        --EXECUTE [dbo].[usp_Audit_Insert] ...                           
                
                    
                    SET @sql = N'SELECT @N = ['+ CONVERT(NVARCHAR(MAX),@I+'c] FROM #inserted'
                    EXECUTE sp_executesql @sql,
                                          N'@N DECIMAL(18,0) OUTPUT',
                                          @Nc OUTPUT;
                    SET @sql = N'SELECT @O = ['+ CONVERT(NVARCHAR(MAX),@I+'c] FROM #deleted'        
                    EXECUTE sp_executesql @sql,
                                          N'@O DECIMAL(18,0) OUTPUT',
                                          @Oc OUTPUT;    
                    IF (ISNULL(@Nc,0<> ISNULL(@Oc,0))                
                        --EXECUTE [dbo].[usp_Audit_Insert] ...

    sp_executesql可以实现返回多个输出参数,改进代码:

    View Code
    DECLARE @sql NVARCHAR(MAX),@FName NVARCHAR(50)
                    DECLARE @Nn DECIMAL(18,0),@On DECIMAL(18,0),@Nc DECIMAL(18,0),@Oc DECIMAL(18,0)    
                                    
                    SET @sql = N'SELECT @Nn = ['+ CONVERT(NVARCHAR(MAX),@I+'n],@Nc = ['+ CONVERT(NVARCHAR(MAX),@I+'c] FROM #inserted'
                    EXECUTE sp_executesql @sql,
                                          N'@Nn DECIMAL(18,0) OUTPUT,@Nc DECIMAL(18,0) OUTPUT',
                                          @Nn OUTPUT,@Nc OUTPUT;                                      
                                          
                    SET @sql = N'SELECT @On = ['+ CONVERT(NVARCHAR(MAX),@I+'n],@Oc = ['+ CONVERT(NVARCHAR(MAX),@I+'c] FROM #deleted'        
                    EXECUTE sp_executesql @sql,
                                          N'@On DECIMAL(18,0) OUTPUT,@Oc DECIMAL(18,0) OUTPUT',
                                          @On OUTPUT,@Oc OUTPUT;
                                              
                    IF (ISNULL(@Nn,0<> ISNULL(@On,0))
                        --EXECUTE [dbo].[usp_Audit_Insert] ...
                                    
                    IF (ISNULL(@Nc,0<> ISNULL(@Oc,0))
                        --EXECUTE [dbo].[usp_Audit_Insert] ...

     更多相关:

    http://www.cnblogs.com/insus/archive/2012/01/18/2325299.html

  • 相关阅读:
    设置ImageView圆角
    GitHub-引导页
    GitHub-特效
    TextView总汇
    【笔记8-Redis分布式锁】从0开始 独立完成企业级Java电商网站开发(服务端)
    【笔记7-部署发布】从0开始 独立完成企业级Java电商网站开发(服务端)
    【笔记6-支付及订单模块】从0开始 独立完成企业级Java电商网站开发(服务端)
    【笔记5-购物车及地址模块】从0开始 独立完成企业级Java电商网站开发(服务端)
    【笔记4-商品模块】从0开始 独立完成企业级Java电商网站开发(服务端)
    【笔记3-用户模块】从0开始 独立完成企业级Java电商网站开发(服务端)
  • 原文地址:https://www.cnblogs.com/insus/p/2325761.html
Copyright © 2011-2022 走看看