Answered stored procedure from view

  • Wednesday, August 22, 2012 6:40 PM
     
     

    I have created a stored procedure this contains a dynamic pivot. 

     create Procedure [dbo].[Usp_GetPivotTable]
    As
    Declare @PivotCols Varchar(2000)    
    Set @PivotCols=''
    Select @PivotCols=(Select Distinct '['+ country +'],' from t2 for Xml Path(''))
    Set @PivotCols=SUBSTRING(@PivotCols,1,len(@PivotCols)-1)
    --Dynemic Query To Run Pivot    
    exec('Select * From t2 as tmp pivot (MIN(country) for  country in ('+@PivotCols+')) as tbl')

    Now, I want to create a view. When am creating the below view, am getting the error...

    create view [dbo].[ViewTest]
    as
    select * from openquery(local, 'Usp_GetPivotTable')a

    ERROR:

    Msg 537, Level 16, State 2, Procedure Usp_GetPivotTable, Line 6
    Invalid length parameter passed to the LEFT or SUBSTRING function.

    Help required pls

All Replies

  • Wednesday, August 22, 2012 6:48 PM
     
     
    do you get any result when you run the sp alone?

    ANK HIT - if reply helps, please mark it as ANSWER or helpful post

  • Wednesday, August 22, 2012 6:51 PM
     
     
    do you get any result when you run the sp alone?

    ANK HIT - if reply helps, please mark it as ANSWER or helpful post

    Yes. I am getting result when I ran stored procedure. What is ur email id?
  • Wednesday, August 22, 2012 6:51 PM
     
     
    Yes. I am getting result when I ran stored procedure. What is ur email id?
  • Wednesday, August 22, 2012 7:32 PM
     
     
    It is working when I put this stored procedure 'sp_who' but when I put my own stored procedure 'Usp_GetPivotTable', am getting error - 

    ERROR:

    Msg 537, Level 16, State 2, Procedure Usp_GetPivotTable, Line 6
    Invalid length parameter passed to the LEFT or SUBSTRING function.

  • Thursday, August 23, 2012 2:21 AM
     
     Answered

    You can't execute an SP from within a view. Under normal circumstances you'd be able to load the SP results into a temp table and use the temp table in place of the view... but... since the SP is using a dynamic pivot, you won't know what columns to define.


    Jason Long