D
Dephic
Unregistered / Unconfirmed
GUEST, unregistred user!
SQLSERVER里想写一个函数,函数的参数想传入一个字段,
然后在函数体对的SELECT语句里用上这个字段,该怎么办呢?
函数如下:问题是在SQLSERVER定义的函数里,不能使用EXEC命令,该怎么办啊?急!
ALTER FUNCTION [dbo].[return_c_field] (@cField varchar(10), @aa int,@bb int)
RETURNS varchar(1000) AS
BEGIN
EXEC('declare my_cursor cursor for select ' + @cField + ' from Temp_SpecialtySchedule where a= '
+ CAST(@aa AS Varchar(2)) + ' and b= ' + CAST(@bb AS VarChar(2)) )
open my_cursor;
declare @result as varchar(200);
select @result=''; --
declare @tmp_c_value as varchar(100);
while(@@fetch_status=0)
begin
select @tmp_c_value='';
fetch next from my_cursor into @tmp_c_value;
select @result=@result+@tmp_c_value;
end
close my_cursor;
deallocate my_cursor;
return @result
END
然后在函数体对的SELECT语句里用上这个字段,该怎么办呢?
函数如下:问题是在SQLSERVER定义的函数里,不能使用EXEC命令,该怎么办啊?急!
ALTER FUNCTION [dbo].[return_c_field] (@cField varchar(10), @aa int,@bb int)
RETURNS varchar(1000) AS
BEGIN
EXEC('declare my_cursor cursor for select ' + @cField + ' from Temp_SpecialtySchedule where a= '
+ CAST(@aa AS Varchar(2)) + ' and b= ' + CAST(@bb AS VarChar(2)) )
open my_cursor;
declare @result as varchar(200);
select @result=''; --
declare @tmp_c_value as varchar(100);
while(@@fetch_status=0)
begin
select @tmp_c_value='';
fetch next from my_cursor into @tmp_c_value;
select @result=@result+@tmp_c_value;
end
close my_cursor;
deallocate my_cursor;
return @result
END