Solved

Turn a procedure to a user define function

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

Expert Comment

by:Jim Horn
Comment Utility
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:
ScottPletcher earned 500 total points
Comment Utility
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:ScottPletcher
ScottPletcher earned 500 total points
Comment Utility
--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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
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.

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now