Solved

Change Function to dynamic SQL

Posted on 2008-10-30
3
958 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
Comment Utility
can't do dynamic sql in a function
0
 
LVL 39

Expert Comment

by:BrandonGalderisi
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

743 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

14 Experts available now in Live!

Get 1:1 Help Now