日期:2014-05-17 浏览次数:20590 次
declare @sql nvarchar(max)
set @sql='SELECT * FROM tab where city in('+@city+')'
exec(@sql)
ALTER Function [dbo].[GetSplitedValues](@Ids nvarchar(max))
returns @Table Table(Id nvarchar(5000))
As
Begin
Declare @ind int
Declare @Start int
Set @Ids=@Ids+','
Set @Start=1
Set @ind =charindex(',',@Ids)
While(@ind>0)
Begin
Declare @SubId nvarchar(100)
Set @SubId=Substring(@Ids,@Start,(@ind-1))
if(@SubId<>'' and @SubId Is Not Null)
Begin
insert into @Table(Id) values(@SubId)
End
Set @Ids =Substring(@Ids,@ind+1,Len(@Ids))
SET @ind = charindex(',',@Ids )
End
Return
END
ALTER PROCEDURE [dbo].[Get_Month]
@city varchar(5000) --地市
AS
BEGIN
SET NOCOUNT ON;
SELECT * FROM tab where city in (select * from GetSplitedValues( @city))
if object_id(N'tempdb..#tab',N'U') is not null --判断临时表是否存在
drop table #tab --若存在就删除重新创建
create table #tab
(
日期 varchar(7)
)
set @sql_y='SELECT CONVERT(varchar(7), 日期, 120)as 日期 FROM tab where city in('+@city+')'
insert into #tab
exec(@sql_y)
declare abc cursor select 日期 from #tab
open abc
--这是定义表变量的语句
declare @tab table
(
日期 varchar(7)