Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Write a function within a stored procedure
Message
From
07/03/2008 14:17:21
Takashi Koyama
National Indemnity Company
Omaha, Nebraska, United States
 
General information
Forum:
Microsoft SQL Server
Category:
Stored procedures, Triggers, UDFs
Environment versions
SQL Server:
SQL Server 2005
Miscellaneous
Thread ID:
01299679
Message ID:
01299841
Views:
9
-- This script accepts a user's input for @cServerFileName,
--   @cLocalFilePath, @cMKDirLocal, to update the softwareupdate table.
-- This script mocks the array which does not exist in T-SQL to allow
--   multiple update or insert.
-- This is made possible by a mocked array and a while loop

DECLARE @nDownloadOrder as int,
        @cFileName varchar(50),
        @cLocalFilePath  varchar(50),
	@cMKDirLocal varchar(50),
        @aryFileNames varchar(200),
        @iLength int, @iStart int, @iEnd int, @iNumOfItem int

-- Begin: User should not touch the following --
   SET @iStart = 1
   SET @nDownloadOrder = 1
   SET @aryFileNames = ','
   SET @iNumOfItem = 0
-- End: User should not touch the following --


-- Begin: User must define the following ----
   SET @cLocalFilePath = '\'
   SET @cMKDirLocal = ''

   -- Begin: Set File --
   SET @cServerFileName = 'file1.dll'   

     -- Begin:  User should not change the following ----
     SET @aryServerFileNames = @aryServerFileNames + @cServerFileName + ',' 
     SET @iNumOfItem = @iNumOfItem + 1
     -- End:  User should not change the following ----
   -- End: Set File --

   -- Begin: Set File --
   SET @cServerFileName = 'file2.xml'

     -- Begin:  User should not change the following ----
     SET @aryServerFileNames = @aryServerFileNames + @cServerFileName + ',' 
     SET @iNumOfItem = @iNumOfItem + 1
     -- End:  User should not change the following ----
   -- End: Set File --

   -- Begin: Set File --
   SET @cServerFileName = 'file3.exe'

     -- Begin:  User should not change the following ----
     SET @aryServerFileNames = @aryServerFileNames + @cServerFileName + ',' 
     SET @iNumOfItem = @iNumOfItem + 1
     -- End:  User should not change the following ----
   -- End: Set File --
-- End: User must define the following ----

WHILE @nDownloadOrder <= @iNumOfItem
   BEGIN
      SET @iStart = (SELECT CHARINDEX( ',', @aryFileNames, @iStart ) ) + 1
      SET @iEnd = (SELECT CHARINDEX( ',', @aryFileNames, @iStart ) )
      SET @iLength = @iEnd - @iStart

      SET @cFileName = SUBSTRING(@aryFileNames, @iStart, @iLength)

      -- The following updates the <Table>   --
     UpdateTable @nDownloadOrder, @cFileName,    @cLocalFilePath, @cMKDirLocal

      SET @nDownloadOrder = @nDownloadOrder + 1      
   END
********
The above (some modification for public view) is what I did.
Offcourse, I did not use the portion

UpdateTable @nDownloadOrder, @cFileName, @cLocalFilePath, @cMKDirLocal

I created a stored procedure and executed. But if I could write a void funciton UpdateTable within this script, it would have been nice.

I don't care whether SQL sever do not have void funciton or not.
I find it funny from 3000 ft view. Since Microsoft created C# and VB.NET whick allows users to create a value returning and void functions, but these do not exist in T-SQL which is also Microsoft product.
Microsoft strives to be user friendly which even puts VB6 like debugging mechanism in VB.NET.
Knowing Microsoft's charactoristic, I don't see why T-SQL limits its capability including forcing a user to create value returning function.
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform