﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / SQL Server 2005 / SQL Server 2005 General Discussion  / Multi-statement Table_valued Function error / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Tue, 21 May 2013 15:15:35 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Multi-statement Table_valued Function error</title><link>http://www.sqlservercentral.com/Forums/Topic425121-149-1.aspx</link><description>I wish I knew enough about replication to help you out of the pickle you're in... Thanks for the feedback.</description><pubDate>Fri, 23 Nov 2007 16:19:48 GMT</pubDate><dc:creator>Jeff Moden</dc:creator></item><item><title>RE: Multi-statement Table_valued Function error</title><link>http://www.sqlservercentral.com/Forums/Topic425121-149-1.aspx</link><description>Hi JeffThanks for you reply.There is no other workaround for fullfill my function requirment. As my requirement is that table return sql statement base on some criteria and then bt executing sql statement will return some Ids. I can't use Store procedure for fullfill above requirement as i want use this function for Replication(for filter row)</description><pubDate>Thu, 22 Nov 2007 22:50:07 GMT</pubDate><dc:creator>patel mayur m</dc:creator></item><item><title>RE: Multi-statement Table_valued Function error</title><link>http://www.sqlservercentral.com/Forums/Topic425121-149-1.aspx</link><description>sp_ExecuteSQL is not an "extended stored procedure" or it would begin with "xp_".  Neither is any form of dynamic SQL.  I'm afraid that you're out of luck.In fact, if you check out Books Online, which says the following about UDF's, you'll see that you've violated several rules for UDF's ;)[quote]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. [/quote]</description><pubDate>Thu, 22 Nov 2007 21:33:29 GMT</pubDate><dc:creator>Jeff Moden</dc:creator></item><item><title>Multi-statement Table_valued Function error</title><link>http://www.sqlservercentral.com/Forums/Topic425121-149-1.aspx</link><description>Hello friendI have created Multi-statement Table_valued function which should return table. Function have dynamic cursor declared.Function is created sucessfully but got error when it executing.Error like [b]"[size="2"]Only functions and extended stored procedures can be executed from within a function."[/size][/b].Function code like below.[b]SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOALTER FUNCTION [dbo].[FetchAddress]()RETURNS @RetTable TABLE (adr_id uniqueidentifier)ASBEGIN	declare @query nvarchar(2000) 	set @query = (select query from Rules where mand=1 and station= 111)	[i][u]--where @query = Select adr_id from Address where mand = 1 and station = 111 and city = 'ahmedabad'[/u][/i]	DECLARE @STRSQL VARCHAR(2000)	SET @STRSQL =  'DECLARE ADDRESSDATA CURSOR READ_ONLY FOR ' + @query	exec sp_executesql @STRSQL	OPEN ADDRESSDATA	DECLARE @addressId uniqueidentifier	FETCH NEXT FROM ADDRESSDATA INTO @addressId		WHILE @@fetch_status = 0			BEGIN				INSERT INTO @RetTable VALues(@addressId)								FETCH NEXT FROM ADDRESSDATA INTO @addressId			END	CLOSE ADDRESSDATA	DEALLOCATE ADDRESSDATA	RETURN END[/b]I am executing above function by "Select * from [Test].[dbo].[FetchAddress]()"Thanks</description><pubDate>Thu, 22 Nov 2007 21:02:01 GMT</pubDate><dc:creator>patel mayur m</dc:creator></item></channel></rss>