创建Funtion函数

春风十里耶耶耶 2013-05-02 11:16:46
以下代码,我想放到function中,怎么实现,在调用function时,不能够传递参数。
该Function作用,循环向表 "tmp_DeptTB"中插入数据,最后select,返回结果集,怎么搞.


declare @totalcount int,
@rownum int,
@id varchar(10),
@deptName varchar(100),
@sql varchar(max),
@guid varchar(100),
@temp varchar(10)
select @totalcount=count(1) from (select distinct t3.ID,t3.DeptName,t3.RootPath from PMProject t1
left join PMProjectSalesInfo t2 on t2.ProjectID=t1.ID
join SCDepartment t3 on t2.SalesInfoDeptID=t3.ID
where t1.ProjectStateID=3 )a
set @rownum=1
set @id=0
set @sql=''
select @guid= newid()
while @rownum <= @totalcount
begin
select top 1 @id=tb1.ID,@deptName=tb1.DeptName from ( select distinct t3.ID,t3.DeptName,t3.RootPath from PMProject t1
left join PMProjectSalesInfo t2 on t2.ProjectID=t1.ID
join SCDepartment t3 on t2.SalesInfoDeptID=t3.ID
where t1.ProjectStateID=3) tb1 where tb1.ID > @id order by tb1.ID
if( select PATINDEX('%14%',t1.RootPath) from SCDepartment t1 where ID= @id)>0
begin
if(select PATINDEX('%14,%',t2.RootPath) from SCDepartment t2 where ID= @id )>1
begin
insert into tmp_DeptTB(GUID,DeptID,ParentID,DeptName,RootPath) select @guid 'Guid',ID 'DeptID' ,ParentID,DeptName,RootPath from SCDepartment t1 where t1.ID=( select SUBSTRING(substring(t1.RootPath,10,100),0, PATINDEX('%,%',substring(t1.RootPath,10,100))) from SCDepartment t1 where ID=@id)
end
else
begin
insert into tmp_DeptTB(GUID,DeptID,ParentID,DeptName,RootPath) select @guid 'Guid',ID 'DeptID',ParentID,DeptName,RootPath from SCDepartment t3 where IsOn=1 and ID=@id
end
end
else
begin
insert into tmp_DeptTB(GUID,DeptID,ParentID,DeptName,RootPath) select @guid 'Guid',ID 'DeptID',ParentID,DeptName,RootPath from SCDepartment where ID=(select t1.ParentID From SCDepartment t1 where t1.ID=@id)
end
set @rownum=@rownum+1
end
select * From tmp_DeptTB
...全文
199 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
谁帮忙看下,把这段改成function可用的
daiyueqiang2045 2013-05-02
  • 打赏
  • 举报
回复
引用 6 楼 maco_wang 的回复:
看一下exec procname 结果是几列的,先创建一个临时表。 把exec的结果插入临时表就可以了。 参考:http://www.cnblogs.com/JohnXIe/archive/2008/04/24/1169722.html
确实
叶子 2013-05-02
  • 打赏
  • 举报
回复


Invalid use of side-effecting or time-dependent operator in 'INSERT' within a function.
黄_瓜 2013-05-02
  • 打赏
  • 举报
回复
CREATE FUNCTION ff( @totalcount int, @rownum int, @id varchar(10), @deptName varchar(100), @sql varchar(max), @guid varchar(100), @temp varchar(10) ) RETURNS @tmp_DeptTB TABLE ( /* 表结构 */ ) AS BEGIN /*你的语句*/ RETURN END 另外function里面不能使用 newid
chuifengde 2013-05-02
  • 打赏
  • 举报
回复
把表tmp_DeptTB定义成表变量
叶子 2013-05-02
  • 打赏
  • 举报
回复
看一下exec procname 结果是几列的,先创建一个临时表。 把exec的结果插入临时表就可以了。 参考:http://www.cnblogs.com/JohnXIe/archive/2008/04/24/1169722.html
  • 打赏
  • 举报
回复
引用 4 楼 maco_wang 的回复:

CREATE PROC procname
AS 
    BEGIN
	 declare @totalcount int,
			 @rownum int,
			 @id varchar(10),
			 @deptName varchar(100),
			 @sql varchar(max),
			 @guid varchar(100),
			 @temp varchar(10)
	select @totalcount=count(1) from (select distinct t3.ID,t3.DeptName,t3.RootPath     from PMProject t1 
	left join PMProjectSalesInfo  t2 on t2.ProjectID=t1.ID 
	join SCDepartment t3 on t2.SalesInfoDeptID=t3.ID  
	where t1.ProjectStateID=3 )a
	set @rownum=1
	set @id=0
	set @sql=''
	select @guid= newid()    
	while @rownum <= @totalcount
		begin
			 select top 1 @id=tb1.ID,@deptName=tb1.DeptName from ( select distinct t3.ID,t3.DeptName,t3.RootPath     from PMProject t1 
			 left join PMProjectSalesInfo  t2 on t2.ProjectID=t1.ID 
			 join SCDepartment t3 on t2.SalesInfoDeptID=t3.ID  
			 where t1.ProjectStateID=3) tb1 where tb1.ID > @id order by tb1.ID 
			 if( select  PATINDEX('%14%',t1.RootPath) from SCDepartment t1 where ID= @id)>0
			 begin
				if(select  PATINDEX('%14,%',t2.RootPath) from SCDepartment t2 where ID= @id )>1
					begin
					  insert into tmp_DeptTB(GUID,DeptID,ParentID,DeptName,RootPath) select @guid 'Guid',ID 'DeptID' ,ParentID,DeptName,RootPath  from SCDepartment t1 where t1.ID=( select SUBSTRING(substring(t1.RootPath,10,100),0, PATINDEX('%,%',substring(t1.RootPath,10,100))) from SCDepartment t1 where ID=@id)
					end
				else
					begin  
					   insert into tmp_DeptTB(GUID,DeptID,ParentID,DeptName,RootPath) select @guid 'Guid',ID 'DeptID',ParentID,DeptName,RootPath from SCDepartment t3 where IsOn=1 and ID=@id
					end
			 end
			 else
			 begin
				 insert into tmp_DeptTB(GUID,DeptID,ParentID,DeptName,RootPath) select @guid 'Guid',ID 'DeptID',ParentID,DeptName,RootPath   from SCDepartment where ID=(select t1.ParentID From SCDepartment t1 where t1.ID=@id)
			 end
			set @rownum=@rownum+1
		end
		select * From tmp_DeptTB
    END
改成存储过程是可以的,但是我想将查询的结果集,当作一张表来和其他的表使用jion查询,使用存储过程不能满足啊,就是类似这样

select * From SCDepartment t1 
left join ('结果集' ) t2 on t2.ID=t1.ID

select * From SCDepartment t1 
left join ( exec procname) t2 on t2.ID=t1.ID
叶子 2013-05-02
  • 打赏
  • 举报
回复

CREATE PROC procname
AS 
    BEGIN
	 declare @totalcount int,
			 @rownum int,
			 @id varchar(10),
			 @deptName varchar(100),
			 @sql varchar(max),
			 @guid varchar(100),
			 @temp varchar(10)
	select @totalcount=count(1) from (select distinct t3.ID,t3.DeptName,t3.RootPath     from PMProject t1 
	left join PMProjectSalesInfo  t2 on t2.ProjectID=t1.ID 
	join SCDepartment t3 on t2.SalesInfoDeptID=t3.ID  
	where t1.ProjectStateID=3 )a
	set @rownum=1
	set @id=0
	set @sql=''
	select @guid= newid()    
	while @rownum <= @totalcount
		begin
			 select top 1 @id=tb1.ID,@deptName=tb1.DeptName from ( select distinct t3.ID,t3.DeptName,t3.RootPath     from PMProject t1 
			 left join PMProjectSalesInfo  t2 on t2.ProjectID=t1.ID 
			 join SCDepartment t3 on t2.SalesInfoDeptID=t3.ID  
			 where t1.ProjectStateID=3) tb1 where tb1.ID > @id order by tb1.ID 
			 if( select  PATINDEX('%14%',t1.RootPath) from SCDepartment t1 where ID= @id)>0
			 begin
				if(select  PATINDEX('%14,%',t2.RootPath) from SCDepartment t2 where ID= @id )>1
					begin
					  insert into tmp_DeptTB(GUID,DeptID,ParentID,DeptName,RootPath) select @guid 'Guid',ID 'DeptID' ,ParentID,DeptName,RootPath  from SCDepartment t1 where t1.ID=( select SUBSTRING(substring(t1.RootPath,10,100),0, PATINDEX('%,%',substring(t1.RootPath,10,100))) from SCDepartment t1 where ID=@id)
					end
				else
					begin  
					   insert into tmp_DeptTB(GUID,DeptID,ParentID,DeptName,RootPath) select @guid 'Guid',ID 'DeptID',ParentID,DeptName,RootPath from SCDepartment t3 where IsOn=1 and ID=@id
					end
			 end
			 else
			 begin
				 insert into tmp_DeptTB(GUID,DeptID,ParentID,DeptName,RootPath) select @guid 'Guid',ID 'DeptID',ParentID,DeptName,RootPath   from SCDepartment where ID=(select t1.ParentID From SCDepartment t1 where t1.ID=@id)
			 end
			set @rownum=@rownum+1
		end
		select * From tmp_DeptTB
    END
叶子 2013-05-02
  • 打赏
  • 举报
回复
数据库的function不行的,在函数中不能进行表的插入,更新,删除的操作。 可以改成存储过程。

27,580

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧