When run as a "User defined function" it errors:
Error:443: Invalid use of Execute within a function.
Suggestions?
Main Topics
Browse All TopicsHere is a UDF using a cursor:
==========================
Declare @var varchar(200)
Declare sql_cursor CURSOR FOR select @fieldname from @tablename where @keyname = @id
Open sql_cursor
Fetch next From sql_cursor into @var
while @@Fetch_Status=0
Begin
Set @string=@string + @var + ';'
Fetch next From sql_cursor into @var
End
Close sql_cursor
Deallocate sql_cursor
==========================
I want to be able to use variables when i declare the cursor via a select but am having difficulties. ie:
Declare sql_cursor CURSOR FOR select @fieldname from @tablename where @keyname = @id
I have also tried using the following to no avail.
Declare sql_cursor CURSOR FOR exec sp_executesql(select @fieldname from @tablename where @keyname = @id)
Thanks
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
yeah, forgot that minor detail ! Since dynamic SQL cannot be used in UDFs (and assuming that this HAS to be a UDF), only other thought I have is to use all (or all *valid*, to keep it sane) permutations of @fieldname, @tablename and @keyname e.g.
DECLARE @var varchar(200)
DECLARE @string varchar(8000)
DECLARE @sql_string varchar(8000)
IF @tablename = 'sysobjects' and @fieldname = 'name' and @keyname = 'id'
BEGIN
DECLARE sql_cursor CURSOR FOR SELECT name FROM sysobjects WHERE id = @id
END
ELSE IF @tablename = 'syscolumns' and @fieldname = 'name' and @keyname = 'id'
BEGIN
DECLARE sql_cursor CURSOR FOR SELECT name FROM syscolumns WHERE id = @id
END
ELSE
...
...
OPEN sql_cursor
FETCH NEXT FROM sql_cursor INTO @var
WHILE @@FETCH_STATUS = 0
BEGIN
SET @string = ISNULL(@string,'') + @var + ';'
FETCH NEXT FROM sql_cursor INTO @var
END
CLOSE sql_cursor
DEALLOCATE sql_cursor
The following is an example of how to get rid of the cursor which is inefficient and just use the straight select statement. It creates a table puts two phone numbers in it and then returns the phone numbers is a comma delimeted list.
CREATE TABLE Contacts
(
ContactID int IDENTITY PRIMARY KEY,
FirstName varchar(100),
LastName varchar(100)
)
CREATE TABLE PhoneNumbers
(
PhoneID int IDENTITY PRIMARY KEY,
ContactID int,
PhoneNumber varchar(50)
)
DECLARE @id int
INSERT INTO Contacts
VALUES ('John', 'Smith')
SELECT @id = @@IDENTITY
INSERT INTO PhoneNumbers
VALUES (@id, '111-222-3333')
INSERT INTO PhoneNumbers
VALUES (@id, '444-555-6666')
GO
/*
Code to create UDF for getting phone numbers
*/
CREATE FUNCTION GetPhones (@ContactID int)
RETURNS varchar(2000)
AS
BEGIN
DECLARE @phones varchar(2000)
SET @phones = ''
SELECT @phones =
CASE @phones
WHEN '' THEN ISNULL(PhoneNumber, '')
ELSE @phones + ', ' + ISNULL(PhoneNumber, '')
END
FROM PhoneNumbers
WHERE ContactID = @ContactID
RETURN(@phones)
END
GO
/*
Code to execute function from SELECT statement
*/
SELECT FirstName, LastName, dbo.GetPhones(ContactID)
From Contacts
greenrc:
This old question needs to be finalized -- accept an answer, split points, or get a refund. For information on your options, please click here-> http:/help/closing.jsp#1
EXPERTS:
Post your closing recommendations! No comment means you don't care.
Business Accounts
Answer for Membership
by: jchopdePosted on 2002-09-14 at 08:32:15ID: 7280655
Use --
--Dynamic parameters
DECLARE @fieldname varchar(256)
DECLARE @tablename varchar(256)
DECLARE @keyname varchar(256)
--change to varchar and handle quotes in dynamic sql if that is the case
DECLARE @id int
SET @fieldname = 'name'
SET @tablename = 'sysobjects'
SET @keyname = 'id'
SET @id = 1
--
DECLARE @var varchar(200)
DECLARE @string varchar(8000)
DECLARE @sql_string varchar(8000)
SET @sql_string = 'DECLARE sql_cursor cursor FOR SELECT ' +
@fieldname + ' FROM ' + @tablename + ' WHERE ' +
@keyname + ' = ' + CAST(@id AS varchar)
EXEC(@sql_string)
OPEN sql_cursor
FETCH NEXT FROM sql_cursor INTO @var
WHILE @@FETCH_STATUS = 0
BEGIN
SET @string = ISNULL(@string,'') + @var + ';'
FETCH NEXT FROM sql_cursor INTO @var
END
--Debug print, remove after testing
PRINT '****' + @string + '****'
CLOSE sql_cursor
DEALLOCATE sql_cursor