Solved

Change Function to dynamic SQL

Posted on 2008-10-30
3
963 Views
Last Modified: 2010-05-19
I need to replace the section where it reads FROM MYDATABASE... to use the @tablename parameter being passed into the function. @tablename is a Linked Server name so I'm imagining I have to somehow use dynamic sql, but can't figure out the syntax

ALTER FUNCTION [dbo].[SF_PickLists](@tablename varchar(50))
RETURNS @PickListTable TABLE
      (ObjectName nvarchar(48),
       FieldName nvarchar(48),
       PickListValue nvarchar(256),
       PickListLabel nvarchar(256))
AS
BEGIN
      INSERT @PickListTable
      SELECT ObjectName, FieldName, PickListValue, PickListLabel
      FROM MYDATABASE...sys_sfpicklists
      RETURN
END
0
Comment
Question by:advlgx
  • 2
3 Comments
 
LVL 60

Expert Comment

by:chapmandew
ID: 22844032
can't do dynamic sql in a function
0
 
LVL 39

Expert Comment

by:BrandonGalderisi
ID: 22844040
You can't do that inside of a function.

You will get:

Invalid use of side-effecting or time-dependent operator in 'INSERT EXEC' within a function.


create function up_IDontWork (@b char(1))
returns @t table (a char(1))
as
begin
insert into @t
exec('select ''' + @b + '''')
 
return
end
 
go

Open in new window

0
 
LVL 39

Accepted Solution

by:
BrandonGalderisi earned 500 total points
ID: 22844063
I realize that you want to be able to "SELECT * FROM dbo.Your_function(@Param)" but you can't.  You would have to do it inside of a stored procedure and populate your temp table as such:



create procedure [dbo].[SF_PickLists](@tablename varchar(50))
 
AS
      exec('INSERT #PickListTable SELECT ObjectName, FieldName, PickListValue, PickListLabel
      FROM MYDATABASE...sys_sfpicklists')
END
 
go
 
create table #PickListTable 
      (ObjectName nvarchar(48), 
       FieldName nvarchar(48),
       PickListValue nvarchar(256),
       PickListLabel nvarchar(256))
 
exec [dbo].[SF_PickLists]
 
select * from #PickListTable 
 
drop table #PickListTable 

Open in new window

0

Featured Post

Industry Leaders: 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 needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how the fundamental information of how to create a table.

726 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