here is my store procedure:-
USE [TenderCrawling] GO /****** Object: StoredProcedure [dbo].[Usp_Temp] Script Date: 5/29/2017 11:51:33 AM ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO -- ============================================= -- Author: <Author,,Name> -- Create date: <Create Date,,> -- Description: <Description,,> -- ============================================= ALTER PROCEDURE [dbo].[Usp_Temp] @SiteId int AS BEGIN SET NOCOUNT ON; Declare @abc int set @abc=(select count(1) from sitemaster) Declare @TemplateName varchar(50) Declare @Isindian bit Declare @Id int Declare @output nvarchar(max) Declare @temp bit Select @TemplateName=Template,@Isindian=IsIndianSite from SiteMaster where SiteId=@SiteId SET @TemplateName= (select substring(@TemplateName, 1, (len(@TemplateName) - 4))) SET @TemplateName=@TemplateName+'_ErrorLog' IF EXISTS (SELECT 1 FROM Information_SCHEMA.Tables WHERE TABLE_NAME = @TemplateName) BEGIN set @temp=1 END else Begin set @temp=0 End if(@temp=1) begin SET @output='select log,OrganizationName,TenderNumber,RequirementWorkBrief,TenderDetailWorkDescription,NameOfWebSite' if(@Isindian=0) Begin SET @output+=',City,State,Country from'+' '+@TemplateName+' '+'e left join sitemaster s on s.SiteId=e.SiteId Where e.IsDuplicate = 0' end else Begin SET @output+= ' from'+' '+@TemplateName+' '+'e left join sitemaster s on s.SiteId=e.SiteId Where e.IsDuplicate = 0 ' end end print @output exec sp_executesql @output END
end
else
Begin
SET @output+= ' from'+' '+@TemplateName+' '+'e left join sitemaster s on s.SiteId=e.SiteId Where e.IsDuplicate = 0 '
print @output exec sp_executesql @output
END