Please start any new threads on our new site at http://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 putting a dynamic query in Stored procedure?
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

akpaga
Constraint Violating Yak Guru

USA
331 Posts

Posted - 08/28/2013 :  15:59:08  Show Profile  Reply with Quote
Hi friends,

I want to put this query in stored procedure so that I can exec the sp on a telerik report by calling it. How can achieve it

DECLARE @cols AS NVARCHAR(MAX);
DECLARE @query AS NVARCHAR(MAX);
DECLARE @SpatialId AS NVARCHAR(MAX);
DECLARE @SpatialIdDynamic AS NVARCHAR(MAX);


select @cols = STUFF((SELECT distinct ',' +
QUOTENAME(AnalysisDescription)
FROM GISportal.vwSpatialReport
where Convert(uniqueidentifier, '54CB783F-2207-45BE-A617-E730AD7D21C5') = SpatialReportID
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
, 1, 1, '');

SELECT @query =

'SELECT *
FROM
(
SELECT
SpatialReportID,
AnalysisDescription,
ResultDescription
FROM GISportal.vwSpatialReport
WHERE Convert(uniqueidentifier,'+@SpatialIdDynamic +') = SpatialReportID

) AS t
PIVOT
(
MAX(ResultDescription)
FOR AnalysisDescription IN( ' + @cols + ' )' +
' ) AS p ; ';

execute(@query);


thank you very much

James K
Flowing Fount of Yak Knowledge

3873 Posts

Posted - 08/28/2013 :  16:07:39  Show Profile  Reply with Quote
Assuming the query as it exists is working correctly for you,
CREATE PROCEDURE dbo.TestProc

	@cols AS NVARCHAR(MAX),
	@query AS NVARCHAR(MAX),
	@SpatialId AS NVARCHAR(MAX),
	@SpatialIdDynamic AS NVARCHAR(MAX)

AS
select @cols = STUFF((SELECT distinct ',' +
.....
If you don't want those parameters on the stored proc, it is even simpler:

CREATE PROCEDURE dbo.TestProc

as

DECLARE @cols AS NVARCHAR(MAX);
DECLARE @query AS NVARCHAR(MAX);
DECLARE @SpatialId AS NVARCHAR(MAX);
DECLARE @SpatialIdDynamic AS NVARCHAR(MAX);
.....


Go to Top of Page

kameswararao polireddy
Starting Member

India
19 Posts

Posted - 08/29/2013 :  05:01:54  Show Profile  Reply with Quote
As far as I understood this may work fine for you...

CREATE PROCEDURE dbo.USP_TestProcedure

@SpatialIdDynamic AS NVARCHAR(MAX)

AS
SET NOCOUNT ON
BEGIN
DECLARE @cols AS NVARCHAR(MAX)
DECLARE @query AS NVARCHAR(MAX)
DECLARE @SpatialId AS NVARCHAR(MAX)

select @cols = STUFF((SELECT distinct ',' + QUOTENAME(AnalysisDescription)
FROM GISportal.vwSpatialReport
where Convert(uniqueidentifier, '54CB783F-2207-45BE-A617-E730AD7D21C5') = SpatialReportID
FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)') , 1, 1, '')

SELECT @query =
'SELECT * FROM
(
SELECT
SpatialReportID,
AnalysisDescription,
ResultDescription
FROM GISportal.vwSpatialReport
WHERE Convert(uniqueidentifier,'+@SpatialIdDynamic +') = SpatialReportID

) AS t
PIVOT
(
MAX(ResultDescription) FOR AnalysisDescription IN( ' + @cols + ' )' +' ) AS p ; ';

EXECUTE (@query)


P.Kameswara rao
Go to Top of Page

akpaga
Constraint Violating Yak Guru

USA
331 Posts

Posted - 08/30/2013 :  10:56:27  Show Profile  Reply with Quote
Thank you my friends and speciallly kamesh.
Go to Top of Page
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.09 seconds. Powered By: Snitz Forums 2000