SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[GetDatas]
(
@Year int = NULL
,@Month int = NULL
,@IP varchar(20) = '%'
)
AS
BEGIN
SET NOCOUNT ON;
DECLARE @sql nvarchar (4000)
SELECT @sql = 'SELECT * FROM InternetLog WHERE s-ip LIKE ''@IP'''
IF @Year IS NULL
SELECT @sql = @sql + ' AND YEAR(logdate) = YEAR(GETDATE())'
ELSE
SELECT @sql = @sql + ' AND YEAR(logdate) = @Year'
IF @Month IS NULL
SELECT @sql = @sql + ' AND MONTH(logdate) = MONTH(GETDATE())'
ELSE
SELECT @sql = @sql + ' AND MONTH(logdate) = @Month'
SELECT @sql = @sql + ' ORDER BY logdate ASC'
Exec @sql
END
|