Hi @ahmed salah ,
I can't find the statement to call the table from the subroutine in your main program [Parts].[sp_TradeCodeGenerateByPLandCodeType].
Please refer to a simple example first:
CREATE TABLE [dbo].[MyTable](
[col1] [int] NOT NULL,
[col2] [char](5) NULL
) ON [PRIMARY]
INSERT INTO [MyTable]
([col1],
[col2])
VALUES (1,
A)
Go
INSERT INTO [MyTable]
([col1],
[col2])
VALUES (2,
B)
GO
INSERT INTO [MyTable]
([col1],
[col2])
VALUES (3,
C)
Go
INSERT INTO [MyTable]
([col1],
[col2])
VALUES (4,
D)
Go
-- If Exist then drop and create
IF EXISTS (SELECT *
FROM sys.objects
WHERE object_id = Object_id(N'[dbo].[called_procedure]')
AND type IN ( N'P', N'PC' ))
DROP PROCEDURE [dbo].[called_procedure]
GO
CREATE PROCEDURE Called_procedure @par1 INT,
@par2 BIT
AS
BEGIN
IF Object_id('tempdb..#mytemp') IS NOT NULL
BEGIN
INSERT INTO #mytemp
SELECT *
FROM Mytable
WHERE col1 = @par1
END
END
Go
IF EXISTS (SELECT *
FROM sys.objects
WHERE object_id = Object_id(N'[dbo].[caller_procedure1]')
AND type IN ( N'P', N'PC' ))
DROP PROCEDURE [dbo].[caller_procedure1]
GO
CREATE PROCEDURE Caller_procedure1
AS
BEGIN
--Testing for if temp table does not exists in scope
EXEC Called_procedure
1,
0
IF Object_id('tempdb..#mytemp') IS NOT NULL
BEGIN
SELECT *
FROM #mytemp
END
END
GO
Regards,
Echo