日期:2014-05-16  浏览次数:20452 次

存储过程的拼接写法 -- 转载
USE [a]
GO
/****** Object:  StoredProcedure [a].[Processname]    Script Date: 02/24/2011 11:49:55 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [a].[Processname]
@FromDateTime varchar(200),
@ToDateTime varchar(200),
@CountryCode varchar(200),
@Version varchar(200)
AS
declare @sql varchar(1000)
set @sql='select AccessDateTime,IpAddress,Area,AreaDetail as description,City,Version from a.TableName where AccessDateTime
 between ''' + @FromDateTime+''' and '''+ @ToDateTime+''''
BEGIN
	if(@CountryCode='')
	begin
		set @sql=@sql
		if(@Version='')
			begin
				set @sql = @sql +' order by id desc'
			end
		else
			begin
				set @sql = @sql + ' and  Version='''+@Version +'''  order by id desc'
			end
	end
	else 
	begin
		set @sql=@sql
		if(@Version='')	
			begin
				set @sql=@sql +' and  Area='''+@CountryCode+''' order by id desc'
			end	
		else
			begin
				set @sql=@sql +' and Area='''+@CountryCode+''' and  Version='''+ @Version +''' order by id desc'
			end
	end	
--print @sql	
exec(@sql)	
END