Solved

Turn a procedure to a user define function

Posted on 2014-11-12
3
146 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
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:
ScottPletcher 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:ScottPletcher
ScottPletcher 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MS SQL - Rotating Values in SQL 9 50
SQL Server Deadlocks 12 49
SQL Select Statement 2 21
Azure SQL Insert not working suddenly 11 21
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

910 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

17 Experts available now in Live!

Get 1:1 Help Now