[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Assigning the result of dynamic SQL to the result in a function

Posted on 2004-11-18
2
Medium Priority
?
229 Views
Last Modified: 2012-06-27
I have the following stored procedure:

CREATE PROCEDURE spInfoSchemaTableList
    (
    @DB nvarchar(30)
    )
AS
--- Set SQL Environment Variables
SET NOCOUNT ON

--- Initialize Stored Procedure

    DECLARE @SQL nvarchar (2000), @DB2 nvarchar(32)
    SET @DB2 = N'[' + @DB + ']'

    SET @SQL = N'SELECT TABLE_NAME FROM ' + @DB2 + '.INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = '  + CHAR(39) + 'BASE TABLE' + CHAR(39) + 'AND TABLE_NAME <> ' + CHAR(39) + 'dtproperties' + CHAR(39) + ' ORDER BY TABLE_NAME'
    EXEC (@SQL)
 
SP_EXIT:
  RETURN

Is there a way to convert this into a function so that when given a database it will return a list of tables?
Is there also a way to asign the EXEC (@SQL) to a table variable?

Thanks,

Tom
0
Comment
Question by:TomCoury
2 Comments
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 2000 total points
ID: 12620320
>>Is there a way to convert this into a function so that when given a database it will return a list of tables?<<
If you mean a function, as in a User Defined Function (UDF), than the answer is no:  You cannot use dynamic SQL in UDF's

>>Is there also a way to asign the EXEC (@SQL) to a table variable?<<
You can insert the values to a temporary table, as in:
CREATE TABLE #MyTempTable (TableName varchar(50))

...

INSERT INTO #MyTempTable EXEC (@SQL)

...

DROP TABLE #MyTempTable

and then copy those to a variable of type table. Although the second step seems like over-kill.
0
 

Author Comment

by:TomCoury
ID: 12626873
Thanks... That's what I was afraid of...
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
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.
Suggested Courses

834 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