简体   繁体   中英

display variable with table columns (select statement) in sql server

Here I want To display Variables Values with Columns of tables

declare @BaseMetal varchar(40)
set @BaseMetal='18K'

 SELECT   CAST(@BaseMetal as varchar(40)) 'BaseMetal', tbl.Columm1,tbl.Columm2, tbl.Columm3 from tbl

O/P should be :

BaseMetal   Columm1   Columm2     Columm3 
  18K       colval1    colval1    colval1
  18K       colval2    colval2    colval2

But when i tried it shows error of Must declare the scalar variable "@BaseMetal".

You don't need the cast.

declare @BaseMetal varchar(40)
set @BaseMetal='18K'

SELECT   @BaseMetal AS 'BaseMetal', tbl.Columm1,tbl.Columm2, tbl.Columm3 from tbl

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM