Please start any new threads on our new site at We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Site Related Forums
 Article Discussion
 Why can't I use it in a User Defined Function?

Author  Topic 

Starting Member

3 Posts

Posted - 2001-12-17 : 13:10:57
I read the article (which was using BULK INSERT in a Stored Procedure), then I mimic the code to import a series of date name text file in a User Defined Function.

It will not work, and complain about my EXEC statement! Wouldn't UDF supposed to be the same beast with SP?

My code is followed...

CREATE FUNCTION fnLoadNetLog (@pStartDate smallDateTime, @pEndDate smallDateTime)
RETURNS @retCounts TABLE (
LogFile char(500),
Rec int

DECLARE @curDate smallDateTime
DECLARE @fname varchar(20)
DECLARE @sqlStmt varchar(500)

SET @curDate = @pStartDate
WHILE (@curDate <= @pEndDate) BEGIN
SET @fname = REPLACE(convert(char(10),@curDate,120),'-','')
SET @sqlStmt = 'BULK INSERT NetLog From ''C:\importdata\'+@fname+'.log'' WITH (FIELDTERMINATOR = '','', KEEPNULLS)'
EXEC (@sqlStmt)
INSERT INTO @retCounts VALUES(@sqlStmt, 1)
SET @curDate = dateadd( dd, 1, @curDate )


Posting Yak Master

218 Posts

Posted - 2001-12-17 : 14:14:33
From Books online:
The following statements are allowed in the body of a multi-statement function. Statements not in this list are not allowed in the body of a function:

Assignment statements.

Control-of-Flow statements.

DECLARE statements defining data variables and cursors that are local to the function.

SELECT statements containing select lists with expressions that assign values to variables that are local to the function.

Cursor operations referencing local cursors that are declared, opened, closed, and deallocated in the function. Only FETCH statements that assign values to local variables using the INTO clause are allowed; FETCH statements that return data to the client are not allowed.

INSERT, UPDATE, and DELETE statements modifying table variables local to the function.

EXECUTE statements calling an extended stored procedures. "

Go to Top of Page

- Advertisement -