I provided sample user defined function,
create function fn_test(@orgname varchar(200),@Renname varchar(200),
@Tempname varchar(200)) returns int as
begin
declare @cmdsql varchar(250)
declare @retval int
set @cmdsql = 'copy ' + @orgname + ' ' + @renname
exec @retval = exec master..xp_cmdshell @cmdsql
if (@retval = 0) -----------------111
begin
set @cmdsql = 'del ' + @orgname
exec @retval = exec master..xp_cmdshell @cmdsql
set @cmdsql = 'copy ' + @renname + '+' + @tempname + ' ' + @orgname
exec @retval = exec master..xp_cmdshell @cmdsql
end
else
begin
set @cmdsql = 'copy ' + @tempname + ' ' + @orgname
exec @retval = exec master..xp_cmdshell @cmdsql
end
return 0
end
if orginal file name not present in the directory, i will copy the content of temporary file to orginal file, otherwise i copy org. file to temp. file, delete the org. file, append 2 files and copy to original file. I will delete the temporary files created.
I am getting return value is always 1(See the code,in the place marked, -------111). If i copy the above statements in query analyser it is returning 0 if success, if i added to the function , it not works. What's the problem in the code.
create function fn_test(@orgname varchar(200),@Renname varchar(200),
@Tempname varchar(200)) returns int as
begin
declare @cmdsql varchar(250)
declare @retval int
set @cmdsql = 'copy ' + @orgname + ' ' + @renname
exec @retval = exec master..xp_cmdshell @cmdsql
if (@retval = 0) -----------------111
begin
set @cmdsql = 'del ' + @orgname
exec @retval = exec master..xp_cmdshell @cmdsql
set @cmdsql = 'copy ' + @renname + '+' + @tempname + ' ' + @orgname
exec @retval = exec master..xp_cmdshell @cmdsql
end
else
begin
set @cmdsql = 'copy ' + @tempname + ' ' + @orgname
exec @retval = exec master..xp_cmdshell @cmdsql
end
return 0
end
if orginal file name not present in the directory, i will copy the content of temporary file to orginal file, otherwise i copy org. file to temp. file, delete the org. file, append 2 files and copy to original file. I will delete the temporary files created.
I am getting return value is always 1(See the code,in the place marked, -------111). If i copy the above statements in query analyser it is returning 0 if success, if i added to the function , it not works. What's the problem in the code.