sql存储过程中执行sql语句

今天写sql语句快写晕了,后来网上查到了资料自己又整理下,最终的代码备份一下

create proc [dbo].[search_ProductTakeStockView_byCP]
@code varchar(50),@producctName varchar(50)
as
declare @s varchar(100)
set @s='select * from ProductTakeStockView where 1=1'
if @code<>''
 set @s = @s +' and code='''+@code+''''
if @producctName<>''
 set @s= @s + ' and productName='''+@producctName+''''
exec(@s)

原文地址:https://www.cnblogs.com/myssh/p/1486823.html