Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Turn a procedure to a user define function

Posted on 2014-11-12
3
Medium Priority
?
155 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
  • 2
3 Comments
 
LVL 66

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 70

Accepted Solution

by:
Scott Pletcher earned 2000 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 70

Assisted Solution

by:Scott Pletcher
Scott Pletcher earned 2000 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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Ready to get certified? Check out some courses that help you prepare for third-party exams.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

971 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