首頁 >php教程 >PHP开发 >SQL預存程序中SQL語句拼接

SQL預存程序中SQL語句拼接

高洛峰
高洛峰原創
2016-12-14 15:22:591747瀏覽

set ANSI_NULLS ON

set QUOTED_IDENTIFIER ON

go

-- ================================== ===========

-- Author:wgh

-- Create date: 2012-06-07

-- Description: IP存取預統計

-- ====== =======================================

CREATE PROCEDURE [dbo].[PROC_HB_PreStatByIP]

    @BeginTime varchar(50),

    @EndTime varchar(50),

    @遠'0',

    @IpAddress varchar(20)

AS

BEGIN

    declare @sql1 nvarchar(2000)

    (13),AddTime,120) AS ' '日期''

                  ,ChannelID AS ''推廣管道''

                      ,ViewCount AS ''瀏覽次數''

             FROM dbo.HB_AdPreStatIP WHERE AddTime>='''+@BeginTime+''' AND AddTime

               SET @sql1 = @sql1 + ' AND ChannelID='''+@ChannelID+''''

        END

        IF      SET @sql1 =@sql1 +' AND IPAddress='''+@ IpAddress+''''

        END

        SET @sql1 =@sql1+'    BEGIN

SET @sql1='SELECT CONVERT(varchar(10),AddTime,120) AS ''日期''

                          ,ChannelID       ,IPAddress AS ''IP位址''

                                       ,SUM(ClickCount)  AS ''點選次數'' 

                    FROM dbo.HB_AdPreStatIP WHERE AddTime  IF @ChannelID > '0'

BEGIN

           SET @sql1 =@sql1 + ' AND ChannelID='''+@ChannelID+''''

    

        BEGIN

          SET @sql1 =@sql1 +' AND IPAddress='''+@IpAddress+''''

        END

SET @sql1 =@sql1 + 'GROUP BY CONVERT(varchar(10),AddTime,120),ChannelChannelID,IP

    END

   EXEC sp_executesql @sql1 

END

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn