Click here to Skip to main content
15,896,557 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
Hi,

I have a stored procedure that accepts parameters which when concatenated with the basic part of an SQL statement produce the required query, which I then execute dynamically.

CREATE PROCEDURE [dbo].[ProcBookingDetails] 
	@Clerk varchar(max),
	@DepDateFrom varchar(50),
	@DepDateTo varchar(50),
	@BookDateFrom varchar(50),
	@BookDateTo varchar(50),
	@Branches varchar(max),
	@Products varchar(max),
	@Suppliers varchar (max),
	@Source varchar(max)
AS
BEGIN
	SET NOCOUNT ON;
DECLARE @sql nvarchar(max)

SET @sql =
'select  distinct b.booking_number as BkgRef....'
set @SQL = @SQL + @BookDateFrom + @BookDateTo + @DepDateFrom + @DepDateTo + @Clerk + @Branches + @Suppliers + @Products + @Source
<pre lang="sql">Create table #bkgnos (bkgref varchar(20))
insert into #bkgnos (bkgref)
EXECUTE sp_executesql @sql


This works fine but when there are multiple users on the system the queries are queued as they use the same temp table.

I was thinking to create a table/temp table for each user and enter the tablename dynamically to reduce the problem.
SQL
set @SQL1='IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N''[dbo].['+@tablename+']'') AND type in (N''U''))
DROP TABLE [dbo].['+@tablename+']'
execute sp_executesql @SQL1
set @SQL1='Create table '+@tablename +' (bkgref varchar(20))'
execute sp_executesql @SQL1


Inserting the tables works but then I found that I could not use the passed in parameters to create the rest of the dynamic SQL.

1. I am looking in the right direction to solve this issue.
2. If so how do i proceed?
3. If not any ideas to reduce processing time will be appreciated.

Thanks in advance
Posted
Comments
Harikrk 3-Jun-11 9:24am    
Can you let you know how you are creating the variable @tablename.
Creating temp tables can affect performance anyway. Check if you can avoid temptable also.

1 solution

Can you let you know how you are creating the variable @tablename.
Creating temp tables can affect performance anyway. Check if you can avoid temptable also.
 
Share this answer
 
Comments
milenalukic 3-Jun-11 9:37am    
I am passing in another variable which I populate from Visual studio by using Enviornment.UserName

I tried both permanent and temp tables. The issue here is not the performance of a single query. It is the fact that a user with a 2 minute query could have to wait 10 minutes or more because another user is running a larger query and blocking him. The time required for one query is acceptable. It is the queuing that is not, and what i am trying to solve.

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