Solved

SSRS Report of View Create Script Using Query Message

Posted on 2011-03-21
6
344 Views
Last Modified: 2012-05-11
I would like to create an SSRS report that presents the Create View script for a view the user enters as a parameter.  I found this helpful query on this forum to generate the message:

DECLARE @SqlText nvarchar(max)
SET @SqlText = ''
SELECT @SqlText = @SqlText + [Text] FROM sys.syscomments
WHERE id = OBJECT_ID('dbo.Sales')
ORDER BY colid
PRINT @SqlText

I'm struggling with how one accesses the message from a query in SSRS - if that is even possible.  Or is there a better way to do it?

Thanks.
0
Comment
Question by:CDMantel
  • 3
  • 3
6 Comments
 
LVL 37

Expert Comment

by:ValentinoV
Comment Utility
I understand that you want to create a report that shows the CREATE VIEW statement.  Ideally the user can select the views from a dropdown, right?
To get a list of all views, including their definition and object_id, you can use the query in the snippet.

Note that the query is using sys.sql_modules instead of sys.syscomments (that one is outdated).

But I don't understand what you mean with that last sentence: "I'm struggling with...".  Please clarify if still applicable.
SELECT sm.object_id, OBJECT_NAME(sm.object_id) AS view_name, sm.definition
FROM sys.sql_modules AS sm
JOIN sys.objects AS o ON sm.object_id = o.object_id
WHERE o.type = 'V'
ORDER BY o.type ASC;

Open in new window

0
 

Author Comment

by:CDMantel
Comment Utility
Thanks for responding VelentinoV.

Yes, you are quite right that i'm thinking the user would select from a list of available views in a dropdown.

The query you suggested gathers the needed data.  The "struggle" is in formatting the data (the Create View script in this case) so that it is visually appealing (that is, not breaking at a random point for line wrap).  The SQL Print command will do that.  However, as i understand it, the output of the Print command is as a SQL message.  So my struggle is in accessing this output from SSRS OR using some other method to format the script.
0
 
LVL 37

Expert Comment

by:ValentinoV
Comment Utility
Okay, understood what you mean now!  Let's see, perhaps the sp_helptext system procedure can be a solution here?  It returns the object's definition split up into multiple "records".  If you use that procedure to retrieve the view's definition and set up a List in your report to show the records, I think it should work.

See http://msdn.microsoft.com/en-us/library/ms176112.aspx
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Author Comment

by:CDMantel
Comment Utility
True enough.  sp_helptext splits output into multiple records.  Problem is that the location of the splitting seems random.  It doesn't follow the same logic that Print uses.  I can get all the information needed.  it just won't be presented in an easier to read format as Print does.  Is my quest in vain?
0
 
LVL 37

Accepted Solution

by:
ValentinoV earned 500 total points
Comment Utility
That's strange.  In my test I'm getting the same results from both methods, with equal line breaks and indentation.  Both methods are returning the view the way it was created, similar to if you would script the view from the Management Studio (right-click view > Script View As > Create To > New Query Editor Window).

If you're looking for even nicer output (including syntax coloring for instance) then I think you'll need to look at developing a Custom Report Item.  But the effort needed would be of course much higher than with a query and a List control.
If you're up to it,  here's a link to get you started: http://msdn.microsoft.com/en-us/library/ms345231.aspx
0
 

Author Comment

by:CDMantel
Comment Utility
Wow.  My bad.  You're right.  Sending the query output to text proves the point.  Thanks very much for your help (including the link for custom report item).
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
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 to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

772 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

9 Experts available now in Live!

Get 1:1 Help Now