Solved

Turn a procedure to a user define function

Posted on 2014-11-12
3
148 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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

756 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