sqlserver 存储过程中拼接sql语句 动态执行

该博客介绍了如何在SQLServer中创建存储过程`CustomerBalanceAccount_pro`,该过程根据输入参数动态拼接SQL查询语句,用于查询指定日期范围、客户名称和代码的客户余额账户信息。通过设置过滤条件,存储过程能够灵活地根据传入参数调整查询条件,并最终执行生成的SQL语句。

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

create proc  [dbo].[CustomerBalanceAccount_pro]--客户余额帐(统一客户名称)
    @beginDate varchar(6),
    @endDate varchar(6),
    @cCusName varchar(100),
    @cCusMnemCode varchar(100),
    @ccode_name varchar(20)
as  
    declare @cStr  nvarchar(1500) --执行语句
    declare @iwhere nvarchar(1500) --过滤条件
    set @iwhere = ' and 1=1 '
    if @beginDate = ''
    BEGIN
     set @beginDate = '190001'
    end 
    if @endDate = ''
    BEGIN
     set @endDate = DateName(year,GetDate())+ DateName(month,GetDate())
    end 
    
    --过滤条件 
if @cCusName != '' 
begin 
    set @iwhere = @iwhere +' and (exists (select ''x'' from Customer t where t.cCusName like  ''%' 
    set @iwhere 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值