Am new to T-SQL. I have a variable holding table name. How I query the same table using this variable as
DECLARE @tb_name varchar(300)
SET @tb_name = 'tbl_deleted_shipmentdata_record'
select * from @tb_name
Am new to T-SQL. I have a variable holding table name. How I query the same table using this variable as
DECLARE @tb_name varchar(300)
SET @tb_name = 'tbl_deleted_shipmentdata_record'
select * from @tb_name
YOu need to write dynamic SQL for that.
DECLARE @tb_name varchar(300)
SET @tb_name = 'tbl_deleted_shipmentdata_record'
Declare @SQL Nvarchar(Max)
SET @SQL = 'select * from '+ @tb_name
Exec SP_ExecuteSQL @SQL
You have to use dynamic query:
DECLARE @tb_name VARCHAR(300)
SET @tb_name = 'tbl_deleted_shipmentdata_record'
DECLARE @sql NVARCHAR(300) = 'select * from ' + QUOTENAME(@tb_name)
EXEC( @sql)
QUOTENAME
function surrounds @tb_name
variable with [tbl_deleted_shipmentdata_record]
. Just to minimize risk of sql injection
.