?
Solved

Temporary table and Table variable issue

Posted on 2009-04-22
2
Medium Priority
?
628 Views
Last Modified: 2012-05-06
I am wondering why this temporary table approach works in my stored procedure and my table variable approach doesn't.

I get this error ONLY for the variable table approach for each line that contains the WITH(NOLOCK). i.e- Temporary approach works fine

"Incorrect syntax near the keyword 'with'. If this statement is a common table expression or an xmlnamespaces clause, the previous statement must be terminated with a semicolon."

Im using SQL Server 2005. Any ideas?
-- TEMPORARY APPROACH --
ALTER PROCEDURE [dbo].[GetWriteAccess1]
	@UserName nvarchar(256),
	@UserId uniqueidentifier
AS
BEGIN
	SET NOCOUNT ON;
 
		create table #custom_Users
		(
			UserName			nvarchar(256),
			UserId				uniqueidentifier
		)
 
		insert into #custom_Users (UserName, UserId) VALUES (@UserName, @UserId)
 
IF (@UserId IS NULL)
			RETURN(1)
 
	SELECT u.UserName, wa.WriteAccess, wa.Write_Id, wt.WriteName
	FROM #custom_Users u WITH(NOLOCK)
	JOIN tb_WriteAccess wa WITH(NOLOCK) ON u.UserId = wa.[User_Id]
	JOIN tb_WriteType wt WITH(NOLOCK) ON wa.Write_Id = wt.Write_Id
	WHERE  wa.[User_Id] = @UserId
 
	select * from #custom_Users
 
END
 
-- VARIABLE APPROACH --
declare @custom_Users table
		(
			UserName			nvarchar(256),
			UserId				uniqueidentifier
		)
 
		insert @custom_Users (UserName, UserId) values (@UserName,@UserId)
 
IF (@UserId IS NULL)
			RETURN(1)
 
	SELECT u.UserName, wa.WriteAccess, wa.Write_Id, wt.WriteName
	FROM @custom_Users u WITH(NOLOCK)
	JOIN tb_WriteAccess wa WITH(NOLOCK) ON u.UserId = wa.[User_Id]
	JOIN tb_WriteType wt WITH(NOLOCK) ON wa.Write_Id = wt.Write_Id
	WHERE  wa.[User_Id] = @UserId
 
	select * from @custom_Users
 
END

Open in new window

0
Comment
Question by:caoimhincryan
2 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 24205676
for temp tables or table variables, you don't need to use NOLOCK at all, as there cannot be any other process using that same table.
so, just remove NOLOCK from temp tables or table variables, to start with.
0
 

Author Comment

by:caoimhincryan
ID: 24206046
Thanks for quick reply. It seems to have done the job.

Thanks a lot.
0

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question