Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
SET FMTONLY ON
Exec YourStoredProcedureHere
SET FMTONLY OFF
Create Procedure Blah @TableName
As
SET NOCOUNT ON
If @TableName = 'People' Then
Select * From People
If @TableName = 'Color' Then
Select * From Color
Else
Select 'No data' As Result
Select * From Information_Schema.Routine_Columns
SELECT text
FROM syscomments
WHERE id = (SELECT id FROM sysobjects WHERE name = 'TestSP')
ORDER BY colid
/*
This is copyrighted material and may not be reproduced in any form
without express written permission of My Company.
Copyright c 2011 123 Company.
This is copyrighted material and may not be reproduced in any form
without express written permission of My Company.
Copyright c 2011 123 Company.
This is copyrighted material and may not be reproduced in any form
without express written permission of My Company.
Copyright c 2011 123 Company.
This is copyrighted material and may not be reproduced in any form
without express written permission of My Company.
Copyright c 2011 123 Company.
Created Date: 08/18/2011
Author: Me
Purpose: This SP is designed to return all issues
from the Issue table.
History
Mod Date Mod By Reference # Reason for change
------------------------------------------------------------------------------------------
9/5/2011 Me 45444 Added fields to the SELECT
*/
CREATE PROCEDURE [dbo].[TestSP]
@IssueID int = NULL
AS
IF(@IssueID = NULL)
SELECT Issues.*, mConfig.DBName, c.CustomerName, l.LocationName, l.LocationShortName, r.RoomName,
a.FirstName + ' ' + a.LastName AS [POC], g.Name AS [Assigned Group], u.FirstName + ' ' + u.LastName AS [Created By],
u2.FirstName + ' ' + u2.LastName AS [Assigned User], ist.IssueStatus AS [Status], iss.IssueStatus AS [Sub-Status],
p.Priority, ic.IssueCategory, o.Origin, it.IssueType, csd_p.Product, csd_v.VersionNumber, svt.SupportVisitType,
u3.FirstName + ' ' + u3.LastName AS [Resolved By], csd_psv.ProjectedSoftwareVersion
FROM Issues Issues
LEFT OUTER JOIN Customers c ON Issues.ConfigurationID = c.CustomerID
INNER JOIN SCMaster.dbo.Configuration mConfig ON c.CustomerShortName = mConfig.CustomerCode
LEFT OUTER JOIN Origins o ON Issues.OriginID = o.OriginID
LEFT OUTER JOIN Locations l ON Issues.LocationID = l.LocationID
LEFT OUTER JOIN Rooms r ON Issues.RoomID = r.RoomID
LEFT OUTER JOIN Groups g ON Issues.AssignedGroupID = g.GroupID
LEFT OUTER JOIN Users u ON Issues.CreatedByUserID = u.UserID
LEFT OUTER JOIN Users u2 ON Issues.AssignedUserID = u2.UserID
LEFT OUTER JOIN Users u3 ON Issues.ResolvedByUserID = u3.UserID
LEFT OUTER JOIN (SELECT * FROM IssueStatuses WHERE IsSubStatus = 0) ist ON Issues.IssueStatusID = ist.IssueStatusID
LEFT OUTER JOIN (SELECT * FROM IssueStatuses WHERE IsSubStatus = 1) iss ON Issues.IssueSubStatusID = iss.IssueStatusID
LEFT OUTER JOIN Priorities p ON Issues.PriorityID = p.PriorityID
LEFT OUTER JOIN SCMaster.dbo.Products csd_p ON Issues.ProductID = csd_p.ProductID
LEFT OUTER JOIN SCMaster.dbo.Versions csd_v ON Issues.VersionID = csd_v.VersionID
LEFT OUTER JOIN SCMaster.dbo.ProjectedSoftwareVersions csd_psv ON Issues.ProjectedSoftwareVersionID = csd_psv.ProjectedSoftwareVersionID
LEFT OUTER JOIN SupportVisitTypes svt ON Issues.SupportVisitTypeID = svt.SupportVisitTypeID
LEFT OUTER JOIN IssueCategories ic ON Issues.IssueCategoryID = ic.IssueCategoryID
LEFT OUTER JOIN IssueTypes it ON Issues.IssueTypeID = it.IssueTypeID
LEFT OUTER JOIN AllPOCs a ON Issues.POCID = a.AllPOCID AND c.CustomerID = a.CustomerID
ORDER BY IssueID DESC
ELSE
SELECT Issues.*, mConfig.DBName, c.CustomerName, l.LocationName, l.LocationShortName, r.RoomName,
a.FirstName + ' ' + a.LastName AS [POC], g.Name AS [Assigned Group], u.FirstName + ' ' + u.LastName AS [Created By],
u2.FirstName + ' ' + u2.LastName AS [Assigned User], ist.IssueStatus AS [Status], iss.IssueStatus AS [Sub-Status],
p.Priority, ic.IssueCategory, o.Origin, it.IssueType, csd_p.Product, csd_v.VersionNumber, svt.SupportVisitType,
u3.FirstName + ' ' + u3.LastName AS [Resolved By], csd_psv.ProjectedSoftwareVersion
FROM Issues Issues
LEFT OUTER JOIN Customers c ON Issues.ConfigurationID = c.CustomerID
INNER JOIN SCMaster.dbo.Configuration mConfig ON
c.CustomerShortName = mConfig.CustomerCode
LEFT OUTER JOIN Origins o ON Issues.OriginID = o.OriginID
LEFT OUTER JOIN Locations l ON Issues.LocationID = l.LocationID
LEFT OUTER JOIN Rooms r ON Issues.RoomID = r.RoomID
LEFT OUTER JOIN Groups g ON Issues.AssignedGroupID = g.GroupID
LEFT OUTER JOIN Users u ON Issues.CreatedByUserID = u.UserID
LEFT OUTER JOIN Users u2 ON Issues.AssignedUserID = u2.UserID
LEFT OUTER JOIN Users u3 ON Issues.ResolvedByUserID = u3.UserID
LEFT OUTER JOIN (SELECT * FROM IssueStatuses WHERE IsSubStatus = 0) ist ON Issues.IssueStatusID = ist.IssueStatusID
LEFT OUTER JOIN (SELECT * FROM IssueStatuses WHERE IsSubStatus = 1) iss ON Issues.IssueSubStatusID = iss.IssueStatusID
LEFT OUTER JOIN Priorities p ON Issues.PriorityID = p.PriorityID
LEFT OUTER JOIN SCMaster.dbo.Products csd_p ON Issues.ProductID = csd_p.ProductID
LEFT OUTER JOIN SCMaster.dbo.Versions csd_v ON Issues.VersionID = csd_v.VersionID
LEFT OUTER JOIN SCMaster.dbo.ProjectedSoftwareVersions csd_psv ON Issues.ProjectedSoftwareVersionID = csd_psv.ProjectedSoftwareVersionID
LEFT OUTER JOIN SupportVisitTypes svt ON Issues.SupportVisitTypeID = svt.SupportVisitTypeID
LEFT OUTER JOIN IssueCategories ic ON Issues.IssueCategoryID = ic.IssueCategoryID
LEFT OUTER JOIN IssueTypes it ON Issues.IssueTypeID = it.IssueTypeID
LEFT OUTER JOIN AllPOCs a ON Issues.POCID = a.AllPOCID AND c.CustomerID = a.CustomerID
WHERE Issues.IssueID = @IssueID
ORDER BY IssueID DESC
and while it returns multiple rows
Select Object_Definition(Object_ID('YourProcedureNameHere'))
I guess the next thing from here would be to write a front-end wrapper that would go through the resultset and parse out the data from the 'SELECT' to the 'FROM'
SELECT Issues.*, mConfig.DBName, c.CustomerName,
SELECT Issues.*, mConfig.DBName, c.CustomerName,
--run the sproc first, to generate an execution plan
SET FMTONLY ON
EXEC dbo.uspGetBillOfMaterials @StartProductID = 0, @CheckDate = '2011-09-29 13:07:20'
SET FMTONLY OFF
SELECT DISTINCT split.part AS colref
FROM sys.dm_exec_cached_plans cache
CROSS APPLY sys.dm_exec_text_query_plan(cache.plan_handle, DEFAULT, DEFAULT) texts
CROSS APPLY dbo.ufnSplitDelimitedString(texts.query_plan, '<') split
WHERE split.part LIKE 'ColumnReference%' AND NOT split.part LIKE 'ColumnReference Column%'
AND texts.objectid = OBJECT_ID('dbo.uspGetBillOfMaterials')
CREATE FUNCTION dbo.ufnSplitDelimitedString (
@InputString VARCHAR(MAX)
, @Delimiter VARCHAR(10))
RETURNS @tbl TABLE (part VARCHAR(MAX))
AS
BEGIN
DECLARE @i INT, @j INT
SET @i = 1
WHILE @i <= LEN(@InputString)
BEGIN
SELECT @j = CHARINDEX(@Delimiter, @InputString, @i + 1)
IF @j = 0 SELECT @j = LEN(@InputString) + 1
INSERT INTO @tbl SELECT SUBSTRING(@InputString, @i, @j - @i)
SELECT @i = @j + LEN(@Delimiter)
END
RETURN
END