Selct From Store Procedure Dynamically

DECLARE @column NVARCHAR(MAX),
        @sql NVARCHAR(MAX),
        @spname NVARCHAR(MAX)
       
        -- Name of Stored procedure.
        SET @spname = '[dbo].[USP_Get_CustomerAll]'
        -- column name with datatype
        -- Structure of column and datadype must match with stored procedure.
            SET @column = '              

                        [Customer_Code] NVARCHAR(MAX),
                        [Customer_Name] NVARCHAR(MAX),
                        [AddressLine1]  NVARCHAR(MAX),
                        [AddressLine2]  NVARCHAR(MAX),
                        [Country_Code]  NVARCHAR(MAX),
                        [Country_Name]  NVARCHAR(MAX),
                        [State_Code]    NVARCHAR(MAX),
                        [States_Name]   NVARCHAR(MAX),
                        [City_Code]     NVARCHAR(MAX),
                        [City_Name]     NVARCHAR(MAX),
                        [Region_Code]   NVARCHAR(MAX),
                        [Region]        NVARCHAR(MAX),
                        [Area_Code]     NVARCHAR(MAX),
                        [Area_Name]     NVARCHAR(MAX),
                        [Email]         NVARCHAR(MAX),
                        [Phone]         NVARCHAR(MAX),
                        [Mobile]        NVARCHAR(MAX),
                        [Web]           NVARCHAR(MAX),
                        [NoofFollowup]  INT,
                        [Status]        NVARCHAR(MAX),
                        [PinCode]       NVARCHAR(MAX)


   SET @sql = ' CREATE TABLE #Temp

( ' + ' ' + @column  + ') INSERT #Temp EXEC ' + @spname + '

      select * from #Temp

DROP TABLE #Temp'

PRINT @sql
  EXEC sp_executesql @sql

Ebook Download
View all
Learn
View all