While Executing the Following query it showing the Invalid object name '#temp1'.
can any body knows the error occurred due to which reason this is my orginal code i used to fetch code , her differnt tables are formed i need to get the sum of the each row of each table
DECLARE @t TABLE (
id int IDENTITY(1,1),
BranchName nvarchar(max)
)
DECLARE @n int = 0,
@i int = 1,
@BranchName nvarchar(max),
@sql nvarchar(max),
@columns nvarchar(max)
INSERT INTO @t
SELECT DISTINCT BranchName
FROM ALX_Branches
SELECT @n = @@ROWCOUNT
WHILE @n >= @i
BEGIN
SELECT @BranchName = BranchName
FROM @t
WHERE id = @i
SELECT @columns = (
SELECT DISTINCT ','+QUOTENAME([SubInventory])
FROM #MyTempTable
WHERE [BranchName] = @BranchName
FOR XML PATH('')
)
SELECT @sql = N'--
SELECT * into #temp1
FROM (
SELECT [BranchID],
[SubInventory],
[Product],
[Stock]
FROM #MyTempTable
WHERE [BranchName] = ''' +@BranchName +'''
) as t
PIVOT (
MAX([Stock]) FOR [SubInventory] IN ('+STUFF(@columns,1,1,'')+')
) as pvt'
EXEC sp_executesql @sql
select * from #temp1