Solved

Change Function to dynamic SQL

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
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…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

839 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