2024/10/23
88 阅读

create proc p_textone

(

    @t_tNumber       int      ,

    @t_trId        varchar(20) ,

    @t_eDateBegin    datetime   ,

    @t_eDateEnd      datetime

)

as

begin

    

       set nocount on;

    

      DECLARE @Parm         NVARCHAR(MAX) = N'',

              @commandsqlSTR   NVARCHAR(MAX) = N''


        SET @commandsqlSTR = 'SELECT [id]

                                  ,[tNumber]

                                  ,[tId]

                                  ,[tDate]

                                  ,[Cstr]

                            FROM [dbo].[Sder] 

                            where 1=1 '

        

        IF(@t_tNumber IS NOT NULL)

            SET @commandsqlSTR = CONCAT(@commandsqlSTR,' AND tNumber= @t_tNumber')


        IF(@t_trId IS NOT NULL)

            SET @commandsqlSTR = CONCAT(@commandsqlSTR,' AND tId= @t_trId')


        IF(@t_eDateBegin IS NOT NULL)

            SET @commandsqlSTR = CONCAT(@commandsqlSTR,' AND tDate>=@t_eDateBegin ')


        IF(@t_eDateEnd IS NOT NULL)

            SET @commandsqlSTR = CONCAT(@commandsqlSTR,' AND tDate<=@t_eDateEnd ')


        SET @Parm= '@t_tNumber        int,

                    @t_trId        varchar(20),

                    @t_eDateBegin    datetime,

                    @t_eDateEnd        datetime '



        PRINT @commandsqlSTR

        EXEC sp_executesql @commandsqlSTR,@Parm,

                            @t_tNumber       =    @t_tNumber,

                            @t_trId        =    @t_trId,

                            @t_eDateBegin    =    @t_eDateBegin,

                            @t_eDateEnd      =    @t_eDateEnd 

        

end