?
Solved

xp_executesql in function

Posted on 2003-03-14
3
Medium Priority
?
1,265 Views
Last Modified: 2012-05-04
intro:
i try to write a function for generating new PK values

function:
CREATE function dbo.sysNewId (@tabName nvarchar(4000))
returns int
AS
begin
declare @text nvarchar (4000), @newId int
set @text='select @newId= isnull( min( id), 0 )+1 from '+ @tabName+ ' where id+1 not in ( select id from '+ @tabName+ ')'
execute master.dbo.sp_executesql @text, N'@newId int out', @newId out
return (@newId)
end

query:
    select dbo.sysNewId ( 'sysclass')

error
Server: Msg 557, Level 16, State 2, Procedure ssysNewId, Line 11
Only functions and extended stored procedures can be executed from within a function.

question:
  What is the meaning of error, if  sp_executesql is extended stored procedure?
0
Comment
Question by:ill
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 4

Accepted Solution

by:
chaos_hooi earned 200 total points
ID: 8134823
sp_executesql is a system stored procedure, NOT a extended stored procedure...
0
 
LVL 23

Expert Comment

by:adathelad
ID: 8134883
Functions cannot execute stored procedures or system stored procedures (sp_* basically). They can only call other functions or extended stored procedures (xp_*).
If you have to use dynamix SQL like you are doing, you need to do it inside a stored procedure as opposed to a function.
0
 
LVL 12

Author Comment

by:ill
ID: 8134929
thx,
EM confused me with its sp grouping.
i just wanted to use output in a query without assign it to variable.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
I have a large data set and a SSIS package. How can I load this file in multi threading?
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Suggested Courses

777 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