SQL SERVER 动态语句实例

DECLARE @FSQ VARCHAR(2000)
DECLARE @COLUMN VARCHAR(200)
declare @CS VARCHAR(200)='管理员'
SET @COLUMN='USERNAME'
SET @FSQ='SELECT '+@COLUMN+' FROM SYS_USER where '+@COLUMN+' = ''管理员'' '

PRINT @FSQ
 
SET @FSQ='SELECT '+@COLUMN+' FROM SYS_USER where '+@COLUMN+' = '''+@CS+''''
PRINT @FSQ
 

EXEC (@FSQ)
  
SELECT USERNAME FROM SYS_USER where USERNAME = '管理员' 
SELECT USERNAME FROM SYS_USER where USERNAME = '管理员'

 

posted @ 2021-10-09 17:11  博客燕  阅读(101)  评论(0编辑  收藏  举报