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.

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
Save Password
Forgot your Password?

 All Forums
 Site Related Forums
 Article Discussion
 Why can't I use it in a User Defined Function?
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Starting Member

3 Posts

Posted - 12/17/2001 :  13:10:57  Show Profile  Reply with Quote
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 - 12/17/2001 :  14:14:33  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2019 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.02 seconds. Powered By: Snitz Forums 2000