SQLSERVER 根据传入的参数拼接sql语句字符串,反馈结果集

ALTER PROCEDURE [dbo].[usp_visit_detail]
(@siteid BIGINT,
 @Startime VARCHAR(15),
 @Endtime  VARCHAR(15),
 @Aflag  INT = 10,                                    --初始值为10,非10,则表示有传入参数
 @Area   VARCHAR(20) = ‘N‘,               -- --初始值为N,非N,则表示有传入参数
 @Nflag  INT = 10,                                -初始值为10,非10,则表示有传入参数
 @VALUE1 varchar(20) = ‘N‘              --初始值为N,非N,则表示有传入参数


)
AS
BEGIN
 DECLARE @AFlag0      varchar(50),
          @NFlag0      varchar(50), 
          --@Sqlstr0     varchar(600),
          @Sqlstr      varchar(2000) 
          
  IF @AFlag = 10 
    SET @AFlag0 = ‘‘
  ELSE 
    SET @AFlag0 = ‘ AND AFlag=‘ + CAST(@AFlag AS VARCHAR) 


  IF @Area = ‘N‘ 
    SET @Area = ‘‘
  ELSE 
    SET @Area = ‘ AND province=‘ + ‘‘‘‘ + @Area + ‘‘‘‘
        
  IF @NFlag = 10 
    SET @NFlag0 = ‘‘
  ELSE 
    SET @NFlag0 = ‘ AND NFlag=‘ + CAST(@NFlag AS VARCHAR)                       

  IF @Value1 = ‘N‘ 
    SET @Value1 = ‘‘
  ELSE 
    SET @Value1 = ‘ AND value1=‘ + ‘‘‘‘ + @Value1 + ‘‘‘‘   
   
   SET @Sqlstr =  ‘SELECT     a.Intime, c.Visitornm, c.Vcount, (CASE WHEN a.value2 = ‘+‘‘‘‘+‘‘‘‘+‘ THEN a.value1 + ‘+‘‘‘‘+‘‘‘‘+‘ WHEN a.value2 != ‘+‘‘‘‘+‘‘‘‘+‘ THEN a.value1 +‘+ ‘‘‘‘+‘-‘+‘‘‘‘+‘ + a.value2 END) AS source0, 
   DATEDIFF(ms,a.Intime, a.Outtime) AS vtime, a.Pages, a.Rev1, dbo.GET_URL(a.SiteID, a.InURLID) AS inrul, dbo.GET_URL(a.SiteID, a.OutURLID) AS outrul
FROM dbo.F_Pages_T AS a INNER JOIN  (SELECT     VisitorID, MAX(ID) AS id FROM dbo.F_Pages_T WHERE SiteID = ‘+‘‘‘‘+CONVERT(VARCHAR,@siteid)+‘‘‘‘+ ‘AND day0>= ‘+@startime+‘ AND day0<= ‘ +@endtime+@aflag0
+@area+   @Nflag0+ @Value1   +‘ GROUP BY VisitorID) AS b ON a.ID = b.id LEFT OUTER JOIN dbo.F_Visit AS c ON a.VisitorID = c.VisitorID‘
                      
   PRINT @sqlstr
   EXEC (@sqlstr)
END

SQLSERVER 根据传入的参数拼接sql语句字符串,反馈结果集,布布扣,bubuko.com

SQLSERVER 根据传入的参数拼接sql语句字符串,反馈结果集

上一篇:sqlserver 大数据量的insert、delete操作优化


下一篇:sqlserver 取月初月末的时间