Solved

Change Function to dynamic SQL

Posted on 2008-10-30
3
961 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how the fundamental information of how to create a table.

813 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now