日期:2014-05-18  浏览次数:20442 次

使用什么参数,如何调用此函数?
CREATE FUNCTION f_DateADD(
@Date datetime,
@DateStr varchar(23)
) RETURNS datetime


想运行此函数,这样写为什么报错?应该如何使用?
f_DateADD('2001-1-01','2001-2-1')

------解决方案--------------------
SQL code

select dbo.f_DateADD('2001-1-01','2001-2-1')

------解决方案--------------------
引用别人的例子,LZ可以参考下如何使用函数!
create table tab1(CompanyPropertyID varchar(10),CompanyPropertyName varchar(100))
Insert into tab1
select '01','aaa'
union all select '02','bbb'
union all select '03','ccc'
 
create table tab2(SellID varchar(10),CompanyPropertyIDS varchar(100))
Insert into tab2
select 'A001','01;02;'
union all select 'A001','01'
union all select 'A001','01;03;'
union all select 'A001','01;02;03;'
 
select * from tab1
select * from tab2
 
--創建函數處理
create function dbo.fn_f(@s varchar(1000))
returns varchar(1000)
as
begin
declare @a varchar(1000)
declare @i int
set @a=''
set @i=1
while @i>0
begin
select @a=@a+CompanyPropertyName+';' from tab1 where CompanyPropertyID=substring(@s,@i,2)
set @i=charindex(';',@s,@i+3)-2
end
return(left(@a,len(@a)-1))
end
 
--刪除
drop table tab1
drop table tab2
drop function dbo.fn_f
 
--結果
select SellID,CompanyPropertyName=dbo.fn_f(CompanyPropertyIDS) from tab2
SellID CompanyPropertyName
---------------------------------------------
A001 aaa;bbb
A001 aaa
A001 aaa;ccc
A001 aaa;bbb;ccc
------解决方案--------------------
加上函数所有者dbo
dbo.f_DateADD
------解决方案--------------------
SQL code

select dbo.f_DateADD

------解决方案--------------------
select dbo.f_DateADD('2001-1-01','2001-2-1') 



我刚才试过了啊.没问题.

结果是:
4002-03-02 00:00:00.000

------解决方案--------------------
SQL code
select dbo.f_DateADD('2001-1-01','2001-2-1')

------解决方案--------------------
SQL code

完美的例子
CREATE  FUNCTION  f_DateADD(@Date  datetime, @DateStr varchar(23) ) 
RETURNS   datetime 
begin
declare @@Date datetime
select @@Date=datediff(year,@Date,convert(datetime,@DateStr))
return @@Date
end
go
select dbo.f_DateADD('2001-1-01','2001-2-1')
/*
-----------------------
1900-01-01 00:00:00.000
*/