SQL Renaming a column of a temp table

SamBa1927 1 Reputation point
2022-11-29T11:56:34.63+00:00

I can't get what is not working in the below bold highlighted line. I would be grateful to anyone who could help me!

DECLARE @AssetWithDrawStartDate DATE;
DECLARE @AssetWithDrawDate DATE;
DECLARE @TradeStartDate DATE;
DECLARE @TradeDate DATE;
SET @AssetWithDrawDate= GETDATE()
SET @AssetWithDrawStartDate = dateadd(day,-7,@AssetWithDrawDate)
SET @TradeDate= GETDATE()
SET @TradeStartDate = dateadd(day,-30,@TradeDate)
SELECT DateTime, Asset, ClientId, Volume
INTO #TempWithdraw FROM dbo.cashoperations
EXEC SP_RENAME '#TempWithdraw.[DateTime]' , '[Dat]', 'COLUMN'
SELECT * FROM #TempWithdraw
Where Datetime >= @AssetWithDrawStartDate and [DateTime] <= @AssetWithDrawDate

SQL Server on Azure Virtual Machines
Azure SQL Database
{count} votes

1 answer

Sort by: Most helpful
  1. Alberto Morillo 32,886 Reputation points MVP
    2022-11-29T12:23:07.957+00:00

    Please try the following:

    EXEC tempdb.sys.sp_rename N'#TempWithdraw.DateTime', N'Dat', N'COLUMN';  
    

    You need to invoke sp_rename from TempDB.

    0 comments No comments