zoukankan      html  css  js  c++  java
  • 动态SQL中返回数值

    最近在做一个paypal抓取数据的程序,由于所有字段和paypal之间存在对应映射的关系,所以所有的sql语句必须得拼接传到存储过程里去执行,

    ALTER proc [dbo].[sp_common_paypal_AddInfo]
    (
     @paypalsql varchar(max),--不包含用户表的paypalsql语句
     @paypalusersql varchar(max),--paypal用户表的sql语句
     @ebaysql varchar(max),--不包含用户表的ebaysql语句
     @ebayusersql varchar(max),--ebay的用户表sql语句
     @paypaluserwhere varchar(max),--paypal用户表查询ID语句
     @ebayuserwhere varchar(max),--ebay用户表查询ID语句
     @websql varchar(max),--web除去用户表的sql语句
     @webusersql varchar(max),--web用户表的sql语句
     @webwhere varchar(max),--web用户表where之后的sql语句
     @ebaystockflag varchar(10),--ebay订单号生成规则
     @webstockflag varchar(10)--web订单号生成规则
    )
    as
    set xact_abort on
    begin transaction mytrans
    begin try
    declare @uid int--根据语句查找用户ID
    declare @execsql varchar(max)
    declare @ebayuid int--根据语句查找用户ID
    declare @execebaysql nvarchar(max)--用sp_executesql 字段类型必须是nvarchar
    declare @sql nvarchar(max)--用sp_executesql 字段类型必须是nvarchar
    set @sql='select @a=ID from  tb_TransactionCustomer where '+ convert(varchar(8000),@paypaluserwhere)
    exec   sp_executesql   @sql,N'@a int output',@uid   output  
    set   @uid =ISNULL(@uid,0)--如果不这样判断 获取的值可能为null用len()获取不到长度

    --存在paypal用户id
    if(@uid>0)
    begin
    set @execsql=@paypalsql-- 存在用户信息
    set @execsql= REPLACE(@execsql,'@uid',''+convert(varchar,@uid)+'')
    end
    else
    begin
    set @execsql=@paypalusersql+@paypalsql --不存在用户信息
    end

    if(LEN(@websql)>0)--执行web语句
    begin
    exec sp_common_WebSiteorder_AddInfo @websql, @webusersql, @webwhere ,@webstockflag
    end
    if(LEN(@ebaysql)>0)--执行ebay语句
    begin
    --exec sp_common_Ebay_AddInfo @ebaysql, @ebayusersql, @ebayuserwhere ,@ebaystockflag
    SELECT * FROM tb_EbayOrder WITH (TABLOCKX)
    SELECT *  FROM tb_EbayOrderList WITH (TABLOCKX) 
    SELECT * FROM tb_EbayOrderUserInfo WITH (TABLOCKX)
    set @sql='select @b=ID from  tb_EbayOrderUserInfo where '+ convert(varchar(8000),@ebayuserwhere)
    exec   sp_executesql   @sql,N'@b int output',@ebayuid   output  
    set   @ebayuid =ISNULL(@ebayuid,0)
    if(@ebayuid>0)
    begin
    set @execebaysql=@ebaysql--存在ebayuid
    set @execebaysql= REPLACE(@execebaysql,'@ebayuid',''+convert(varchar,@ebayuid)+'')--必须替换 否则会报错误说必须声明标量变量
    end
    else
    begin
    set @execebaysql=@ebayusersql+@ebaysql --不存在ebayuid
    end
    set @execebaysql= REPLACE(@execebaysql,'@00',dbo.GetOrderNum(@ebaystockflag))--调用函数替换订单编号
    exec (@execebaysql)

    end
    exec(@execsql)

    end try
    begin catch
    if(@@TRANCOUNT>0)
    rollback transaction mytrans

    end catch
    if(@@TRANCOUNT>0)
    begin
     commit transaction mytrans
     
    end
    else begin
    rollback transaction mytrans

     end


     

  • 相关阅读:
    【LSGDOJ 1408】邮局
    中间件小姐姐直播“带货”——阿里程序员必知的插件
    ChaosBlade 发布对 C++ 应用混沌实验的支持
    来自 Spring Cloud 官方的消息,Spring Cloud Alibaba 即将毕业
    大合集 | 9 场 Dubbo Meetup 回顾视频和PPT下载
    Future Maker | 领跑亚太 进击的阿里云数据库
    更简单易用的数据仓库,阿里云重磅推出分析型数据库3.0版
    AnalyticDB for MySQL 3.0 技术架构解析
    阿里云 EMAS HTTPDNS 联合函数计算重磅推出 SDNS 服务,三大能力获得突破
    新一代互联网传输协议QUIC浅析
  • 原文地址:https://www.cnblogs.com/zhang9418hn/p/2253533.html
Copyright © 2011-2022 走看看