Click here to Skip to main content
15,887,848 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hey

1) Temp table ain't getting created by procedure.
2) even i tried using tempdb..tablename but not helpfull
3) So i modified code taken permanent table instead of temp table..
and code works and permanent table gets populated..


Can anyone please help....
I don't know why temmp table ain't getting created ?

Thanks in advance..

What I have tried:

ALTER PROCEDURE USP_FONM_HM
(
@DB_NAME NVARCHAR (MAX), -- DB NAME
@FORMAT_TABLE NVARCHAR (MAX),
@FORMAT_NODUPS NVARCHAR(MAX) -- REMOVE NO DUPS TABLE

)

AS

--@SQL_NO_DUPS_ID NVARCHAR (MAX) -- FETCH MAX ID FROM PREVIOUS SCRIPT

BEGIN
DECLARE @SQL_REMOVE_NODUPS NVARCHAR (MAX) -- TEMPORARY TABLE

PRINT '-- REMOVE DUPLICATES --'
BEGIN
SET @SQL_REMOVE_NODUPS=
CONCAT
(

'DROP TABLE ',SPACE(1),'#',@FORMAT_NODUPS,'

;WITH SAMPLECTE
AS
(
SELECT A.* , ROW_NUMBER () OVER ( PARTITION BY FIRST_NAME , LAST_NAME , ADDRESS, ZIP ORDER BY FIRST_NAME) AS RNUM
FROM ',SPACE(1), @DB_NAME,'..',@FORMAT_TABLE ,' A
)
SELECT * INTO',SPACE(2) ,'#',@FORMAT_NODUPS , '
FROM SAMPLECTE WHERE RNUM=','1','
'
)
END
PRINT @SQL_REMOVE_NODUPS

EXEC (@SQL_REMOVE_NODUPS)

END

------------------
EXEC USP_FONM_HM 'PRAC','TEST_FORMAT','TEST_FORMAT_NODUPS'
Posted
Updated 2-Oct-18 3:14am
Comments
an0ther1 2-Oct-18 1:39am    
Local Temp tables are only available for the connection they were created for. Once the connection is disconnected, the table is automatically deleted.
If you use SSMS & debug, step through, your stored procedure you can use the object explorer to view the table. But the name is not exactly as you have specified.
The table will be named #<tablename>______<sessionid>
This ensures that 2 users can create & access the same Temp table at once.

Kind Regards

1 solution

It won't be created if it already exists. In fact, you should get an error.

Furthermore, even though you define a string as NVARCHAR(MAX), you should precede the string itself with an N, like so:

SQL
DECLARE @query NVARCHAR(MAX) = N'...';


Running a dynamic query without the "N" may cause a really long query to be cut off and not run correctly.

I also make it a habit to precede the creation of a temp table with this (it's one of those CYA things):

SQL
IF UNIQUE_ID('tempdb..#tempTable', 'U') IS NOT NULL DROP TABLE #tempTable;
 
Share this answer
 
Comments
#realJSOP 2-Oct-18 14:12pm    
Do you have permissions to execute a stored proc?
Member 13138564 2-Oct-18 18:55pm    
Yes...

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