SQL编写自定义函数

-- 通过一个子级ID 返回一级分类名称
alter function calcclass
(@dclassid as int)
returns varchar(50)
as
begin
-- 通过一个子级ID 返回一级分类名称
-- 返回ID 和父级分类ID 直到父级分类ID=0
declare @cid int
declare @cfatherid int
set @cid=@dclassid
while (@cid>0)
select @cfatherid=cid,@cid=cfatherid from trd_archivesclass where cid=@cid
return (select cname from trd_archivesclass where cid=@cfatherid)
end

如果需要在 Sql Server 中批量插入日期,可采用以下语句:

DECLARE @i int
SET @i = 0
WHILE @i < 365
BEGIN
 INSERT INTO table1
( Day1 ,Flag2 ,AddTime3 )
 VALUES  (  DATEADD(DAY,@i,'2015-1-1') ,1,GETDATE() ) 
 SET @i = @i + 1
END 

 

 

posted on 2014-04-11 10:37  z5337  阅读(964)  评论(0编辑  收藏  举报