Click here to Skip to main content
14,239,876 members
Rate this:
Please Sign up or sign in to vote.
See more:
USE [ABC]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER Procedure [dbo].[LibFetchFilterItem]
@Query nvarchar(1000)

AS
Begin
set nocount on

DECLARE @str varchar(8000)

SET @str = N'
  select ID
  ,case  when i.CodeVersion>1 then i.Code+''+.+''+convert(varchar,i.CodeVersion) else i.Code end as Code
  ,i.OldCode
  ,HRMS.dbo.InitialCap(i.[Name]) as Name
  ,i.[Description]
  ,d.DeptName as Department
  ,HRMS.dbo.InitialCap(p.[Name]) as Product
  ,Edition
  ,[Version]
  ,PublisherBrand
  ,Country
  ,KeywordDesc
  ,l.[name] as Location
  ,HRMS.dbo.InitialCap(s.[Name]) as SubCategory
  ,Synopsis
  ,i.Website
  from LIBMstItem i inner join LIBMstLocation l  on i.LocationId=l.LocationId
  inner join LIBMstProduct p on i.ProductCategory = p.Id
  Left outer join  HRMS.dbo.Master_EmployeeMaster u on i.BroughtBy=u.[NewID]
  Left outer join  dynamicreport.dbo.CompanyMst c on i.CompanyId = c.CompanyId
  Left outer join  HRMS.dbo.Master_DepartmentMaster d on i.DepartmentID = d.RowID
  inner join LIBMstSubCategory s on  i.SubCategory = s.Id
  where (i.Scrap is null or i.Scrap=0) 
  and i.IsActive=1
  and i.KitId=0' + ' '  + @Query + ' ' +
  'and 0=0 order by i.ID'
 
PRINT @str
EXEC @str

end
go

When i Execute the above procedure it's compiled successfully, but when i run it using this statement EXEC LibFetchFilterItem 'and i.Code=''10''' then

Msg 203, Level 16, State 2, Procedure LibFetchFilterItem, Line 40
The name '
select ID
,case when i.CodeVersion>1 then i.Code+'+.+'+convert(varchar,i.CodeVersion) else i.Code end as Code
,i.OldCode
,HRMS.dbo.InitialCap(i.[Name]) as Name
,i.[Description]
,d.DeptName as Department
,HRMS.dbo.InitialCap(p.[Name]) as Product
,Edition
,[Version]
,PublisherBrand
,Country
,KeywordDesc
,l.[name] as Location
,HRMS.dbo.InitialCap(s.[Name]) as SubCategory
,Synopsis
,i.Website
from LIBMstItem i inner join LIBMstLocation l on i.LocationId=l.LocationId
inner join LIBMstProduct p on i.ProductCategory = p.Id
Left outer join HRMS.dbo.Master_EmployeeMaster u on i.Brought' is not a valid identifier.

error Occured .. But when i just print the query it is 100% right ...
Plz help me ...
Posted
Updated 11-Jul-19 19:23pm
v3
Comments
_Asif_ 12-Aug-13 2:04am
   
You have not posted the value of @Query. Let us know the value of @Query.
Hitesh Vaghasiya 12-Aug-13 2:26am
   
EXEC LibFetchFilterItem 'and i.Code=''10'''

Here
and i.Code=''10''
is the value of @Query
_Asif_ 12-Aug-13 4:18am
   
can you paste the final query. Also did you check the query by executing the final one in query analyzer?
Rate this:
Please Sign up or sign in to vote.

Solution 2

use EXEC (@str), it should resolve your problem.
   
Comments
CHill60 12-Jul-19 4:01am
   
I'm guessing you got downvoted because this is such an old post that you've answered. However you are correct. It would have been nice if you had explained why though - without the parentheses you are trying to execute a stored procedure or a function; with the parentheses you are trying to execute the contents of a string. Reference: EXECUTE (Transact-SQL) - SQL Server | Microsoft Docs[^]
Rate this:
Please Sign up or sign in to vote.

Solution 1

Try this and let us know

USE [ABC]
GO
 
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER Procedure [dbo].[LibFetchFilterItem]
@Query nvarchar(1000)
 
AS
Begin
set nocount on
 
DECLARE @str varchar(8000)
 
SET @str = N'
  select ID
  ,case  when i.CodeVersion>1 then i.Code+''+'''.'''+''+convert(varchar,i.CodeVersion) else i.Code end as Code
  ,i.OldCode
  ,HRMS.dbo.InitialCap(i.[Name]) as Name
  ,i.[Description]
  ,d.DeptName as Department
  ,HRMS.dbo.InitialCap(p.[Name]) as Product
  ,Edition
  ,[Version]
  ,PublisherBrand
  ,Country
  ,KeywordDesc
  ,l.[name] as Location
  ,HRMS.dbo.InitialCap(s.[Name]) as SubCategory
  ,Synopsis
  ,i.Website
  from LIBMstItem i inner join LIBMstLocation l  on i.LocationId=l.LocationId
  inner join LIBMstProduct p on i.ProductCategory = p.Id
  Left outer join  HRMS.dbo.Master_EmployeeMaster u on i.BroughtBy=u.[NewID]
  Left outer join  dynamicreport.dbo.CompanyMst c on i.CompanyId = c.CompanyId
  Left outer join  HRMS.dbo.Master_DepartmentMaster d on i.DepartmentID = d.RowID
  inner join LIBMstSubCategory s on  i.SubCategory = s.Id
  where (i.Scrap is null or i.Scrap=0) 
  and i.IsActive=1
  and i.KitId=0' + ' '  + @Query + ' ' +
  'and 0=0 order by i.ID'
 
PRINT @str
EXEC @str
 
end
go


I hope it will solve your problem
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100