首页 > 代码库 > sp_executesql 使用
sp_executesql 使用
declare @tableName nvarchar(50),@SQLString nvarchar(max),@userid varchar(50),@ParmDefinition nvarchar(max);
set @tableName=‘commodity‘;
set @userid=‘15639919851‘;
--set @sql=N‘select @count=count(empid) from table27‘
set @SQLString=N‘select id from ‘+@tableName+‘ where userid=@userid‘;
set @ParmDefinition=N‘@userid nvarchar(50)‘;
exec sp_executesql @SQLString,@ParmDefinition,@userid=@userid ;
--declare @count int,@tableName nvarchar(50),@SQLString nvarchar(max),@proid int,@id int,@ParmDefinition nvarchar(max);
--set @tableName=‘commodity‘;
--set @proid=433;
--set @id=159;
----set @sql=N‘select @count=count(empid) from table27‘
--set @SQLString=N‘select @countOUT=count(empid) from ‘+@tableName+‘ where proid=@proid1 and id<@id1 and state!=4‘;
--set @ParmDefinition=N‘@proid1 int,@id1 int,@countOUT int output‘;
--exec sp_executesql @SQLString,@ParmDefinition,@proid1=@proid,@id1=@id,@countOUT=@count output;
--select @count;
sp_executesql 使用