?
Solved

xp_executesql in function

Posted on 2003-03-14
3
Medium Priority
?
1,270 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
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
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

578 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