stored procedures in SQL Server where conditions need to be judged for stitching

Source: Internet
Author: User

1  Use [Mct]2 GO3 /** * * * object:storedprocedure [dbo].    [P_counticcardtradinfo] Script date:05/20/2015 09:04:45 * * * * **/4 SETAnsi_nulls on5 GO6 SETQuoted_identifier on7 GO8 9 ALTER PROCEDURE [dbo].[P_counticcardtradinfo]Ten ( One  @bdate  datetime, A  @edate  datetime, -  @CusName varchar( -), -  @CardCode varchar( -), the  @BusinessType varchar( -), -  @UserCode varchar( -) - ) -  as + Declare @strSql varchar(Max) - Set @strSql='Select Recordtime,cardcode,cuscode,relname,boothcode,deposit,usercode,businesstype,businessvalue from # Temptabletradinfocount where 1=1'; +  A BEGIN at     --create temporary tables for saving data - Create Table#TempTableTradInfoCount - ( -  -Recordtimedatetime       not NULL, -Cardcodevarchar(Ten) not NULL, inCuscodevarchar( -) not NULL, -Relnamevarchar( -) not NULL, toBoothcodevarchar( -) not NULL, +Depositdecimal(Ten,2) not NULL, -Usercodevarchar( -) not NULL, theBusinesstypevarchar( -) not NULL, *Businessvaluevarchar( -) not NULL $ )Panax Notoginseng  - if(@bdate  is  not NULL) and(@bdate!="') the  Set @strSql= @strSql + 'and CONVERT (varchar (), recordtime,120) >=" "+CONVERT(varchar(Ten),@bdate, -)+" '" ; +   A if(@edate  is  not NULL) and(@edate!="') the  Set @strSql= @strSql + 'and CONVERT (varchar (), recordtime,120) <=" "+CONVERT(varchar(Ten),@edate, -)+" '" ; +   - if(@CusName  is  not NULL) and(@CusName!="') $  Set  @strSql= @strSql + 'and relname =" " + @CusName +" '"; $  - if(@CardCode  is  not NULL) and(@CardCode!="') -  Set  @strSql= @strSql + 'and Cardcode =" " + @CardCode+" '" ; the   - if(@BusinessType  is  not NULL) and(@BusinessType!="')Wuyi  Set  @strSql= @strSql + 'and Businesstype =" " + @BusinessType+" '" ; the   - if(@UserCode  is  not NULL) and(@UserCode!="') Wu  Set  @strSql= @strSql + 'and Usercode =" " + @UserCode +" '"; -   About  --print @strsql; $   - --Main card - Insert  into#TempTableTradInfoCount (Recordtime,cardcode,cuscode,relname,boothcode,deposit,usercode,businesstype, Businessvalue) - SelectEDITDATE,ICCARDNOCPU,KH_DM,KH_MC,"',0, Usecode,'Maincard','Main card'   fromKhdawhereIccardnocpu!="'  and(Usecode!='Y' orUsecode!='y') A  + --Secondary Card the Insert  into#TempTableTradInfoCount (Recordtime,cardcode,cuscode,relname,boothcode,deposit,usercode,businesstype, Businessvalue) - SelectEDITDATE,PARTCARDNO,KHDM,KHMC,"', Ysje,usercode,'Vicecard','Secondary Card'  fromIccardcopyrecord $  the --Temporary card regularization the Insert  into#TempTableTradInfoCount (Recordtime,cardcode,cuscode,relname,boothcode,deposit,usercode,businesstype, Businessvalue) the SelectPd. Recordtime,cus. Iccardnocpu,cus. Kh_dm,cus. KH_MC,"'Pd. Deposit,registration,'Temcard','Temporary card'  fromPositivedeposit PDInner JoinKhda cus onPd. Customercord=cus. KH_DM the  - --Reporting Loss in Insert  into#TempTableTradInfoCount (Recordtime,cardcode,cuscode,relname,boothcode,deposit,usercode,businesstype, Businessvalue) the SelectLOSSRQ,CARDCODE,KHDM,KHMC,"', Gsf,usercode,'Losscard','Reporting Loss'  fromIccardloss the  About --return card the Insert  into#TempTableTradInfoCount (Recordtime,cardcode,cuscode,relname,boothcode,deposit,usercode,businesstype, Businessvalue) the SelectEDITDATE,CARDCODE,KHDM,KHMC,"', Tkf,usercode,'Backcard','return card'  fromIccardreturn the  + exec(@strSql); -  the END

stored procedures in SQL Server where conditions need to be judged for stitching

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.