Solved

Turn a procedure to a user define function

Posted on 2014-11-12
3
149 Views
Last Modified: 2014-12-05
HI there how can I turn this procedure to a user define function?
DECLARE @ReturnCode int

declare @cmd sysname
declare @NumberKey sysname
set @NumberKey = 'B07-100001'
set @cmd = 'dir "E:\Adept Technologies\Documents\Building\'+@NumberKey+'\'+@NumberKey+'_Expired*.doc" /b'

exec @ReturnCode = master..xp_cmdshell @cmd, no_output

-- If @ReturnCode is 0, file exists
-- If @ReturnCode is 1, file does NOT exist
PRINT @ReturnCode

Open in new window

0
Comment
Question by:COHFL
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 65

Expert Comment

by:Jim Horn
ID: 40438035
User-defined Functions, 'Limitations and Restrictions' section, fifth bullet point, UDF's cannot call another stored procedure.

So, it appears the answer is 'You can't do that' if the requirements are to create a UDF with a call to xp_cmdshell
0
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 500 total points
ID: 40438188
XPs are an exception.  You should be fine:

CREATE FUNCTION Check_If_File_Exists (
    @NumberKey sysname
)
RETURNS int
AS
BEGIN
DECLARE @cmd nvarchar(4000)
DECLARE @return_code int
SET @cmd = 'dir /b "E:\Adept Technologies\Documents\Building\'+@NumberKey+'\'+@NumberKey+'_Expired*.doc"'
EXEC @return_code = master.sys.xp_cmdshell @cmd, NO_OUTPUT
RETURN @return_code
END --FUNCTION
0
 
LVL 69

Assisted Solution

by:Scott Pletcher
Scott Pletcher earned 500 total points
ID: 40438622
--Although personally I would make it generic enough to work for any file; it could
--still handle your current case by default:


CREATE FUNCTION Check_If_File_Exists (
     @NumberKey sysname = NULL,
     @FileName sysname = 'E:\Adept Technologies\Documents\Building\$NumberKey$\$NumberKey$_Expired*.doc'
 )
RETURNS int
AS
BEGIN
DECLARE @cmd nvarchar(4000)
DECLARE @return_code int
SET @cmd = 'dir /b "' + REPLACE(@FileName, '$NumberKey$', ISNULL(@NumberKey, '')) + '"'
EXEC @return_code = master.sys.xp_cmdshell @cmd, NO_OUTPUT
RETURN @return_code
END --FUNCTION
GO
0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question