?
Solved

SSRS Report of View Create Script Using Query Message

Posted on 2011-03-21
6
Medium Priority
?
356 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 37

Expert Comment

by:ValentinoV
ID: 35189920
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
ID: 35231925
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
ID: 35239217
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:CDMantel
ID: 35255921
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 2000 total points
ID: 35273442
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
ID: 35295204
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

764 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