Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
SQL
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?

use EXEC (@str), it should resolve your problem.
 
Share this answer
 
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[^]
Try this and let us know

SQL
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
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900