悟生慧

 

存储过程中传递动态Sql语句

create procedure myprocdure 
@sqlstr nvarchar(1000) 
as 
exec (@sqlstr) 
go 
使用: 
EXEC MYPROCDURE N'SELECT * FROM TABLENAME'

字段名,表名,数据库名之类作为变量时,必须用动态SQL
----------------------------------------------
declare @fname varchar(20) set @fname = '[stuId]' exec ('select '+ @fname + ' from student')
当然将字符串改成变量的形式也可
declare @fname varchar(20) set @fname = '[stuId]'
declare @s varchar(1000) set @s= 'select ' + @fname + ' from student' Exec(@s)

posted on 2012-05-25 11:59  悟生慧  阅读(369)  评论(0编辑  收藏  举报

导航