Click here to Skip to main content
15,912,329 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

I'm facing a problem and hope that can get some advise from you. I want to pass a filter query into store procedure.

For example,

SQL
alter proc [dbo].[SP_Staff]
     @Filter varchar(1000)=''
as
--drop tempTable if exist
if exists (select  * from tempdb.dbo.sysobjects o where o.xtype in ('U')
    and o.id = object_id(N'tempdb..#TmpTable')) 
DROP TABLE #TmpTable;
DECLARE @SQLQuery AS VARCHAR(1000)
DECLARE @SQLQuery1 AS VARCHAR(1000)

SET @SQLQuery = 'select * into TmpTable From Staff'
SET @SQLQuery1 = 'order by StaffCode'
IF Len(@Filter) > 0
Begin
SET @SQLQuery = @SQLQuery + ' Where ' + @Filter + @SQLQuery1
End
EXECUTE(@SQLQuery)
select * from #TmpTable

execute SP_Staff 'StaffCode between 001 and 005'


It got error "Invalid object name '#TmpTable'" when I execute the store procedure.
How to solve this error?
Posted
Updated 21-Jun-12 22:53pm
v2
Comments
Maciej Los 22-Jun-12 4:58am    
What's the problem?
Sandeep Mewara 22-Jun-12 5:17am    
And the issue is?

Hi frnd:

Basically it is unable to find temporary table #TmpTable
First of all i would like to know where you are creating #TmpTable table.

One another thing missing in the below line:
"HTML"
SET @SQLQuery = 'select * into TmpTable From Staff'


It should be like this :
"HTML"
SET @SQLQuery = 'select * into #TmpTable From Staff'


My suggestion is instead of using this code:

"HTML"
if exists (select  * from tempdb.dbo.sysobjects o where o.xtype in ('U')
    and o.id = object_id(N'tempdb..#TmpTable'))
DROP TABLE #TmpTable;

Modify the code like this :

SQL
if exists (select  * from tempdb.dbo.sysobjects o where o.xtype in ('U')
    and o.id = object_id(N'tempdb..#TmpTable'))
Begin
  DROP TABLE #TmpTable;
  Create Table #TmpTable(
   Id int --  you may include more columns (for Example)
  )
End
 
Share this answer
 
Comments
Sandeep Mewara 22-Jun-12 14:05pm    
5!
You don't need to remove the temp table in begining. It would anyways be removed after the SP execution.

Look at this:

SQL
alter proc [dbo].[SP_Staff]
     @Filter varchar(1000)=''
as

DECLARE @SQLQuery AS VARCHAR(1000)
DECLARE @SQLQuery1 AS VARCHAR(1000)

SET @SQLQuery = 'select * into #TmpTable From Staff'
SET @SQLQuery1 = 'order by StaffCode'
IF Len(@Filter) > 0
Begin
SET @SQLQuery = @SQLQuery + ' Where ' + @Filter + @SQLQuery1
End
EXECUTE(@SQLQuery)
select * from #TmpTable

DROP TABLE #TmpTable;
 
Share this answer
 
Comments
Sandeep Mewara 22-Jun-12 14:06pm    
My 5!
It should be like this

SQL
SET @SQLQuery = 'select * into #TmpTable From Staff'
SET @SQLQuery1 = 'order by StaffCode'
 
Share this answer
 
SQL
if (IsNull(OBJECT_ID('SP_Staff'),0) <> 0)
	Drop Procedure SP_Staff
Go
Create Procedure SP_Staff
	@rvcFilter	varchar(max)
As  
Begin

	Declare @vcQuery varchar(max)
	
	if (IsNull(OBJECT_ID('tempdb..##tempTable'),0) <> 0)
		Drop Table ##temp1234;
	
	if(Len(RTRIM(LTRIM(@rvcFilter))) = 0)
		Set @rvcFilter = '1 = 1';
	
	Set @vcQuery =
		'Select * into #tempTable From Staff'
		+ ' Where '
		+ @rvcFilter
		+ ' Order By StaffCode';
	
	Execute(@vcQuery)	

	Select * From ##tempTable

End
Go

EXECUTE  SP_Staff 'StaffCode between 001 and 005'
 
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